Table.Sort
Table function · By Tahir Nas · Examples run in our simulator · Published · Updated
Sorts rows by one or more columns.
When to use it
Use it before any step that depends on order: taking the top N, keeping the first row per key, adding an index or filling down. Sorting purely for display is better left to the report.
Syntax
Table.Sort(table as table, comparisonCriteria as any) as tableParameters
- comparisonCriteria
- A column name, or a list of {column, Order.Ascending | Order.Descending} pairs.
Returns
The sorted table.
Example
Table.Sort(#table({"Region", "Product", "Qty"}, {{"North", "Kite", 2}, {"South", "Reel", 5}, {"North", "Reel", 1}, {"West", "Kite", 4}}), {{"Region", Order.Ascending}, {"Qty", Order.Descending}})Result
| Region | Product | Qty |
|---|---|---|
| North | Kite | 2 |
| North | Reel | 1 |
| South | Reel | 5 |
| West | Kite | 4 |
Example: highest amount first, then by name
The second key only decides ties in the first.
Table.Sort(#table({"Region", "Rep", "Amount"}, {{"North", "Ana", 120}, {"South", "Bo", 80}, {"North", "Cy", 200}, {"South", "Ana", 50}}), {{"Amount", Order.Descending}, {"Rep", Order.Ascending}})Result
| Region | Rep | Amount |
|---|---|---|
| North | Cy | 200 |
| North | Ana | 120 |
| South | Bo | 80 |
| South | Ana | 50 |
Both results are produced by the Power Query Exercises simulator, an independent implementation of a documented subset of M, and checked on every build.
Good to know
- Later keys only break ties in earlier keys.
Common mistakes
- Assuming the source order is stable: it isn't guaranteed, so sort explicitly before order-dependent steps.
- Sorting text numbers, where "10" sorts before "9".
The matching command in Excel
- Excel for Windows
- Home > Sort Ascending or Sort Descending
- What you see
- Sorting a second column from its header menu adds it to the same Sorted Rows step.
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).
Exercises that use Table.Sort
- 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.
- intermediate~8 minFree
Find the three largest orders (kite shop)
Calculate each order's value and keep the three biggest, largest first.
- beginner~6 minFree
Remove a leftover step that cuts the data short (kite shop)
A Kept First Rows step left over from testing silently drops orders. Find it and remove it.
- 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.
- beginner~5 minPro
Number claims in date order (consultancy)
Sort claims by date and add a Line number starting at 1.
- intermediate~8 minPro
Summarise orders by region (kite shop)
Group by Region with order count, total quantity and average unit price, sorted by region.
Learn more
- Sort rows on more than one column lesson
- Top N per group in Power Query, with ties handled guide
- How to open the Power Query editor in Excel guide
Related functions
Official documentation: Table.Sort on Microsoft Learn.