Power Query M Cheat Sheet
30 essentials with syntax and working examples. Search or filter by group.
let … in
Every query is a let block: each line is a named step that can use earlier steps, and in says which step is the result.
let
Step1 = expression,
Step2 = expression
in
Step2let
Source = Csv.Document(File.Contents("C:\Data\sales_flat.csv"), [Delimiter = ",", Encoding = 65001]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true])
in
Promotedeach / _
each creates a one-argument function; _ is the current item. Inside a row, [column] is short for _[column].
each expression // same as (_) => expressionList.Transform({1, 2, 3}, each _ * 10) // {10, 20, 30}if … then … else
Conditional expression. Lowercase keywords; chain with else if. There is no end keyword.
if condition then value1 else value2Table.AddColumn(Source, "size", each if [net_sales] >= 50 then "High" else if [net_sales] >= 20 then "Medium" else "Low", type text)try … otherwise
Evaluates an expression and returns the fallback if it raises an error, like IFERROR in Excel.
try expression otherwise fallbackTable.AddColumn(Source, "qty", each try Number.From([quantity]) otherwise null, type nullable number)Table.SelectRows
Keeps only the rows where the condition is true (the Filter Rows step).
Table.SelectRows(table as table, condition as function) as tableTable.SelectRows(Source, each [region] = "South" and [net_sales] > 20)Table.AddColumn
Adds a calculated column. Give the type as the fourth argument so it is not Any.
Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as tableTable.AddColumn(Source, "gross_margin", each [net_sales] - [cost], type number)Table.TransformColumnTypes
Sets data types for columns. The optional culture controls how text dates and numbers are read (e.g. "en-US" for MM/DD/YYYY).
Table.TransformColumnTypes(table as table, typeTransformations as list, optional culture as nullable text) as tableTable.TransformColumnTypes(Source, {{"order_date", type date}, {"quantity", Int64.Type}, {"net_sales", type number}}, "en-US")Table.RenameColumns
Renames columns using a list of {old, new} pairs.
Table.RenameColumns(table as table, renames as list, optional missingField as nullable number) as tableTable.RenameColumns(Source, {{"net_sales", "Net Sales"}, {"store_name", "Store"}})Table.RemoveColumns
Deletes the listed columns.
Table.RemoveColumns(table as table, columns as any, optional missingField as nullable number) as tableTable.RemoveColumns(Source, {"cost", "segment"})Table.SelectColumns
Keeps only the listed columns, in that order. Safer than removing when new columns may appear.
Table.SelectColumns(table as table, columns as any, optional missingField as nullable number) as tableTable.SelectColumns(Source, {"order_date", "store_name", "net_sales"})Table.TransformColumns
Applies a function to the values of existing columns without adding new ones.
Table.TransformColumns(table as table, transformOperations as list, optional defaultTransformation as nullable function, optional missingField as nullable number) as tableTable.TransformColumns(Source, {{"store_name", each Text.Proper(Text.Trim(_)), type text}, {"category", Text.Proper, type text}})Table.ReplaceValue
Replaces values in columns. Replacer.ReplaceText swaps part of a text; Replacer.ReplaceValue swaps the whole value.
Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as tableTable.ReplaceValue(Source, "$", "", Replacer.ReplaceText, {"net_sales"})Table.Group
Groups rows by key columns and aggregates each group (the Group By step).
Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as nullable function) as tableTable.Group(Source, {"region"}, {{"Total Sales", each List.Sum([net_sales]), type number}, {"Rows", each Table.RowCount(_), Int64.Type}})Table.Sort
Sorts rows by one or more columns.
Table.Sort(table as table, comparisonCriteria as any) as tableTable.Sort(Source, {{"region", Order.Ascending}, {"net_sales", Order.Descending}})Table.Distinct
Removes duplicate rows, comparing all columns or only the ones you list.
Table.Distinct(table as table, optional equationCriteria as any) as tableTable.Distinct(Source)Table.NestedJoin
Merges two queries on key columns; the matches arrive as a nested table column (Merge Queries).
Table.NestedJoin(table1 as table, key1 as any, table2 as any, key2 as any, newColumnName as text, optional joinKind as nullable number, optional keyEqualityComparers as nullable list) as tableTable.NestedJoin(Orders, {"store_id"}, Stores, {"store_id"}, "Stores", JoinKind.LeftOuter)Table.ExpandTableColumn
Expands a nested table column (usually after a merge) into regular columns.
Table.ExpandTableColumn(table as table, column as text, columnNames as list, optional newColumnNames as nullable list) as tableTable.ExpandTableColumn(Merged, "Stores", {"region", "store_type"})Table.Combine
Appends tables one below another (Append Queries). Columns are matched by name.
Table.Combine(tables as list, optional columns as any) as tableTable.Combine({Sales2024, Sales2025})Table.UnpivotOtherColumns
Turns many columns into attribute/value rows, keeping the listed columns fixed. Ideal for month-per-column reports.
Table.UnpivotOtherColumns(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as tableTable.UnpivotOtherColumns(Source, {"store_name"}, "Month", "Sales")Table.Pivot
Turns the values of one column into new columns, aggregating another column.
Table.Pivot(table as table, pivotValues as list, attributeColumn as text, valueColumn as text, optional aggregationFunction as nullable function) as tableTable.Pivot(Source, List.Distinct(Source[channel]), "channel", "net_sales", List.Sum)Table.FillDown
Copies the last non-null value down into null cells below it.
Table.FillDown(table as table, columns as list) as tableTable.FillDown(Source, {"region"})Table.PromoteHeaders
Uses the first row as column names.
Table.PromoteHeaders(table as table, optional options as nullable record) as tableTable.PromoteHeaders(Source, [PromoteAllScalars = true])Text.Trim
Removes leading and trailing spaces. Unlike Excel TRIM, it does not collapse double spaces in the middle.
Text.Trim(text as nullable text, optional trim as any) as nullable textText.Trim(" Sunshine Miami ") // "Sunshine Miami"Text.Proper
Capitalizes the first letter of each word. Text.Upper and Text.Lower change the whole text.
Text.Proper(text as nullable text, optional culture as nullable text) as nullable textText.Proper("SUNSHINE MIAMI") // "Sunshine Miami"Text.Replace
Replaces every occurrence of a substring.
Text.Replace(text as nullable text, old as text, new as text) as nullable textText.Replace("$12.50", "$", "") // "12.50"Text.BeforeDelimiter
Returns the text before a delimiter. Text.AfterDelimiter returns the part after it.
Text.BeforeDelimiter(text as nullable text, delimiter as text, optional index as any) as anyText.BeforeDelimiter("3 units", " ") // "3"Date.Year
Returns the year of a date. Date.Month and Date.Day return the month and day.
Date.Year(dateTime as any) as nullable numberDate.Year(#date(2025, 3, 14)) // 2025Date.StartOfMonth
Returns the first day of the month, useful for monthly grouping. Date.EndOfMonth returns the last day.
Date.StartOfMonth(dateTime as any) as anyDate.StartOfMonth(#date(2025, 3, 14)) // #date(2025, 3, 1)List.Sum
Adds the numbers in a list. Table[column] gives a column as a list.
List.Sum(list as list, optional precision as nullable number) as anyList.Sum(Source[net_sales])Number.Round
Rounds to the given decimals. The default mode rounds halves to even; use RoundingMode.AwayFromZero to match Excel.
Number.Round(number as nullable number, optional digits as nullable number, optional roundingMode as nullable number) as nullable numberNumber.Round(2.5, 0, RoundingMode.AwayFromZero) // 3 (default mode gives 2)