OpenBI Docs Buy

Table.Sort

Puts the rows in order by one or more columns, each ascending or descending.

Group: Table. Power Query M has it too: a query that uses it is written as in Power BI.

Syntax

Table.Sort(table as table, comparisonCriteria as any) as table
ParameterTypeDescription
tabletableThe table to order.
comparisonCriteriaanyA column name; a pair {name, Order.Descending}; a list of names or pairs, the first deciding first; or a function of a row that gives the value to order by.

Returns

table. The same rows in the new order.

Remarks

  • Ascending is the default. Nulls come first going up and last going down.
  • Text is ordered by character code: capitals come before small letters, so "Desk" sorts before "apple".

Examples

By shop, then the best sellers first

let
    Sales = #table({"Shop", "Product", "Sales"}, {{"Oslo", "Desk", 120}, {"Bergen", "Chair", 50}, {"Oslo", "Chair", 80}})
in
    Table.Sort(Sales, {{"Shop", Order.Ascending}, {"Sales", Order.Descending}})
ShopProductSales
BergenChair50
OsloDesk120
OsloChair80

Where the nulls go

Table.Sort(#table({"Product", "Price"}, {{"Chair", 50}, {"Desk", null}, {"Lamp", 20}}), "Price")
ProductPrice
Desknull
Lamp20
Chair50

By a value worked out per row

Table.Sort(#table({"Product"}, {{"Bookcase"}, {"Desk"}, {"Lamp shade"}}), each Text.Length([Product]))
Product
Desk
Bookcase
Lamp shade

Shortest name first.

Table.ReverseRows, Table.Max, Table.AddRankColumn, List.Sort.

Last updated · OpenBI Studio 0.3.79