Introduction
A monthly sales export arrives with inconsistent customer names, dates stored as text and an amount column containing both numbers and “N/A”. Repeating the same manual corrections makes reporting difficult to maintain.
Power Query lets you define those preparation steps once and apply them again when the source refreshes. This Power Query M cheat sheet connects useful functions to business tasks, then brings them together in a worked example.
The goal is a repeatable process whose output you can explain and check.
What Is Power Query M, and Where Do You Use It?
M is the formula language behind Power Query transformations in Excel and Power BI. The editor generates M as you apply steps; Advanced Editor lets you inspect or change the complete query.
In an Import model, Power Query prepares data before loading. DAX handles model calculations, where concepts such as DAX filter context determine which records contribute to a measure.
An M query commonly follows this structure:
let
Source = #table({"Amount"}, {{100}, {200}}),
Total = List.Sum(Source[Amount])
in
Total
This returns 300. let defines named expressions, and in chooses the result. Separate steps with commas, but do not put a comma before in.
M is case-sensitive. Names containing spaces use syntax such as #"Changed Type". The keyword each defines a single-argument function: inside a row operation, [Amount] accesses that row's field; inside a column transformation, _ represents the current value.
Which Power Query M Functions Should You Keep Handy?
These Power Query M functions cover common preparation tasks. Choose the operation according to what should happen to the data.
| Task | Function or syntax | Practical use |
| Select fields | Table.SelectColumns | Keep named columns in the required order. |
| Filter records | Table.SelectRows | Keep rows satisfying a condition. |
| Clean text | Table.TransformColumns Text.Trim Text.Clean Text.Upper | Transform values, remove surrounding whitespace and control characters, and standardise case. |
| Set data types | Table.TransformColumnTypes | Convert columns, specifying culture where interpretation depends on locale. |
| Parse amounts | Number.FromText try/otherwise | Convert numeric text and handle conversion errors deliberately. |
| Add a field | Table.AddColumn | Create calculations or quality flags using each. |
| Remove duplicates | Table.Distinct | Remove records matching the chosen comparison columns. |
| Summarise | Table.Group List.Sum | Aggregate valid amounts by customer or another grouping field. |
| Append exports | Table.Combine | Stack tables, aligning columns by name. |
| Merge a lookup | Table.NestedJoin Table.ExpandTableColumn | Match keys, then expand the required lookup fields. |
| Reshape columns | Table.UnpivotOtherColumns | Turn measure columns into attribute-value rows. |
| Count records | Table.RowCount | Compare record counts before and after transformations. |
try/otherwise is language syntax, not a function. Keep identifiers such as account codes as text when leading zeros matter.
How Do You Clean a Sales Export Step by Step?
Consider an orders export intended to contain one row per order. Order 1003 appears twice because the same record was imported twice. Its duplicate is confirmed before removal.
Create a blank query in Power Query Editor, open Advanced Editor and replace its contents with this code. Name the query CleanOrders.
let
Source = #table(
type table [
OrderID = text, Customer = nullable text,
OrderDate = text, AmountRaw = nullable text
],
{
{"1001", " acme ", "31/01/2026", "1,200.00"},
{"1002", "ACME", "01/02/2026", "300.00"},
{"1003", " beta ", "02/02/2026", "250.00"},
{"1003", " beta ", "02/02/2026", "250.00"},
{"1004", "gamma", "03/02/2026", "N/A"},
{"1005", "Beta ", "04/02/2026", null}
}
),
UniqueRows = Table.Distinct(Source),
Names = Table.TransformColumns(
UniqueRows,
{{"Customer", each
if _ = null then null
else Text.Upper(Text.Trim(Text.Clean(_))),
type nullable text}}
),
Dates = Table.TransformColumnTypes(
Names, {{"OrderDate", type date}}, "en-GB"
),
Amounts = Table.AddColumn(
Dates, "Amount", each
try Number.FromText([AmountRaw], "en-GB")
otherwise null,
type nullable number
),
Flagged = Table.AddColumn(
Amounts, "AmountStatus", each
if [AmountRaw] = null then "Missing"
else if Text.Trim([AmountRaw]) = "" then "Missing"
else if [Amount] = null then "Invalid"
else "Valid",
type text
)
in
Flagged
Standardise Text and Convert Dates Deliberately
The name-cleaning step turns both “ acme ” and “ACME” into “ACME”. Text.Trim removes surrounding whitespace, but does not collapse repeated spaces inside a name. Text.Clean removes control characters; it does not correct spelling.
The date conversion uses "en-GB" to interpret day/month/year text. Number parsing uses the same culture for these comma-separated thousands and decimal points. Match the culture to your source format, not simply your computer settings. Invalid dates remain errors requiring review.
For a real export, replace the embedded Source with your imported table and match the column names and source formats.
Keep Missing Values and Conversion Errors Visible
The original AmountRaw remains available. Failed numeric conversions become null in Amount, while AmountStatus distinguishes missing input from invalid text.
The output contains five orders: three valid amounts, one invalid amount and one missing amount. The valid amounts sum to 1,750. That is not a complete total for all five orders.
This distinction matters in Power Query data cleaning. Replacing every exception with zero can make an incomplete report appear complete.
How Do You Combine and Reshape Tables Safely?
Appending adds rows. For existing queries named January and February, use Table.Combine({January, February}). Standardise column names and types first; fields absent from one input can introduce nulls.
Merging adds related information. Suppose a separate Customers query contains standardised Customer keys and a Segment column, with one row per customer. Use this as a new query:
let
Merged = Table.NestedJoin(
CleanOrders, {"Customer"},
Customers, {"Customer"},
"Lookup", JoinKind.LeftOuter
),
Expanded = Table.ExpandTableColumn(
Merged, "Lookup", {"Segment"}, {"Segment"}
)
in
Expanded
Check that lookup keys are unique before expanding. Two matching lookup rows can duplicate an order and inflate summed amounts. The same duplicate-key check applies when merging data in Python.
Unpivoting changes the table's shape. For an existing Monthly query containing only Customer, Jan and Feb, use:
Table.UnpivotOtherColumns(
Monthly, {"Customer"}, "Month", "Amount"
)
A row containing ACME, 1,200 and 300 becomes two rows: ACME/Jan/1,200 and ACME/Feb/300. Review the selected columns when the source changes, so new descriptive fields are not accidentally unpivoted as amounts.
What Should You Check Before Loading the Result?
Power Query data transformation is only useful if the result still represents the intended business records.
- Record counts: Six source rows become five after the confirmed duplicate is removed.
- Business keys: Repeated OrderIDs may be legitimate in a table containing order lines.
- Exceptions: Keep the invalid and missing amounts visible until resolved.
- Dates and matches: Check the date range, conversion errors and unmatched lookup keys.
- Totals: Reconcile valid amounts before and after merging or reshaping.
Do not assume Table.Distinct preserves the first or latest record when rows share a key but differ elsewhere. This example removes identical rows across all columns.
Power Query Practice Exercises With Expected Results
Use CleanOrders for these exercises.
- Filter valid February orders. Use Table.SelectRows with AmountStatus = "Valid" and dates from 1 February 2026 up to, but excluding, 1 March. Expected result: two orders totalling 550.
- Summarise valid amounts by customer. Filter valid records, then use Table.Group with List.Sum([Amount]). Expected result: ACME 1,500; BETA 250.
- Correct an invalid input. Replace “N/A” with “150” in the source and refresh. Expected result: four valid orders totalling 1,900, with one missing amount still requiring review.
Explain each change in row count or total. A query that refreshes successfully can still apply the wrong business rule.
Put Your Data Preparation Skills Into Practice
CompeteX is PangaeaX's data competition platform. Its data analytics challenges provide opportunities to apply preparation, validation and analytical reasoning to business problems.
Check each challenge's required tools and submission format before choosing it. Use these exercises as a starting point, then practise explaining which records you changed, which exceptions remain and why the resulting data is suitable for analysis.

