Table.AddRankColumn Function (Power Query M)

Table.AddRankColumn adds a new column that ranks each row against the others based on one or more columns you choose. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.

If you want to number your rows in Power Query by sales, score, or any value, from highest to lowest or the other way around, this is the function that does it in one step.

Syntax of Table.AddRankColumn Function

Table.AddRankColumn(table as table, newColumnName as text, comparisonCriteria as any, optional options as nullable record) as table

where

  • table (required, table). The table you want to rank.
  • newColumnName (required, text). The name for the new rank column that gets added.
  • comparisonCriteria (required, any). How rows are ranked. Use {"Column",Order.Descending} to rank by one column, or a list of those like {{"Score",Order.Descending},{"Time",Order.Ascending}} to add tie-breakers.
  • options (optional, nullable record). A record that tweaks the ranking. The main one is [RankKind=...], which picks how ties are handled. Omit it for the default behavior.

Returns: the original table with one extra column of rank numbers. The output rows are reordered to follow the ranking, so the result is sorted by rank, not by the source order.

In plain terms, you point it at a table, name the new column, and tell it which column to rank by. It hands back the table with a rank number on every row.

Example 1: Rank salespeople by sales

You have a list of salespeople and their sales, and you want to rank them from highest seller to lowest.

Here is the starting data:

SalespersonSales
Maya940
Leo1200
Ivy760
Sam1050

Rank by Sales in descending order:

let
Source = Excel.CurrentWorkbook(){[Name="Example1"]}[Content],
AddedRank = Table.AddRankColumn(Source,"SalesRank",{"Sales",Order.Descending})
in
AddedRank

The result adds a SalesRank column and reorders the rows by rank:

SalespersonSalesSalesRank
Leo12001
Sam10502
Maya9403
Ivy7604

Leo has the highest sales, so he gets rank 1. Notice the rows are no longer in the original order.

Example 2: Handle ties with RankKind.Competition

Two customers have the same revenue and you want tied rows to share a rank. RankKind.Competition does this, and it leaves a gap after the tie.

Here is the starting data:

CustomerRevenue
Bob200
Jim100
Paul200
Ringo50

Rank by Revenue descending with RankKind.Competition:

let
Source = Excel.CurrentWorkbook(){[Name="Example2"]}[Content],
AddedRank = Table.AddRankColumn(Source,"RevenueRank",{"Revenue",Order.Descending},[RankKind=RankKind.Competition])
in
AddedRank

Both 200 rows share rank 1, then the next rank jumps to 3:

CustomerRevenueRevenueRank
Bob2001
Paul2001
Jim1003
Ringo504

Rank 2 is skipped because two rows already occupy the top spot. This is the default behavior when you omit options.

Example 3: Close the gap with RankKind.Dense

Same data, but this time you want tied rows to share a rank with no gap afterward. RankKind.Dense does exactly that.

Here is the starting data:

CustomerRevenue
Bob200
Jim100
Paul200
Ringo50

Rank by Revenue descending with RankKind.Dense:

let
Source = Excel.CurrentWorkbook(){[Name="Example3"]}[Content],
AddedRank = Table.AddRankColumn(Source,"RevenueRank",{"Revenue",Order.Descending},[RankKind=RankKind.Dense])
in
AddedRank

The two 200 rows share rank 1, and the next rank is 2, not 3:

CustomerRevenueRevenueRank
Bob2001
Paul2001
Jim1002
Ringo503

No rank is skipped, so the numbers stay continuous even with a tie.

Example 4: Force unique ranks with RankKind.Ordinal

Same data again, but now you want every row to get a distinct rank even when values tie. RankKind.Ordinal breaks ties using the source order.

Here is the starting data:

CustomerRevenue
Bob200
Jim100
Paul200
Ringo50

Rank by Revenue descending with RankKind.Ordinal:

let
Source = Excel.CurrentWorkbook(){[Name="Example4"]}[Content],
AddedRank = Table.AddRankColumn(Source,"RevenueRank",{"Revenue",Order.Descending},[RankKind=RankKind.Ordinal])
in
AddedRank

The tied 200 rows get 1 and 2 instead of sharing a rank:

CustomerRevenueRevenueRank
Bob2001
Paul2002
Jim1003
Ringo504

Bob comes before Paul in the source, so he takes rank 1. No two rows share a rank.

Example 5: Break ties with a second column

When the main column ties, you can add your own tie-breaker instead of relying on source order. Pass a list of criteria, and the second one settles the tie.

Here is the starting data:

PlayerScoreTime
Amy9050
Ben9045
Cleo8540
Dan9560

Rank by Score descending, then by Time ascending for ties:

let
Source = Excel.CurrentWorkbook(){[Name="Example5"]}[Content],
AddedRank = Table.AddRankColumn(Source,"Rank",{{"Score",Order.Descending},{"Time",Order.Ascending}},[RankKind=RankKind.Competition])
in
AddedRank

Amy and Ben both score 90, so the faster time wins:

PlayerScoreTimeRank
Dan95601
Ben90452
Amy90503
Cleo85404

Ben beats Amy on the tie because his Time of 45 is lower. With both columns deciding order, no tie is left for RankKind to handle.

Things to keep in mind with Table.AddRankColumn

  • The rank column is added as the last column. The new column lands at the end of the table. If you want it up front, follow the step with Table.ReorderColumns.
  • Ranking within groups is not built in. To rank rows inside each category, group with Table.Group and call Table.AddRankColumn on each group’s sub-table inside the aggregation. A single top-level call ranks the whole table as one set.
  • It is a newer function, so older builds may not have it. If you hit Expression.Error: The name Table.AddRankColumn wasn't recognized, your Power Query build is too old. Update Excel or Power BI, or rank with Table.AddIndexColumn on a sorted table instead.

Common questions about Table.AddRankColumn

How is this different from sorting and adding an index column?

An index column just numbers rows 1,2,3 no matter what, so ties get different numbers and you handle ordering yourself. Table.AddRankColumn understands ties through RankKind and ranks by your criteria in one step.

Can I rank rows within groups?

Not directly. Group the table with Table.Group, then apply Table.AddRankColumn to each group’s sub-table inside the aggregation, so each group is ranked on its own.

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.