AMQL Docs
AQL ReferenceFunctionsWindow functionsRanking

rank

AQL Window Function: A window function that returns the rank of rows within a partition of a table. Tie values are assigned the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...

Knowledge Checkpoint

Readings that will help you understand this documentation better:

Definition

A window function that returns the rank of rows within a partition of a table. Tie values are assigned the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...

To get consecutive rank, use dense_rank.

Syntax

rank(order: order_expr)
rank(order: order_expr, ..., partition: partition_expr, ...)
Examples
rank(order: count(orders.id) | desc())
rank(order: count(orders.id), order: average(users.age))

// with partition
rank(order: count(orders.id), partition: orders.status)

// Axis-aware examples
rank(order: 'rows')                              // Rank by row/x-axis order
rank(order: 'columns' | desc())                  // Rank by column/legend order (descending)
rank(order: revenue | desc(), partition: 'rows') // Rank within each row

Input

  • order (required, repeatable): A field that is used for ordering. The order defaults to ascending. The order can be set explicitly with asc() or desc(). You can also use axis references:
    • 'rows' or 'x_axis': Order by dimensions mapped to rows/x-axis
    • 'columns' or 'legend': Order by dimensions mapped to columns/legend
    • E.g. rank(order: count(orders.id) | desc()) or rank(order: 'rows')
  • partition (repeatable, optional): A field that is used for partitioning the table. You can use axis references like 'rows', 'columns', 'x_axis', or 'legend'. E.g. rank(order: count(orders.id), partition: orders.status) or rank(order: revenue, partition: 'rows')

Output

Rank of the current row within its partition (if no partition is specified, the whole table is considered as a single partition). If two rows are tied for a rank, each tied rows receives the same rank. The next rank in the sequence is not consecutive. E.g. 1, 1, 3, 4, 4, 6, ...

Sample Usages

Rank top buyer by gender and age group
explore {
  dimensions {
    users.gender, users.age_group
  }
  measures {
    count(orders.id),
    _rank: rank(order: count(orders.id) | desc())
  }
}
users->genderusers->age_groupcount_orders->id_rank
fYoung Adult7431
fTeen6402
mYoung Adult3563
mTeen2864
fMature505
mMature246
Open in playground ↗
Rank top buyer by age group partition by gender
explore {
  dimensions {
    users.gender, users.age_group
  }
  measures {
    count(orders.id),
    _rank: rank(order: count(orders.id) | desc(), partition: users.gender)
  }
}
users->genderusers->age_groupcount_orders->id_rank
fYoung Adult7431
fTeen6402
fMature503
mYoung Adult3561
mTeen2862
mMature243
Open in playground ↗

Using rank in a dimension definition is straight-forward. Remember that the rank is calculated against all rows in the model.

dimension ranking_by_age {
  label: 'Rank By Age'
  type: 'number'
  hidden: false
  description: ''
  definition: @aql rank(order: users.age | desc()) ;;
}

You can see that users with the same age are assigned the same rank

Rank users by Age
explore {
  dimensions {
    users.age,
    users.ranking_by_age
  }
}
users->ageusers->ranking_by_age
23399
392
26201
3422
22505
19963
376
3327
3151
27160
20775
21623
181165
24310
161634
3610
384
3238
25247
171398
401
28114
3515
2983
3068
Open in playground ↗

You can also use it in filter() to filter out the top 10 users by age:

Filter top 1 Youngest Users
users 
| select(users.full_name, _rank: rank(order: users.age))
| filter(_rank == 1)
users->full_name_rank
Bacolor Hong1
Vea Maybelle1
Beattie Chana1
Tudman Kelly1
Paplow Dino1
Schepp Guan1
Sinkovich Tyron1
Westler Mark1
Goldberg Hermila1
Chatagnier Sondra1
Stepney Vuong1
Balcerzak Syed1
Madsen Gomez1
Vankammen Tressa1
Floras Milissa1
Olkowski Easter1
Gunsolus Sabra1
Leas Deb1
Carlone Saito1
Baptista Cherie1
Lieser Seki1
Miss Brice1
Doyer Bishop1
Nevala Hang1
Muszynski Sheilah1
Malatesta Kubo1
Villenas Willard1
Danser Raguel1
Houseman Verda1
Kuney Jerilyn1
Anecelle Roland1
Bauknight Mai1
Frericks Takagi1
Noonkester Pacheco1
Friess Euna1
Zakutney Hermelinda1
Burchill Elliott1
Dooney Odelia1
Zawislak Narayan1
Kernighan Angel1
Muschaweck Hannelore1
Castellaneta Arnold1
Shannonhouse Renae1
Plott Rose1
Kuechler Nelle1
Gurrero Kawashima1
Corigliano Josphine1
Fauske Matsuura1
Borsh Carmella1
Rout Pablo1
Berray Darrel1
Treglia Melissia1
Lanter Gilbert1
Venskoske Julietta1
Gulisano Brady1
Leighty Kelley1
Vandale Maggie1
Werderman Florene1
Dolinsky Eugena1
Ped Emmett1
Whitehill Mui1
Ruple Evelia1
Sulloway Cammie1
Prieur Nick1
Goetze Allison1
Bonnet Otilia1
Drey Elfreda1
Coggeshall Prom1
Birdsey Choudhry1
Aldape Courtney1
Raisley Petrina1
Pentek Reina1
Mizwicki Genevie1
Kunimitsu Daniel1
Shollenbarger Cruz1
Liebler Chawla1
Lehrer Margarito1
Open in playground ↗

See also

On this page