Power Query M Language for Beginners: A Practical Starter Guide

By Vivek Dhumal • 2026-09-22 • 11 min read
Power Query M Language for Beginners: A Practical Starter Guide

Key Takeaways

  • M (the Power Query formula language) is the code behind every step you apply in Power Query's point-and-click interface. When you click "Remove Columns" or "Filter Rows," Power Query writes M in the background. Learning M means learning to read, understand, and occasionally edit that code — not starting from scratch.
  • Every M query follows the same structure: a let block that defines steps, and an in statement that says which step to return. Once this pattern makes sense, the rest of M becomes much more readable.
  • M is case-sensitive. Table.SelectRows works. table.selectrows throws an error. This is the single most common source of beginner mistakes when typing M directly.
  • The each keyword appears constantly in M code and confuses almost every beginner. It means "for each row, apply this expression" — and understanding it unlocks most of the filter and transformation functions you'll use daily.
  • You don't need to write M from scratch to benefit from knowing it. The three most practical beginner skills are: reading M code you didn't write, editing an existing step in the Advanced Editor, and writing a custom column formula.

If you've looked at Power Query's Advanced Editor and felt the code was completely impenetrable — lines of function calls, strange-looking symbols, and that let/in structure that doesn't look like anything else — this guide is for you. M looks more intimidating than it is, and most of what you need to know as a beginner fits into a handful of patterns that repeat constantly.

What M Actually Is

M is the formula language that runs underneath Power Query. Every data transformation you apply through Power Query's interface — filtering rows, removing columns, merging tables, splitting text — is secretly stored as M code. The point-and-click experience is generating that code automatically on your behalf.

This means you've been using M every time you've transformed data in Power Query, even if you've never opened the Advanced Editor. The question isn't whether to use M — it's whether to understand what it's doing.

Knowing M becomes practically useful in three specific situations. First, when you need to edit a transformation that the UI generated but got slightly wrong. Second, when you want to do something the UI doesn't have a button for — like applying a conditional transformation based on two columns simultaneously. Third, when you inherit someone else's query and need to understand what it does.

None of these require you to become a programmer. They require you to be able to read a few patterns and know what the most common functions do.

The Sample Data Used in Every Example

Every code example in this guide transforms the same Orders table. It has six rows covering a small set of customer orders across three months.

OrderID Customer Product Quantity UnitPrice OrderDate Region Status Revenue
1Acme CorpLaptop285,00015-Jan-2026NorthDelivered170,000
2Beta LtdMouse51,20022-Jan-2026SouthPending6,000
3Acme CorpMonitor145,00003-Feb-2026NorthDelivered45,000
4Gamma IncLaptop385,00017-Feb-2026EastDelivered255,000
5Beta LtdKeyboard43,50005-Mar-2026South(blank)14,000
6Delta CoMouse21,20018-Mar-2026EastPending2,400

Note: Order 5 has a blank Status — that's there on purpose for the null-handling examples later.

Part 1: The let...in Structure

Every M query is built around this pattern:

let
    StepOne = some_expression,
    StepTwo = another_expression(StepOne),
    StepThree = yet_another_expression(StepTwo)
in
    StepThree

The let block contains a sequence of named steps. Each step assigns a name to an expression — a transformation, a function call, or a value. Steps are separated by commas.

The in statement specifies which step's result to return as the query's output. In practice, this is almost always the last step.

Each step can reference any step defined above it in the let block — that's how the pipeline works. StepTwo can use StepOne's result. StepThree can use StepTwo's result. The order matters only in the sense that you must define a step before referencing it.

Here's what a real M query looks like when you load the Orders table from Excel and apply two transformations:

