OpenBI Docs Buy

Table.AddRankColumn

Ranks the rows by one or more columns and puts each row's rank in a new column.

Group: Table. Power Query M has it too: a query that uses it is written as in Power BI.

Syntax

Table.AddRankColumn(table as table, newColumnName as text, comparisonCriteria as any, optional options as nullable record) as table
ParameterTypeDescription
tabletableThe rows to rank.
newColumnNametextThe name of the rank column.
comparisonCriteriaanyA column name; a pair {name, Order.Descending}; or a list of them: what the rows are ranked by, lowest first unless you say descending.
optionsnullable record
optional
[RankKind = RankKind.Competition] (the default), RankKind.Dense or RankKind.Ordinal: how ties are numbered.

Returns

table. The rows in rank order, with the rank as the last column.

Remarks

  • With RankKind.Competition, tied rows share a rank and the next rank skips as many places (1, 2, 2, 4); with RankKind.Dense it does not skip (1, 2, 2, 3); RankKind.Ordinal gives every row its own rank.
  • Rank 1 is the lowest value unless the criteria say Order.Descending.

Examples

Shops by sales, best first

let
    Shops = #table({"Shop", "Sales"}, {{"Oslo", 120}, {"Bergen", 340}, {"Trondheim", 120}, {"Stavanger", 90}})
in
    Table.AddRankColumn(Shops, "Rank", {"Sales", Order.Descending})
ShopSalesRank
Bergen3401
Oslo1202
Trondheim1202
Stavanger904

Oslo and Trondheim tie for second place; the next shop is fourth.

Dense ranks

let
    Shops = #table({"Shop", "Sales"}, {{"Oslo", 120}, {"Bergen", 340}, {"Trondheim", 120}, {"Stavanger", 90}})
in
    Table.AddRankColumn(Shops, "Rank", {"Sales", Order.Descending}, [RankKind = RankKind.Dense])
ShopSalesRank
Bergen3401
Oslo1202
Trondheim1202
Stavanger903

Table.AddIndexColumn, Table.Sort, Table.MaxN.

Last updated · OpenBI Studio 0.3.79