Excel Power Query Rank: How to Create Ranking Columns
In this article
To rank data in Excel Power Query, you must first sort your data by the target column and then add an Index Column. Because Power Query does not have a built-in "RANK" function like standard Excel formulas, the combination of sorting and indexing creates a sequential rank based on the order of the rows. This process allows you to create a dynamic ranking system that updates automatically whenever you refresh your data source.
What You'll Need
- Microsoft Excel with the Power Query add-in (built-in to Excel 2016 and later).
- A dataset imported into the Power Query Editor (via Data > Get Data).
- A numeric column that you intend to rank (e.g., Sales, Scores, or Dates).
Step-by-Step
- Open the Power Query Editor: Go to the Data tab on the Excel ribbon. Click Get Data and select your source, or if your data is already a table in Excel, click From Table/Range. This opens the Power Query Editor window. Note that menu labels may vary slightly depending on your version of Excel or region.
- Sort the Ranking Column: Locate the column you want to rank. Click the drop-down arrow in the column header. Select Sort Descending if you want the highest value to be Rank 1 (e.g., for revenue), or Sort Ascending if the lowest value should be Rank 1 (e.g., for race times). This step is critical because the Index column simply numbers rows in their current visual order.
- Buffer the Table (Optional but Recommended): In some complex queries, Power Query may re-order rows during the loading process, which can break your rank. To prevent this, you can wrap your previous step in a buffer. Go to the formula bar, and wrap the table name in the function
Table.Buffer(). For example, if your previous step was= Table.Sort(...), change it to= Table.Buffer(Table.Sort(...)). This forces Power Query to freeze the sorted order in memory. - Add the Index Column: Navigate to the Add Column tab on the top ribbon. In the Index Column drop-down menu, select From 1. This creates a new column at the end of your table starting with 1, 2, 3, and so on. This index now serves as your rank.
- Rename the Column: Double-click the header of the new "Index" column and rename it to "Rank" or "Position" to make the data clear for the end user.
- Reorder Columns: If you want the Rank column to appear first, click and drag the column header to the far left of the table.
- Load the Data: Go to the Home tab and click Close & Load. Your ranked data will now appear in a new Excel worksheet.
Common Mistakes
One of the most frequent errors is forgetting to sort the data before adding the index. If you add an index to an unsorted list, the rank is meaningless as it simply reflects the order in which the data was originally entered into the source file. Always verify the sort order visually before clicking the Index button.
Another common mistake is ignoring the "Tie" problem. The Index Column method creates a unique rank for every single row. If two people have the exact same sales figure of $5,000, one will be ranked 4th and the other 5th based solely on their position in the list. This is known as "Competition Ranking" or "Ordinal Ranking." If your business requirements demand that ties receive the same rank (e.g., both get 4th place), the Index method alone is insufficient and requires a custom M-code formula to group identical values.
Users also often forget to use Table.Buffer when dealing with large datasets or merged queries. Power Query uses a "lazy evaluation" engine, meaning it optimizes the order of operations. Sometimes it decides to move the sorting step after the indexing step during the final load to save resources, which completely scrambles your ranking. Buffering the table ensures the sort is locked in before the index is applied.
Tips
To create a "Top 10" list, you can add a filter step after creating your rank. Click the filter arrow on your new Rank column, select Number Filters > Less Than or Equal To, and enter 10. This ensures that your final output only contains the top performers, regardless of how many thousands of rows are in the original source.
If you need to rank data within categories (for example, ranking employees within their specific departments), you cannot use a simple global index. Instead, you must use the Group By feature. Group by the "Department" column, select "All Rows" as the operation, and then create a custom column that adds an index to each nested table. This allows the rank to reset to 1 every time a new department begins.
For those who prefer a cleaner look, you can remove the original sorting column after the rank is established, although this is rarely recommended as it makes the rank harder to verify. Instead, keep the value column and the rank column side-by-side for transparency.
Troubleshooting
If your ranks are appearing in a random order after you load the data into the Excel sheet, the most likely cause is the query optimization mentioned earlier. To fix this, go back into the Power Query Editor, find the step immediately before you added the Index column, and apply Table.Buffer() to that step in the formula bar. This prevents the engine from rearranging the rows during the load process.
If the Index column is not appearing at all, check that you are in the Add Column tab and not the Transform tab. The Transform tab modifies existing columns, while the Add Column tab creates new ones. If the column is there but the numbers are incorrect (e.g., starting at 0), ensure you selected "From 1" instead of "From 0" in the Index menu.
If you encounter an error stating that the column cannot be sorted, check for data type inconsistencies. Select the column, go to the Transform tab, and change the Data Type to "Decimal Number" or "Whole Number." Replace any errors or nulls using the Replace Values tool before sorting.
Frequently Asked Questions
Can I handle ties so that two identical values get the same rank?
The standard Index Column tool cannot do this; it always assigns a unique number. To handle ties, you must group the data by the value column, count the rows in each group, and then use a running total of those counts to assign ranks. This requires writing a custom M-function using List.Accumulate or a similar advanced technique.
Does the rank update when the source data changes?
Yes. Because Power Query records these as "Applied Steps," any time you click Data > Refresh All, Excel will re-import the data, re-sort it, and re-apply the index, ensuring your ranks are always current.
Can I rank based on multiple columns?
Yes. You can apply multiple sort steps. For example, you can sort by "Region" ascending and then by "Sales" descending. When you add the Index column after these two sorts, the rank will reflect the hierarchy of both criteria.
Is Power Query ranking faster than the RANK.EQ formula?
For very large datasets (tens of thousands of rows), Power Query is generally more efficient. Standard Excel formulas can slow down a workbook because they recalculate every time a cell changes, whereas Power Query only calculates when the data is refreshed.
Can I create a rank that goes from bottom to top?
Yes. Simply change the initial sort step to Sort Ascending. The Index column will then assign "1" to the smallest value in your dataset.