Power Query M Language Guide (2026)
Home›Excel›Power Query›M Language
ExcelSub-Guide⏱ 12 min read

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 step1 = ... , step2 = ... , step3 = ... in step3

let introduces named steps. Each step's value can reference earlier steps. in declares which step is the query's output.

let Source = Csv.Document(File.Contents("C:\data\sales.csv")), Promoted = Table.PromoteHeaders(Source), Filtered = Table.SelectRows(Promoted, each [Region] = "West"), Sorted = Table.Sort(Filtered, {"Date", Order.Descending}) in Sorted

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

TypeExample
number42, 3.14
text"hello" (double quotes required)
logicaltrue, 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]
tableRows of records
nullnull

Common M functions

Table operations

Table.PromoteHeaders(t) — first row becomes column headers Table.SelectRows(t, condition) — filter rows Table.SelectColumns(t, names) — keep only certain columns Table.RemoveColumns(t, names) — drop columns Table.RenameColumns(t, pairs) — rename Table.Sort(t, sortSpec) — sort by columns Table.AddColumn(t, name, fn) — add computed column Table.Group(t, groupBy, aggregations) — group and aggregate

Text operations

Text.Upper("hello") → "HELLO" Text.Lower("HELLO") → "hello" Text.Length("abc") → 3 Text.Contains("abc", "b") → true Text.Trim(" hi ") → "hi" Text.Split("a,b,c", ",") → {"a", "b", "c"}

Date operations

Date.Year(#date(2026,9,6)) → 2026 Date.Month(#date(2026,9,6)) → 9 Date.DayOfWeek(#date(2026,9,6), Day.Monday) → 6 Date.From(#datetime(2026,9,6,15,0,0)) → date only Date.AddMonths(#date(2026,9,6), 3) → #date(2026,12,6)

Number operations

Number.Round(3.14159, 2) → 3.14 Number.Abs(-5) → 5 Number.From("42") → 42 (text to number)

The each keyword

Many M functions take a function-argument. each is a shorthand.

// These two lines mean the same thing: Table.SelectRows(t, each [Amount] > 100) Table.SelectRows(t, (row) => row[Amount] > 100)

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").

Table.SelectRows(t, each [Region] = "West" and [Amount] > 1000)

Common custom column patterns

Concatenation

Text.Combine({[FirstName], [LastName]}, " ")

Alternative: [FirstName] & " " & [LastName]

Conditional

if [Amount] > 1000 then "Large" else if [Amount] > 100 then "Medium" else "Small"

Date extraction

Date.Year([OrderDate]) Date.QuarterName(#datetime(2026,9,6,0,0,0)) → "Q3"

Handling nulls

if [Amount] = null then 0 else [Amount]

Or: Number.From([Amount]) ?? 0 (the ?? operator returns the right side if the left is null)

Nested field access from a related table

[MergedTable]{0}[Column]

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:

let Source = ... , threshold = 1000, LargeOrders = Table.SelectRows(Source, each [Amount] > threshold) in LargeOrders

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:

  1. Home → Manage Parameters → New Parameter
  2. Name, type, default value
  3. Reference in M as parameterName
let Source = Csv.Document(File.Contents(FilePath)), ... in ...

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

Column names change and everything breaks

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.

Types matter

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.

Format your M code

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.

Related guides