Table.AddRankColumn Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Table.AddRankColumn function in Power Query adds a column that ranks the rows of a table based on the values in one or more columns. You also get to decide what rank two rows get when they have the same value.

In this article, I’ll show you how to use it with practical examples, from a basic rank to handling ties, ranking within groups, and dealing with blank values.

Follow along with the example file

Table.AddRankColumn Function Examples.xlsx

Download

Table.AddRankColumn Function Syntax

= Table.AddRankColumn(table as table, newColumnName as text, comparisonCriteria as any, optional options as nullable record) as table
  • table – the table you want to rank. This is usually the previous step, such as Source.
  • newColumnName – the name of the new rank column, as text in double quotes, such as "Rank". It has to be a name the table doesn’t already use.
  • comparisonCriteria – the column (or columns) to rank by. A column name on its own, like "Score", ranks from the lowest value to the highest. To rank from the highest value, use a list with the sort order, like {"Score",Order.Descending}.
  • options (optional) – a record with a RankKind field that decides how ties are ranked: RankKind.Competition (the default), RankKind.Dense, or RankKind.Ordinal, for example [RankKind=RankKind.Dense].

What it returns – the same table with the rank column added at the end. The rows also get sorted in rank order, so rank 1 is at the top.

I’ve tested the Table.AddRankColumn function in Power Query in Excel for Microsoft 365. It’s also part of the Power Query M language that Power BI uses.

When to Use the Table.AddRankColumn Function

Use this function when you need to:

  • Rank rows by a number, from the highest to the lowest or the lowest to the highest
  • Control how tied values are ranked (1, 1, 3 or 1, 1, 2 or 1, 2, 3)
  • Give places to the top performers, like 1st, 2nd, and 3rd
  • Break ties using a second column
  • Rank rows within each group, like each region or each team
  • Add a rank while keeping the rows in their original order

Example 1: Rank Values from Lowest to Highest

Let’s start with the simplest version, where you only give the table, the name of the new column, and the column to rank by.

Below is an Excel table named GolfScores with each golfer’s number of strokes, and I want to add a Rank column where the fewest strokes get rank 1.

Golf scores with each golfer's strokes

To follow along, select any cell in the table and go to Data > From Table/Range to open it in the Power Query Editor. If Power Query adds a Changed Type step, use #"Changed Type" in place of Source in the formulas below.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddRankColumn(Source,"Rank","Strokes")

Result: the golfers sorted from the fewest strokes to the most, with Pamela Brown (68) at rank 1 and Sheila Webb (75) at rank 6

Golfers ranked from the fewest strokes, Pamela Brown at rank 1

Here, I gave just the column name, “Strokes”, as the third argument. When you do that, the lowest value gets rank 1, which is exactly what you want in golf.

Notice that the rows have moved.

Table.AddRankColumn sorts the table by the rank, so Pamela Brown is now in the first row even though she was second in the original table. Example 8 shows how to keep the original order.

Example 2: Rank Values from Highest to Lowest

Most of the time, you want the biggest number to get rank 1. You do that by adding the sort order to the column name.

Below is an Excel table named Fundraiser with the amount each volunteer raised, and I want to add a Rank column where the highest amount gets rank 1.

Fundraiser amounts raised by each volunteer

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddRankColumn(Source,"Rank",{"Amount Raised",Order.Descending})

Result: the volunteers sorted from the highest amount to the lowest, with Nicole Phillips (1540) at rank 1 and Patricia Bennett (720) at rank 6

Volunteers ranked from the highest amount, Nicole Phillips at rank 1

Here, the third argument is a list with two items: the column name and Order.Descending. That tells the function to rank from the highest amount to the lowest.

If you use Order.Ascending instead, the lowest amount gets rank 1, the same as giving just the column name in Example 1.

Another way to rank in Power Query is to sort the table and then add an index column with the Table.AddIndexColumn function. That gives every row a different number, even when two values are tied.

Table.AddRankColumn lets you choose how ties are ranked, which the next example shows.

Example 3: See How Ties Are Ranked (Competition, Dense, and Ordinal)

When two rows have the same value, the RankKind option decides what ranks they get. This is the part of the function you’ll want to get right.

Below is an Excel table named QuizScores with each student’s score, where some students have the same score. I want to rank them in three ways, in three new columns (Competition, Dense, and Ordinal), so you can compare them side by side.

Quiz scores with tied students at 95 and 88

Go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor, open the Advanced Editor (Home > Advanced Editor) and use this code.

The Source line reads the table by its name, so if your table has a different name, change QuizScores to that name. The same goes for the table names in the later examples.