let
    Source = Excel.Workbook(
        File.Contents("C:\Data\Orders.xlsx"),
        null,
        true
    ),
    Orders_Sheet = Source{[Item="Orders", Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(
        Orders_Sheet,
        [PromoteAllScalars=true]
    ),
    #"Changed Type" = Table.TransformColumnTypes(
        #"Promoted Headers",
        {{"OrderID", Int64.Type}, {"Revenue", Int64.Type}}
    )
in
    #"Changed Type"

Reading this step by step:

  • Source — connects to the Excel file and reads its contents
  • Orders_Sheet — navigates inside that workbook to the specific sheet named "Orders"
  • #"Promoted Headers" — takes the first row of raw data and turns it into column headers
  • #"Changed Type" — sets specific columns to the correct data types
  • in #"Changed Type" — returns the result of the last step as the query output

The #"..." notation (hash symbol followed by text in quotes) is how M names a step that contains spaces. Power Query generates this automatically when you apply steps through the UI. You can use normal names without spaces too — Source, FilteredRows, CleanedData — and those don't need the hash-quote notation.

Part 2: Case Sensitivity — The Rule That Catches Everyone

Before going further, this rule is important enough to repeat: M is case-sensitive in two specific ways.

Function names must be written exactly as specified. Table.SelectRows is a valid function. table.selectrows and Table.Selectrows are not — they'll throw an error immediately.

Column names must match the actual column header exactly. If your column is called OrderID, referencing [orderid] or [Orderid] in an expression will produce an error or a null result.

This trips up almost everyone who first tries writing M manually after coming from Excel, where formulas are completely case-insensitive. The simplest way to avoid case errors when writing M: click on an existing step in the Applied Steps panel, look at how the formula bar spells the function names and column names, and copy that spelling exactly.

Part 3: M's Data Types

M treats every value as having a type. The types you'll encounter most often:

Primitive types hold a single value:

  • type text — a text string: "Acme Corp", "North"
  • type number — any number, whole or decimal: 85000, 1.5
  • Int64.Type — whole numbers specifically (used often when setting column types)
  • type logical — true or false: true, false
  • type date — a date: #date(2026, 1, 15) (note the hash prefix for literal date values)
  • type datetime — date and time combined: #datetime(2026, 1, 15, 9, 30, 0)
  • type null — represents a blank or missing value

Structured types hold multiple values:

  • List — an ordered collection of values, written with curly braces: {1, 2, 3} or {"North", "South", "East"}
  • Record — a collection of named values, written with square brackets: [Name="Acme Corp", Region="North"]
  • Table — the main data structure you work with in Power Query: rows and columns of typed values

Understanding why these types exist becomes practical once you start writing custom column expressions. When you write Table.AddColumn(Source, "Tax", each [Revenue] * 0.18, type number), that last argument type number tells Power Query what type the new column holds. Getting it right prevents type mismatch errors downstream.

Part 4: The Most Important Keyword — each

each appears in M code constantly, and it's the piece that confuses beginners the most because it looks like nothing else in the language.

each means "for each row in the table, evaluate this expression using that row's values."

When you write a filter in Power Query's UI, the generated M looks like this:

= Table.SelectRows(Source, each [Revenue] > 100000)

The each [Revenue] > 100000 part means: go through every row in Source, look at that row's Revenue value, and keep the row if that value is greater than 100,000. The expression [Revenue] > 100000 is evaluated once per row, using each row's own data.

Without each, [Revenue] > 100000 has no row context — M doesn't know which row's Revenue you're comparing. each provides that row-by-row evaluation context.

Here's how it appears in the most common M functions:

Table.SelectRows (filter rows):

#"Filtered Rows" = Table.SelectRows(
    Source,
    each [Revenue] > 100000
)

Running this against the Orders table returns two rows: Order 1 (Revenue 170,000) and Order 4 (Revenue 255,000).

Table.AddColumn (add a custom column):

#"Added Tax" = Table.AddColumn(
    Source,
    "Tax",
    each [Revenue] * 0.18,
    type number
)

For each row, this calculates that row's Revenue multiplied by 0.18 and puts the result in a new Tax column. Order 1 gets Tax = 30,600. Order 4 gets Tax = 45,900. And so on across all six rows.

