Skip to content

Calculating tied ranks

This page adds a column that ranks each product by its sales. The product with the highest sales is ranked 1, and products with equal sales share a rank. The technique is to declare a small function as a step of the query, and then call it once for each row.

The example starts from this Excel table of sales by product, in which two pairs of products have equal sales.

An Excel table with Product and Sales columns, listing six fruits with sales between 10 and 50, where oranges and pears both have 30 and apples and pineapples both have 10

  • Click inside the table, and on Excel’s Data tab click From Table/Range. The Power Query Editor opens with a new query, whose first step is called Source.
  • Click the fx button beside the formula bar to add a new step.
  • Replace everything after the equals sign with this expression:
(SalesValue) => Table.RowCount(Table.SelectRows(Source, each [Sales]>SalesValue) ) + 1

The expression declares a function that works out the rank for one sales value. It takes the sales value from a row, counts the rows in the Source table with higher sales, and adds one. A product with no higher sales is therefore ranked 1, and two products with the same sales get the same count, so they share a rank.

Because the step returns a function rather than a table, the Power Query Editor shows the function’s parameter and an Invoke button instead of data.

The formula bar beginning = (SalesValue) =>, with an Enter Parameter pane below for SalesValue and the signature function (SalesValue as any) as any

The query now has two steps, which load the data and declare the function. A function declared as a step takes the step’s name, so the step needs a meaningful name.

  • In Applied Steps, right-click the second step, which is called Custom1, choose Rename, and name it Rank.

The Applied Steps pane with Source and Custom1, and the right-click menu for Custom1 open at Rename

The function needs the original table to work on, so the next step returns to it.

  • Click the fx button again to add another step.
  • Replace everything after the equals sign with Source, the name of the query’s first step. The new step returns the original table.
  • On the Add Column tab, click Custom Column.
  • Name the new column Rank, and enter this formula, which calls the function for each row:
= Rank([Sales])

The Custom Column dialog with the new column named Rank and the formula = Rank([Sales])

The new column shows each product’s rank.

  • Click Close & Load.

The loaded Excel table with a Rank column: bananas ranked 1, oranges and pears both 2, grapes 4, and apples and pineapples both 5

The ranks skip a number after each tie, so the two products ranked 2 are followed by a product ranked 4, as in Excel’s RANK.EQ function.

let
//Load data from Excel worksheet
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Declare function to calculate the rank
Rank = (SalesValue) => Table.RowCount(Table.SelectRows(Source, each [Sales]>SalesValue) ) + 1,
//Go back to the original input table
Custom2 = Source,
//Add new custom column to show the rank values
#"Added Custom" = Table.AddColumn(Custom2, "Rank", each Rank([Sales]))
in
#"Added Custom"

This example follows one in Chris Webb’s Power Query for Power BI and Excel (Apress).