Learn  /  Cheat sheets

Power Query M cheat sheet

The Power Query M steps behind almost every Power BI and Excel data import, on one printable page.

Cheat sheet5 min readUpdated 18 October 2026
Power BIExcelData analysis
All cheat sheets and guides
All guides and cheat sheets

Anatomy of a query

Power Query M
let
    Source = Csv.Document(File.Contents("C:\data\sales.csv"),
        [Delimiter = ",", Encoding = 65001]),
    Promoted = Table.PromoteHeaders(Source,
        [PromoteAllScalars = true]),
    Typed = Table.TransformColumnTypes(Promoted,
        {{"Date", type date}, {"Amount", type number}})
in
    Typed

Filter and sort rows

Power Query M
Table.SelectRows(Typed, each [Amount] > 100)
Table.SelectRows(Typed, each [Region] = "North"
    and [Date] >= #date(2026, 1, 1))
Table.Distinct(Typed, {"OrderID"})
Table.Sort(Typed, {{"Amount", Order.Descending}})
Table.FirstN(Sorted, 10)

Columns

Power Query M
Table.SelectColumns(Typed, {"Date", "Region", "Amount"})
Table.RemoveColumns(Typed, {"Notes"})
Table.RenameColumns(Typed, {{"Amt", "Amount"}})
Table.AddColumn(Typed, "Net",
    each [Amount] - [Discount], type number)

Clean text

Power Query M
Table.TransformColumns(Typed, {
    {"Customer", Text.Trim, type text},
    {"Region", Text.Proper, type text}})
Table.ReplaceValue(Typed, "N/A", null,
    Replacer.ReplaceValue, {"Region"})
Table.AddColumn(Typed, "Domain",
    each Text.AfterDelimiter([Email], "@"), type text)

Group by

Power Query M
Table.Group(Typed, {"Region"}, {
    {"Total", each List.Sum([Amount]), type number},
    {"Orders", each Table.RowCount(_), Int64.Type}})

Merge (join) and append

Power Query M
Merged = Table.NestedJoin(Sales, {"ProductID"},
    Products, {"ProductID"}, "Product", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Product",
    {"Category", "Price"}),
AllYears = Table.Combine({Sales2025, Sales2026})

Unpivot and pivot

Power Query M
// Month columns into rows: Region | Month | Amount
Table.UnpivotOtherColumns(Source, {"Region"},
    "Month", "Amount")

// And back again
Table.Pivot(Long, List.Distinct(Long[Month]),
    "Month", "Amount", List.Sum)

Conditional columns

Power Query M
Table.AddColumn(Typed, "Band", each
    if [Amount] >= 1000 then "Large"
    else if [Amount] >= 100 then "Medium"
    else "Small", type text)

Errors and nulls

Power Query M
Table.AddColumn(Typed, "Unit price",
    each try [Amount] / [Qty] otherwise null)
Table.RemoveRowsWithErrors(Typed, {"Amount"})
Table.ReplaceErrorValues(Typed, {{"Amount", 0}})
Table.SelectRows(Typed, each [Region] <> null)

Dates

Power Query M
Date.Year([Date])
Date.StartOfMonth([Date])
Date.EndOfMonth([Date])
Date.AddMonths([Date], 1)
Date.DayOfWeekName([Date])

A date table

Power Query M
let
    Start = #date(2026, 1, 1),
    Dates = List.Dates(Start, 365, #duration(1, 0, 0, 0)),
    Calendar = Table.FromList(Dates,
        Splitter.SplitByNothing(), {"Date"}),
    Typed = Table.TransformColumnTypes(Calendar,
        {{"Date", type date}})
in
    Typed

Good habits

On database sources, filter rows and remove columns early so the work folds back to the database (right-click a step and check View Native Query). Set data types explicitly, give steps clear names, and reference a staging query instead of duplicating it.

Alessandro Ecclesie AgazziWritten by Alessandro Ecclesie Agazzi, analytics engineer in London. Updated 18 October 2026. About me

Found this useful?

New guides and "what's new" explainers on data and AI land here regularly. Follow along on LinkedIn, or share this one with a colleague.