Using each with a condition:

#"Added Status Flag" = Table.AddColumn(
    Source,
    "Priority",
    each if [Revenue] > 50000 then "High" else "Standard",
    type text
)

Part 5: The Functions You'll See Most Often

Table Functions

These are the workhorses. Almost every step Power Query generates is one of these.

  • Table.SelectRows — filters a table to rows where a condition is true:
    #"High Value Orders" = Table.SelectRows(Source, each [Revenue] > 100000)
    // Result: 2 rows (Orders 1 and 4)
  • Table.AddColumn — adds a new column with values calculated per row:
    #"With Tax" = Table.AddColumn(Source, "Tax", each [Revenue] * 0.18, type number)
  • Table.RemoveColumns — removes one or more columns by name:
    #"Removed Cols" = Table.RemoveColumns(Source, {"UnitPrice", "Status"})
  • Table.RenameColumns — renames columns using a list of from-to pairs:
    #"Renamed" = Table.RenameColumns(Source, {{"UnitPrice", "Price"}, {"OrderDate", "Date"}})
  • Table.TransformColumnTypes — sets data types for specified columns:
    #"Typed" = Table.TransformColumnTypes(Source, {{"OrderID", Int64.Type}, {"Revenue", Int64.Type}, {"OrderDate", type date}})
  • Table.SortRows — sorts by one or more columns, ascending or descending:
    #"Sorted" = Table.SortRows(Source, {{"Revenue", Order.Descending}})
  • Table.KeepTopNRows / Table.FirstN — keeps only the top N rows:
    #"Top 3" = Table.FirstN(Source, 3)

Text Functions

Text.Upper("north")         // Returns "NORTH"
Text.Proper("acme corp")    // Returns "Acme Corp"
Text.Length("Acme Corp")    // Returns 9
Text.Start("Acme Corp", 4)  // Returns "Acme" (first 4 characters)
Text.End("Acme Corp", 4)    // Returns "Corp" (last 4 characters)
Text.Contains("Acme Corp", "Corp")  // Returns true
Text.Replace("Acme Corp", "Corp", "Ltd")  // Returns "Acme Ltd"
Text.Trim("  Acme Corp  ") // Returns "Acme Corp" (removes surrounding spaces)

Date Functions

