Power Query M Language Guide
M is the language behind every Power Query step. You don't need to write M by hand for most work — the UI generates it — but knowing the basics unlocks edits the ribbon can't reach and custom columns beyond point-and-click.
What M is
M (formally, the Power Query Formula Language) is a functional, case-sensitive language purpose-built for data transformation. It's not SQL, not Python, not VBA — it's its own thing. Every function returns a value; nothing has side effects.
You'll see M in three places:
- Formula Bar — one line at a time, showing the current step
- Advanced Editor — the full script for the query (View → Advanced Editor)
- Custom Column — write your own M expression for a new column
The let-in structure
Every M query has this shape:
let introduces named steps. Each step's value can reference earlier steps. in declares which step is the query's output.
Four steps: read the CSV, promote first row to headers, filter to West region, sort by date descending. The in Sorted line says "the result of this query is the value of Sorted".
Case sensitivity
M is case-sensitive. Table.SelectRows works. table.selectrows fails. Column names in brackets like [Region] must match exactly.
Data types
| Type | Example |
|---|---|
| number | 42, 3.14 |
| text | "hello" (double quotes required) |
| logical | true, false (lowercase) |
| date | #date(2026, 9, 6) |
| time | #time(15, 30, 0) |
| datetime | #datetime(2026, 9, 6, 15, 30, 0) |
| list | {1, 2, 3} |
| record | [Name = "Alice", Age = 30] |
| table | Rows of records |
| null | null |
Common M functions
Table operations
Text operations
Date operations
Number operations
The each keyword
Many M functions take a function-argument. each is a shorthand.
each is more readable for simple conditions. The longer form is useful when you need multiple references to the row or complex logic.
Column references
Inside each, columns are referenced with [ColumnName]. Outside each, you need Record.Field(row, "ColumnName").
Common custom column patterns
Concatenation
Alternative: [FirstName] & " " & [LastName]
Conditional
Date extraction
Handling nulls
Or: Number.From([Amount]) ?? 0 (the ?? operator returns the right side if the left is null)
Nested field access from a related table
Access the first row of a nested table result and pull a column value.
Variables in Advanced Editor
Every step name is a variable. You can add intermediate variables that aren't steps:
Now threshold can be reused, and changing it in one place updates everywhere. This is a common way to parameterize queries.
Parameters
For values you want to change at runtime (file paths, filter dates), promote them to Query Parameters:
- Home → Manage Parameters → New Parameter
- Name, type, default value
- Reference in M as
parameterName
Reading the Advanced Editor for debugging
When a query errors, the error message usually names a step. Open Advanced Editor. Find that step's line. Common issues:
- Column not found — the source column was renamed or removed. Fix the reference.
- Type mismatch — a step expects a number but got text. Add a type conversion step earlier.
- File not found — the source path changed. Update the File.Contents argument.
Common pitfalls
M references columns by exact name. When source data changes a header, every downstream step referring to that name fails. Rename early in the query so downstream steps are stable.
The auto-detected types on import are guesses. Set types explicitly early — before any calculation that depends on them. A column that's silently text can't be summed.
The Advanced Editor doesn't enforce formatting, but readability matters when queries grow. Break long expressions across lines. Add blank lines between logical sections. Name steps descriptively.