let
Source = Excel.CurrentWorkbook(){[Name="QuizScores"]}[Content],
#"Added Competition" = Table.AddRankColumn(Source,"Competition",{"Score",Order.Descending}),
#"Added Dense" = Table.AddRankColumn(#"Added Competition","Dense",{"Score",Order.Descending},
[RankKind=RankKind.Dense]),
#"Added Ordinal" = Table.AddRankColumn(#"Added Dense","Ordinal",{"Score",Order.Descending},
[RankKind=RankKind.Ordinal])
in
#"Added Ordinal"

Result: three rank columns for the same scores. Competition goes 1, 1, 3, 3, 3, 6, 7, Dense goes 1, 1, 2, 2, 2, 3, 4, and Ordinal goes 1 to 7

Competition, Dense and Ordinal rank columns side by side for the same scores

Here, each step adds one rank column, and the only difference between them is the RankKind. Shane Anderson and Kimberly Turner both scored 95, and three students scored 88, so you can see how each kind treats the ties.

Competition is what you get when you leave out the options. Tied rows share a rank, and the next rank skips ahead.

The two 95s are both 1, so the 88s are 3 (not 2), because two students are ahead of them.

Dense also gives tied rows the same rank, but it doesn’t skip any numbers. The 88s get 2, and the 79 gets 3.

Dense is useful when you care about the distinct score levels.

Ordinal gives every row its own number, even when values are tied. Tied rows keep the order they had in the table, so Shane Anderson (who comes before Kimberly Turner in the table) gets 1 and Kimberly Turner gets 2.

Example 4: Give Places to the Top 3 Scores

Dense ranking is handy when you want to give out places like 1st, 2nd, and 3rd, and some people are tied.

Below is an Excel table named ScienceFair with the points each student got from the judges, and I want to add a Place column and keep only the students in the top 3 places.

Science fair points for seven students

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ScienceFair"]}[Content],
#"Added Place" = Table.AddRankColumn(Source,"Place",{"Points",Order.Descending},
[RankKind=RankKind.Dense]),
#"Top 3 Places" = Table.SelectRows(#"Added Place",each [Place] <= 3)
in
#"Top 3 Places"

Result: five students in the top 3 places. Keith Johnson and Kenneth Cooper (49) are 1st, Connor Stewart and Mallory Hughes (46) are 2nd, and Todd King (44) is 3rd

Five students in the top 3 places with dense ranking

Here, the Added Place step ranks the points with RankKind.Dense, so the tied students share a place and no place number gets skipped. The Top 3 Places step then uses the Table.SelectRows function to keep only the rows where the place is 3 or less.

If you used the default Competition ranking here, the places would go 1, 1, 3, 3, 5. Nobody would get 2nd place, and Todd King would be 5th, so he’d drop out of the top 3.

Example 5: Give Every Row a Unique Rank

Sometimes you can’t have ties at all, like when each rank is a slot that only one person can fill. That’s what Ordinal ranking is for.

Below is an Excel table named Applications with scholarship applications in the order they came in (by App No), and I want to add a Rank column where every applicant gets a different rank.

Scholarship applications with App No and score

Select any cell in the Applications table and go to Data > From Table/Range to open it in the Power Query Editor, as in Example 1. Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddRankColumn(Source,"Rank",{"Score",Order.Descending},
[RankKind=RankKind.Ordinal])

Result: ranks 1 to 6 with no ties, where Christopher Collins (91) is rank 1 and Lucy Griffin (91) is rank 2

Ordinal ranks 1 to 6 with no ties

Here, RankKind.Ordinal gives every row its own rank. Christopher Collins and Lucy Griffin both scored 91, and Christopher gets the better rank because his application (102) comes before Lucy’s (105) in the table.

The same goes for Wendy Wood and Addison Matthews, who both scored 85. This works here because the table is already in the order the applications came in.

Important: Ordinal breaks ties using the order the rows are in. Microsoft doesn’t document this, so if the rows could come in a different order, break the tie with a second column, as in the next example.

Follow along with the example file

Table.AddRankColumn Function Examples.xlsx

Download

Example 6: Break Ties Using a Second Column

When two rows tie on the main column, you can use a second column to decide which one ranks higher.

Below is an Excel table named GameScores with each player’s points and the time they took, and I want to add a Rank column that ranks by points first, and by the faster time when the points are tied.

Game scores with points and time in seconds

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddRankColumn(Source,"Rank",
{{"Points",Order.Descending},{"Time (sec)",Order.Ascending}})

Result: ranks 1 to 6, where Steven Evans (410 points, 195 seconds) is rank 1 and Howard Robinson (410 points, 220 seconds) is rank 2

Ranks by points, with the faster time winning ties

Here, the third argument is a list of two lists. The first one ranks by Points from the highest to the lowest.

The second one only comes into play when the points are tied, and it ranks the faster time (the lower number) higher.