Date.Year(#date(2026, 1, 15))    // Returns 2026
Date.Month(#date(2026, 1, 15))   // Returns 1
Date.Day(#date(2026, 1, 15))     // Returns 15
Date.DayOfWeek(#date(2026, 1, 15))  // Returns 3 (Thursday, zero-indexed from Sunday)
Date.AddDays(#date(2026, 1, 15), 30)  // Returns #date(2026, 2, 14)
Date.AddMonths(#date(2026, 1, 15), 3) // Returns #date(2026, 4, 15)

List Functions

List.Sum({170000, 6000, 45000, 255000, 14000, 2400})   // Returns 492,400
List.Max({2, 5, 1, 3, 4, 2})   // Returns 5
List.Min({2, 5, 1, 3, 4, 2})   // Returns 1
List.Count({2, 5, 1, 3, 4, 2}) // Returns 6
List.Distinct({"North","South","North","East","South","East"})
// Returns {"North","South","East"} — unique values only

Null Handling

M uses null for blank or missing values. Order 5 in the sample data has a blank Status — in M, that's stored as null.

Two patterns handle nulls in custom column expressions:

The null-coalescing operator ??:

#"Status Fixed" = Table.AddColumn(
    Source,
    "Status Clean",
    each [Status] ?? "Unknown",
    type text
)
// Order 5's null Status becomes "Unknown"; all others keep their original Status value

Explicit null check with if:

each if [Status] = null then "Unknown" else [Status]

Part 6: Conditional Logic — if...then...else

M's conditional expression uses if, then, and else — all lowercase. The structure is always: if condition then result_when_true else result_when_false.

Simple binary condition:

#"Priority Flag" = Table.AddColumn(
    Source,
    "Priority",
    each if [Revenue] > 50000 then "High" else "Standard",
    type text
)

Multiple conditions using nested if:

#"Tier" = Table.AddColumn(
    Source,
    "Revenue Tier",
    each if [Revenue] >= 200000 then "Platinum"
         else if [Revenue] >= 100000 then "Gold"
         else if [Revenue] >= 20000 then "Silver"
         else "Bronze",
    type text
)

Combining two conditions with and:

each if [Revenue] > 50000 and [Region] = "North" then "Priority North" else "Other"

Part 7: Reading M Code You Didn't Write

This is the most practical skill for a beginner — understanding what a query does when you open someone else's Advanced Editor. The approach is systematic rather than trying to parse everything at once.

  1. Count the steps in the let block. Each comma-separated assignment is one step. Read the step names from top to bottom.
  2. Read the in statement. Whatever name follows in is the final output — usually the last step.
  3. Trace the pipeline. Each step references the one above it. Find the first step that references Source, and follow the chain downward.
  4. Identify unfamiliar functions. Look at function arguments: the first argument is almost always the previous step's table.

Part 8: Writing a Custom Column — Your First M Expression

In Power Query Editor: Add Column → Custom Column.

  • Adding a combined customer-region label: [Customer] & " — " & [Region]
  • Adding a formatted revenue display: "₹" & Text.From([Revenue])
  • Adding a year extracted from the order date: Date.Year([OrderDate])
  • Adding a conditional label: if [Quantity] >= 3 then "Bulk" else "Single"

Part 9: The Advanced Editor — When to Use It and When Not To

Open it from Home → Advanced Editor.

When the Advanced Editor is the right tool:

  • Editing hardcoded file paths or URLs in a Source step
  • Changing threshold values in deeply nested filter steps
  • Adding parameters that the UI doesn't expose
  • Auditing and diagnosing queries

When to leave it alone: Simple UI transformations that Power Query generates cleanly.

In Practice: Building a Real Transformation Pipeline

let
    // Step 1: Load the source data
    Source = Excel.Workbook(
        File.Contents("C:\Data\Orders.xlsx"),
        null,
        true
    ),
    Orders_Sheet = Source{[Item="Orders", Kind="Sheet"]}[Data],

    // Step 2: Promote headers and set types
    #"Promoted Headers" = Table.PromoteHeaders(
        Orders_Sheet,
        [PromoteAllScalars=true]
    ),
    #"Correct Types" = Table.TransformColumnTypes(
        #"Promoted Headers",
        {
            {"OrderID", Int64.Type},
            {"Quantity", Int64.Type},
            {"UnitPrice", Int64.Type},
            {"Revenue", Int64.Type},
            {"OrderDate", type date}
        }
    ),

    // Step 3: Handle null Status values
    #"Cleaned Status" = Table.ReplaceValue(
        #"Correct Types",
        null,
        "Unknown",
        Replacer.ReplaceValue,
        {"Status"}
    ),

    // Step 4: Add calculated columns
    #"With Tax" = Table.AddColumn(
        #"Cleaned Status",
        "Tax",
        each [Revenue] * 0.18,
        type number
    ),
    #"With Priority" = Table.AddColumn(
        #"With Tax",
        "Priority",
        each if [Revenue] >= 100000 then "High"
             else if [Revenue] >= 20000 then "Medium"
             else "Low",
        type text
    ),
    #"With Year" = Table.AddColumn(
        #"With Priority",
        "OrderYear",
        each Date.Year([OrderDate]),
        Int64.Type
    ),

    // Step 5: Remove columns no longer needed
    #"Final Columns" = Table.RemoveColumns(
        #"With Year",
        {"UnitPrice"}
    )
in
    #"Final Columns"

