Power QueryExercises

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 table

Parameters

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

RegionProductQty
NorthKite2
NorthReel1
SouthReel5
WestKite4

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

RegionRepAmount
NorthCy200
NorthAna120
SouthBo80
SouthAna50

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

Common mistakes

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

Learn more

Related functions

Official documentation: Table.Sort on Microsoft Learn.