Table.AddRankColumn

Table.AddRankColumn is a Power Query M function that appends a column to the table with the ranking of one or more other columns based on comparison criteria. The function returns a new table with the added rank column.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Table.AddRankColumn(
   table as table,
   newColumnName as text,
   comparisonCriteria as any,
   optional options as nullable record,
) as table
ArgumentAttributeDescription
TableThis is the main table to add a ranking column to.
newColumnNameThe name for the ranking column as text.
comparisonCriteriaUsed to provide one or more columns sorting directions. You can provide a single list, or a list of lists for multiple criteria, example: { { “Revenue”, Order.Descending }, {“CustomerID”, Order.Ascending } }
optionsOptionalAllows you to provide a RankKind.Type in the form of a record. You can choose from RankKind.Competition, RankKind.Dense and RankKind.Ordinal. Provide the argument as record: [ RankKind = RankKind.Ordinal ]

Description

Appends a column named newColumnName to the table with the ranking of one or more other columns described by comparisonCriteria. The RankKind option in options can be used by advanced users to pick a more-specific ranking method.

Examples

Imagine you have a dataset called Source with the following setup:

Source table as dataset for RankKind.Type

You can rank this data using the Table.AddRankColumn function. Below examples rank the data based on the revenue amount.

= Table.AddRankColumn( 
    Source,
    "Revenue Asc",
    { "Revenue", Order.Ascending },   // Sort column in Ascending order
    [ RankKind = RankKind.Competition ]
 )

= Table.AddRankColumn( 
    Source,
    "Revenue Desc",
    { "Revenue", Order.Descending },  // Sort column in Descending order
    [ RankKind = RankKind.Competition ]
 )
Using Table.AddRankColumn with Ascending and Descending Order

To make this ranking unique, you can provide provide multiple columns in the comparison criteria.

= Table.AddRankColumn( 
    Source,
    "Revenue Asc",
    { { "Revenue", Order.Descending }, {"CustomerID", Order.Ascending } },
    [ RankKind = RankKind.Competition ]
 )

= Table.AddRankColumn( 
    Source,
    "Revenue Desc",
    { { "Revenue", Order.Descending }, {"CustomerID", Order.Ascending } },
    [ RankKind = RankKind.Competition ]
 )
Using Table.AddRankColumn with Multiple Sort by Columns

Alternatively you can make use of Ranking types like RankKind.Ordinal and RankKind.Dense.

Other functions related to Table.AddRankColumn are:

BI Gorilla Blog

Last update: August 28, 2023 | Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-addrankcolumn
© 2023 BI Gorilla. All rights reserved. Content derived from Microsoft documentation is property of Microsoft Corp.