AMQL Docs
AQL ReferenceFunctionsWindow functionsRanking

dense_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 consecutive. E.g. 1, 1, 2, 3, 3, 4, ...

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 consecutive. E.g. 1, 1, 2, 3, 3, 4, ...

To return non-consecutive rank, use rank.

Syntax

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

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

// Axis-aware examples
dense_rank(order: 'rows')                              // Dense rank by row order
dense_rank(order: 'columns' | desc())                  // Dense rank by column order (descending)
dense_rank(order: revenue | desc(), partition: 'rows') // Dense 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. dense_rank(order: count(orders.id) | desc()) or dense_rank(order: 'rows')
  • partition (repeatable, optional): A field that is used for partitioning the table. You can also use axis references like 'rows', 'columns', 'x_axis', or 'legend'. E.g. dense_rank(order: count(orders.id), partition: orders.status) or dense_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 consecutive. E.g. 1, 1, 2, 3, 3, 4, ...

Sample Usages

Rank top buyer by gender and age group
explore {
  dimensions {
    users.gender, users.age_group
  }
  measures {
    count(orders.id),
    _rank: dense_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: dense_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 dense_rank(order: users.age | desc()) ;;
}
Rank users by Age
explore {
  dimensions {
    users.age,
    users.ranking_by_age
  }
}
users->ageusers->ranking_by_age
2120
392
365
2417
3110
2021
374
2318
2516
1922
383
1724
356
1625
338
2219
1823
329
2813
347
2615
2714
2912
401
3011
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: dense_rank(order: users.age))
| filter(_rank == 1)
users->full_name_rank
Fauske Matsuura1
Treglia Melissia1
Miss Brice1
Danser Raguel1
Balcerzak Syed1
Corigliano Josphine1
Burchill Elliott1
Plott Rose1
Anecelle Roland1
Westler Mark1
Friess Euna1
Leas Deb1
Houseman Verda1
Sinkovich Tyron1
Malatesta Kubo1
Zawislak Narayan1
Whitehill Mui1
Lanter Gilbert1
Dooney Odelia1
Stepney Vuong1
Kuney Jerilyn1
Venskoske Julietta1
Muszynski Sheilah1
Borsh Carmella1
Birdsey Choudhry1
Aldape Courtney1
Ruple Evelia1
Schepp Guan1
Gunsolus Sabra1
Dolinsky Eugena1
Berray Darrel1
Drey Elfreda1
Liebler Chawla1
Vankammen Tressa1
Castellaneta Arnold1
Madsen Gomez1
Vea Maybelle1
Pentek Reina1
Paplow Dino1
Gulisano Brady1
Shollenbarger Cruz1
Lieser Seki1
Zakutney Hermelinda1
Goldberg Hermila1
Chatagnier Sondra1
Floras Milissa1
Kunimitsu Daniel1
Prieur Nick1
Carlone Saito1
Villenas Willard1
Bacolor Hong1
Leighty Kelley1
Noonkester Pacheco1
Rout Pablo1
Werderman Florene1
Goetze Allison1
Bauknight Mai1
Gurrero Kawashima1
Mizwicki Genevie1
Doyer Bishop1
Nevala Hang1
Kuechler Nelle1
Sulloway Cammie1
Baptista Cherie1
Olkowski Easter1
Vandale Maggie1
Raisley Petrina1
Coggeshall Prom1
Beattie Chana1
Lehrer Margarito1
Bonnet Otilia1
Muschaweck Hannelore1
Frericks Takagi1
Shannonhouse Renae1
Ped Emmett1
Tudman Kelly1
Kernighan Angel1
Open in playground ↗

See also

On this page