Sort rows on more than one column
Sort on several keys in one step and know when order is meaningful for the output.
~8 min read · beginner · Module 5: Filtering and sorting rows
By Tahir Nas · Examples run in our simulator · Published · Updated
Before this: Filter rows by value
Sorting changes the row order of the result. Many downstream tools re-sort anyway (a PivotTable, a Power BI visual), so ask first whether order matters. For a ranked list, a printed table, a top-N step or an index column, it does.
Several keys, one step
Sort a first column, then sort a second one in the editor, and Power Query adds the second as a tie-breaker inside the same Table.Sort step:
let
Source = #table({"OrderID", "Customer", "Region", "Product", "Qty", "UnitPrice", "Status"}, {
{"A-101", "Ana Ruiz", "North", "Kite", 2, 12.5, "Complete"},
{"A-102", "Ben Okafor", "South", "Reel", 6, 4, "Cancelled"},
{"A-103", "Cleo Park", "North", "Line", 4, 3.5, "Complete"},
{"A-104", "Dev Shah", "West", "Kite", 1, 12.5, "Complete"}
}),
Sorted = Table.Sort(Source, {{"Region", Order.Ascending}, {"UnitPrice", Order.Descending}})
in
Table.SelectColumns(Sorted, {"OrderID", "Region", "UnitPrice"})Result
| OrderID | Region | UnitPrice |
|---|---|---|
| A-101 | North | 12.5 |
| A-103 | North | 3.5 |
| A-102 | South | 4 |
| A-104 | West | 12.5 |
Output produced by the Power Query Exercises simulator, an independent implementation of a documented subset of M.
The order of the keys matters: sorting by UnitPrice first and Region second gives a list ordered by price, with Region only breaking ties.
Why two separate sort steps go wrong
If you write one sort step for UnitPrice and a later, separate step for Region, the second step decides the order. Ties in Region keep their previous relative order only because the sort is stable, and that's easy to break by inserting a step in between. One step with both keys states the intent directly.
Text sorts by character
Text is compared character by character, so case and leading spaces affect the order. Uppercase letters sort before lowercase ones in an ordinal comparison:
Table.Sort(#table({"Name"}, {{"bea"}, {"Adam"}, {" Zoe"}, {"Carl"}}), {{"Name", Order.Ascending}})Result
| Name |
|---|
| Zoe |
| Adam |
| Carl |
| bea |
Output produced by the Power Query Exercises simulator, an independent implementation of a documented subset of M.
- Put the most important key first.
- Choose ascending or descending per key.
- Sort after filtering and calculating, so the sort sees final values.
- Add a unique key (such as OrderID) as the last sort key when the order must be fully deterministic.
Where to find this in Excel
- Excel for Windows
- Home > Sort Ascending or Sort Descending (or the column header menu)
- What you see
- Sorting a second column from its header menu adds it to the same Sorted Rows step, with a number showing its priority.
Paths are for the Power Query editor in Excel for Microsoft 365, checked against Microsoft's documentation on 8 November 2026: Sort columns (Microsoft Learn).
Common mistakes
- Sorting in two separate steps and relying on the earlier sort surviving.
- Sorting text that still has mixed case or leading spaces.
- Sorting early in the query and assuming later merges or groups keep that order; re-sort at the end if order matters.
Excel, Power BI and the simulator
- Excel and Power BI Desktop: the column filter arrow has Sort Ascending / Sort Descending; sorting a second column adds a tie-breaker to the same step.
- Power BI visuals and Excel PivotTables apply their own sort; sort in Power Query when the loaded table itself must be ordered (for example an Excel worksheet table).
- The simulator sorts text ordinally by default (compatibility page KD08). Culture-aware sorting with Comparer.FromCulture can differ from a native host in edge cases.
Next step
Apply this lesson in the simulator. The exercise is checked against fresh data, so the query has to work for rows it hasn't seen. It's free, with no account needed.
Practise: Sort orders by region, then by price (kite shop)Practice this
Each exercise runs in the browser and is checked against fresh data, not just the example.
- beginner~6 minFree
Sort orders by region, then by price (kite shop)
Produce a list sorted by Region A→Z and, within each region, by UnitPrice from highest to lowest.
- beginner~6 minPro
Sort orders by region, then by price (bakery)
Produce a list sorted by Region A→Z and, within each region, by UnitPrice from highest to lowest.
- beginner~6 minPro
Sort orders by region, then by price (bike rental)
Produce a list sorted by Region A→Z and, within each region, by UnitPrice from highest to lowest.
- intermediate~8 minFree
Sort tickets by business priority, not alphabetically (telecom)
Sort Urgent, High, Medium, Low (in that order), then oldest first within each priority.
- intermediate~8 minPro
Sort tickets by business priority, not alphabetically (saas)
Sort Urgent, High, Medium, Low (in that order), then oldest first within each priority.