Steven Evans and Howard Robinson both have 410 points, but Steven was faster, so he’s rank 1. In the same way, Dennis Mason (188 seconds) is ranked above Brianna Bryant (212 seconds), who also has 340 points.

If two rows still tie on both columns, they share a rank, because this formula uses the default Competition ranking.

Example 7: Rank Within Each Group

This next one comes up a lot. Instead of one ranking for the whole table, you want a separate ranking for each group, like each region.

Below is an Excel table named RegionSales with each employee’s region and sales, and I want to add a Region Rank column that ranks the employees within their own region.

Employee sales for the East and West regions

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="RegionSales"]}[Content],
#"Grouped Rows" = Table.Group(Source,{"Region"},
{{"Ranked",each Table.AddRankColumn(_,"Region Rank",{"Sales",Order.Descending}),type table}}),
#"Combined Regions" = Table.Combine(#"Grouped Rows"[Ranked])
in
#"Combined Regions"

Result: a Region Rank column that goes 1 to 4 for the East employees and 1 to 3 for the West employees

A Region Rank column that restarts for each region

Here, the Table.Group function splits the rows into one small table for each region. For each of those tables (the underscore _ stands for the table), Table.AddRankColumn ranks the employees by sales.

The ranked tables go into a new column called Ranked, one per region. The Combined Regions step then uses the Table.Combine function to stack them back into one table.

The regions come out in the order they first appear in the source (East, then West), and within each region the rows are sorted by rank. You can use any RankKind inside the group too, like [RankKind=RankKind.Dense].

Example 8: Add a Rank Without Changing the Row Order

As you saw in Example 1, Table.AddRankColumn sorts the rows by rank. If you want the rank but need the rows to stay in their original order, you can put the order back.

Below is an Excel table named Catalog with products listed in catalog order and the units sold for each, and I want to add a Sales Rank column without changing the order of the products.

Kitchen products in catalog order with units sold

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Catalog"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source,"Index",1,1,Int64.Type),
#"Added Rank" = Table.AddRankColumn(#"Added Index","Sales Rank",{"Units Sold",Order.Descending}),
#"Sorted Rows" = Table.Sort(#"Added Rank",{{"Index",Order.Ascending}}),
#"Removed Index" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
in
#"Removed Index"

Result: the products in their original catalog order, with a Sales Rank column (Air Fryer is rank 1 and Hand Mixer is rank 6)

Products in catalog order with a Sales Rank column

Here, the Added Index step uses the Table.AddIndexColumn function to number the rows 1, 2, 3, and so on, in their original order. The Added Rank step then ranks by units sold, which sorts the rows by rank.

The Sorted Rows step uses the Table.Sort function to sort by the index again, which puts the rows back in catalog order. The index was only a helper, so the last step removes it.

Example 9: Rank a Column That Has Blank Values

Blank cells can give you a surprising result, especially when you rank from the lowest value to the highest.

Below is an Excel table named ResponseTimes with each support agent’s average response time in hours, where Adam Campbell has no time yet (I left his cell blank on purpose). I want to add a Rank column where the fastest time gets rank 1.

Support agents' average response hours with one blank

Select any cell in the ResponseTimes table and go to Data > From Table/Range to open it in the Power Query Editor, as in Example 1. Then add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddRankColumn(Source,"Rank","Avg Hours")

Result: Adam Campbell, whose time is blank (null), is at rank 1, and the fastest agent, Darren Perry (1.9), is at rank 2

The blank (null) response time lands at rank 1

This happens because a blank cell comes into Power Query as null, and when ranking, null counts as lower than any number. So when you rank from the lowest to the highest, the blank row lands at the top.

When you rank from the highest to the lowest, it ends up at the bottom instead.

To fix this, remove the blank rows before you rank. Create a new blank query (it reads the same ResponseTimes table), open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ResponseTimes"]}[Content],
#"Removed Blanks" = Table.SelectRows(Source,each [Avg Hours] <> null),
#"Added Rank" = Table.AddRankColumn(#"Removed Blanks","Rank","Avg Hours")
in
#"Added Rank"

Result: five agents ranked from 1 to 5, with Darren Perry (1.9) at rank 1 and Lucas Martin (4.1) at rank 5

Five agents ranked after removing the blank row

Here, the Removed Blanks step keeps only the rows where Avg Hours is not null, so Adam Campbell’s row is left out before the ranking happens. The Added Rank step then ranks the five agents who have a time.

Keep in mind that this removes Adam Campbell from the result, so the ranked list only has the agents who have a time.

Other Excel Articles You May Also Like:

  • List.Sort – Use List.Sort in Power Query M to sort a list.
  • List.PositionOf – Use List.PositionOf in Power Query M to find where a value sits in a list.
  • Table.Distinct – Use Table.Distinct in Power Query M to remove duplicate rows.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.