Custom Sort Order Column (e.g., High/Medium/Low) in Power Query M

Adds a numeric sort-order column for a text category so it sorts by business meaning instead of alphabetically.

Column OperationsBeginnersort ordercustom sortcategory

Power Query M code

let
    Source = YourTable,
    SortOrder = [High = 1, Medium = 2, Low = 3],
    AddedSortOrder = Table.AddColumn(Source, "SortOrder", each Record.Field(SortOrder, [Priority]), Int64.Type)
in
    AddedSortOrder

Notes

Set SortOrder as the 'Sort by column' for Priority in the Power BI model.

Requirements

How to use this snippet

  1. In Power BI Desktop, choose Transform data to open Power Query, then open the Advanced Editor on a query (or start from a blank query).
  2. Paste the snippet and replace the placeholder names (tables, columns, file paths) with your own. Check the requirements above first.
  3. Run it through the Power Query M Formatter to tidy the layout.

More Power Query M resources

Browse all 145 snippets in the Power Query M Code Snippets, or search the library for something similar to Custom Sort Order Column (e.g., High/Medium/Low).