Common Mistakes Beginners Make in M

  • Forgetting case sensitivity on function names: Table.selectrows fails; Table.SelectRows works.
  • Referencing a step before defining it: References in the let block must flow strictly top-to-bottom.
  • Using each where it isn't needed: each belongs inside functions that iterate row by row.
  • Editing the last step name without updating in: The name in the in statement must match the final step.
  • Confusing square brackets [] and curly braces {}: [] is for column names/records, {} is for lists.

Frequently Asked Questions

What is the M language in Power Query?

M is the formula language that runs underneath Power Query. Every transformation you apply through the point-and-click interface is stored as M code in the background.

Do I need to learn M to use Power Query effectively?

Not to use Power Query — the UI handles most tasks without any M knowledge. But learning to read M makes you a significantly more effective Power Query user: you can understand what a query is doing, diagnose problems, and make targeted edits that the UI can't express directly.

What does each mean in M code?

each means "for each row in this table, evaluate the following expression using that row's data." It appears in functions like Table.SelectRows and Table.AddColumn where the expression needs to be applied row by row.

What is the let...in structure in M?

let introduces a block of named steps, each building on the previous one. in specifies which step to return as the query's output — almost always the final step.

Why does M say "Expression.Error: The name doesn't exist in the current context"?

Usually one of three reasons: a step name in the in statement doesn't match any step in the let block, a step references another step that hasn't been defined yet, or a column name has a casing mismatch.

Is M the same as DAX?

No. M and DAX are completely separate languages serving different purposes. M runs inside Power Query and handles data loading and transformation. DAX runs inside the Power BI data model and handles calculations against the loaded data.

Recommended Next Guides & Deep Dives

Continue expanding your Power BI and Fabric architecture knowledge with these closely connected tutorials:

Step-by-Step Implementation Sandbox

1. Count and identify the steps in the let block

Read the step names from top to bottom. Each comma-separated step represents an applied transformation in sequence, building the pipeline.

2. Read the in statement

Check which step follows in. This step determines what output table or value the query actually returns to Power BI.

3. Trace the pipeline chain

Find where Source is loaded, then trace each step downward as it takes the previous step's output table as its first argument.

4. Inspect function arguments and column names

Verify exact case-sensitivity and check configuration parameters like column names in square brackets and target data types.

Frequently Asked Questions

What is the M language in Power Query?

M is the formula language that runs underneath Power Query. Every transformation you apply through the point-and-click interface is stored as M code in the background. You can read, edit, and write this code directly in the Advanced Editor or formula bar.

Do I need to learn M to use Power Query effectively?

Not to use basic features — the UI handles most daily tasks. But learning to read and understand M allows you to inspect what a query is doing, diagnose step errors, and build flexible custom transformations that the UI cannot express directly.

What does each mean in M code?

The each keyword means 'for each row in this table, evaluate the following expression using that row\'s values.' It provides row-by-row context inside functions like Table.SelectRows and Table.AddColumn.

What is the let...in structure in M?

The let block introduces a sequence of named transformation steps that build on one another, while the in statement specifies which step to return as the final query output — usually the last step in the pipeline.

Why does M say "Expression.Error: The name doesn't exist in the current context"?

Usually for one of three reasons: a step name in the in statement doesn't match any step in the let block, a step references another step that hasn't been defined yet (references must go top-to-bottom), or a column name in square brackets doesn't match the actual column name exactly including case.

Is M the same as DAX?

No. M and DAX are completely separate languages serving different purposes. M runs inside Power Query to handle data extraction, cleaning, and transformation before loading into the model. DAX runs inside the Power BI tabular engine to compute measures, aggregations, and business calculations.

Vivek Dhumal

Vivek Dhumal

Microsoft Certified: Power BI Data Analyst Associate (PL-300)

Senior Power BI & Fabric Architect

Power BI & Microsoft Fabric Specialist with extensive enterprise data modeling, DAX optimization, and ETL architecture experience.