Table.Sort

Table

Sorts the rows of a table using one or more comparison criteria.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Table.Sort(table as table, comparisonCriteria as any) as table

Parameters

NameTypeRequiredDescription
tabletableYesThe input table to sort.
comparisonCriteriaanyYesA column name, a {column, Order} pair, or a list of such pairs for multi-column sorting.

Return Value

tableA 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
13CharlieWidget CWidgets15102/1/2024East
28DianaWidget CWidgets1584/15/2024North
31AliceWidget AWidgets2541/15/2024East
45DianaWidget AWidgets2563/5/2024West
52BobGadget BGadgets5021/18/2024West
67CharlieGadget BGadgets5034/1/2024West
74AliceGadget DGadgets7512/10/2024North
86BobThingamajig EMisc12013/12/2024East

Example 2: Sort descending

Table.Sort(Sales, {"Quantity", Order.Descending})
Result
OrderID
CustomerName
Product
Category
UnitPrice
Quantity
OrderDate
Region
13CharlieWidget CWidgets15102/1/2024East
28DianaWidget CWidgets1584/15/2024North
35DianaWidget AWidgets2563/5/2024West
41AliceWidget AWidgets2541/15/2024East
57CharlieGadget BGadgets5034/1/2024West
62BobGadget BGadgets5021/18/2024West
74AliceGadget DGadgets7512/10/2024North
86BobThingamajig EMisc12013/12/2024East

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks