Table.Sort
TableSorts the rows of a table using one or more comparison criteria.
Syntax
Table.Sort(table as table, comparisonCriteria as any) as tableParameters
| Name | Type | Required | Description |
|---|---|---|---|
table | table | Yes | The input table to sort. |
comparisonCriteria | any | Yes | A column name, a {column, Order} pair, or a list of such pairs for multi-column sorting. |
Return Value
table — A table with the same columns and rows, sorted according to the specified criteria.
Remarks
Table.Sort reorders rows based on one or more columns. Use Order.Ascending or Order.Descending to control sort direction. When sorting by multiple columns, the first criterion is primary.
If only a column name is provided, the sort defaults to ascending order.
If you use Table.Sort before Table.RemoveDuplicates or Table.Group, wrap the sorted table in Table.Buffer first. Without buffering, the sort order is not guaranteed to be preserved by downstream operations, which can lead to unexpected results (e.g., keeping the wrong duplicate row).
Examples
Example 1: Sort ascending by one column
Table.Sort(Sales, {"UnitPrice", Order.Ascending})Result
OrderID | CustomerName | Product | Category | UnitPrice | Quantity | OrderDate | Region | |
|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Charlie | Widget C | Widgets | 15 | 10 | 2/1/2024 | East |
| 2 | 8 | Diana | Widget C | Widgets | 15 | 8 | 4/15/2024 | North |
| 3 | 1 | Alice | Widget A | Widgets | 25 | 4 | 1/15/2024 | East |
| 4 | 5 | Diana | Widget A | Widgets | 25 | 6 | 3/5/2024 | West |
| 5 | 2 | Bob | Gadget B | Gadgets | 50 | 2 | 1/18/2024 | West |
| 6 | 7 | Charlie | Gadget B | Gadgets | 50 | 3 | 4/1/2024 | West |
| 7 | 4 | Alice | Gadget D | Gadgets | 75 | 1 | 2/10/2024 | North |
| 8 | 6 | Bob | Thingamajig E | Misc | 120 | 1 | 3/12/2024 | East |
Example 2: Sort descending
Table.Sort(Sales, {"Quantity", Order.Descending})Result
OrderID | CustomerName | Product | Category | UnitPrice | Quantity | OrderDate | Region | |
|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Charlie | Widget C | Widgets | 15 | 10 | 2/1/2024 | East |
| 2 | 8 | Diana | Widget C | Widgets | 15 | 8 | 4/15/2024 | North |
| 3 | 5 | Diana | Widget A | Widgets | 25 | 6 | 3/5/2024 | West |
| 4 | 1 | Alice | Widget A | Widgets | 25 | 4 | 1/15/2024 | East |
| 5 | 7 | Charlie | Gadget B | Gadgets | 50 | 3 | 4/1/2024 | West |
| 6 | 2 | Bob | Gadget B | Gadgets | 50 | 2 | 1/18/2024 | West |
| 7 | 4 | Alice | Gadget D | Gadgets | 75 | 1 | 2/10/2024 | North |
| 8 | 6 | Bob | Thingamajig E | Misc | 120 | 1 | 3/12/2024 | East |
Compatibility
✓ Power BI Desktop✓ Power BI Service✓ Excel Desktop✓ Excel Online✓ Dataflows✓ Fabric Notebooks