M Code in Function: What It Is & How to Use It

Formulas & functions · Updated November 23, 2026

Yes, “M code” in a function refers to the M language used inside Power Query (Excel’s Get & Transform) — specifically, you can write M code directly inside a custom function by using the let expression and the (parameter) => ... syntax. In practice, you create a custom function in Power Query by converting a query into a function, or by writing an inline function in the Advanced Editor, and that function can take parameters and return a value, just like a standard programming function.

What Is M Code in Power Query?

M is the formula language behind Power Query, which is built into Excel (Microsoft 365, Excel 2021, and earlier versions as an add-in) and Power BI. It is a functional, case-sensitive language that transforms data step by step. When you open the Power Query Editor and click “Advanced Editor,” you see M code. A function in M is a value that maps a set of input parameters to an output. You can write a function like this:

let
    AddOne = (x as number) => x + 1
in
    AddOne

That function takes a number x and returns x + 1. To use it, you invoke it with AddOne(5) which returns 6. The key point: M code is not a macro language like VBA; it is a query language that runs inside Power Query’s engine.

How to Write a Custom Function in M

There are two main ways to create a custom function in Power Query:

  • From a query: Right-click a query in the Queries pane and select “Create Function.” This turns the query’s steps into a function, and any parameters you defined in the query become function parameters.
  • From the Advanced Editor: Open a blank query, go to Advanced Editor, and type a function expression directly. For example: (start as number, end as number) => List.Sum({start..end}).

Once you have a function, you can invoke it from a new query by using the “Invoke Custom Function” button, or you can call it from another M expression, such as Table.AddColumn(Source, "Total", each MyFunction([Qty], [Price])).

How M Functions Differ from Excel Formulas

Excel worksheet formulas (like =SUM(A1:A10)) operate on cell ranges and are recalculated by Excel’s calculation engine. M functions operate on tables and lists inside Power Query’s memory. They are not entered into a cell; instead, they are part of a data transformation pipeline. For example, to add a column that multiplies two existing columns, you would write in M:

Table.AddColumn(PreviousStep, "Total", each [Qty] * [Price])

That each keyword is shorthand for a function that takes the current row as an implicit parameter. In Excel, the same operation would be a formula like =D2*E2 filled down. So M functions are more like reusable, parameterized transformation steps, not cell formulas.

Common M Function Patterns and Examples

Here are three practical patterns you will use often:

  • Simple arithmetic function: (a as number, b as number) => a * b – returns the product.
  • Text transformation: (text as text) => Text.Upper(text) – converts to uppercase.
  • Table filter: (tbl as table, minPrice as number) => Table.SelectRows(tbl, each [Price] > minPrice) – filters rows.

To test a function, create a new query and call it with literal values, like MyFunction(3, 4). The result appears in the preview pane. You can also use functions inside other M expressions, such as List.Transform({1,2,3}, each AddOne(_)).

How to Choose Between M Function and VBA

If you need to transform data inside Power Query, use M. If you need to automate Excel’s UI, manipulate worksheets, or run a macro after data is loaded, use VBA. M runs only in Power Query; VBA runs in Excel’s main application. For example, to combine 20 CSV files with different column names, M is ideal because you can write a function that reads a file path and returns a cleaned table. To send an email from Excel, you need VBA. There is no way to call VBA from M, and no way to call M from VBA (unless you refresh a query). So choose based on where the data manipulation must happen.

Common Errors and How to Fix Them

M is strict about syntax. The most frequent mistakes:

  • Missing comma between parameters: (a as number b as number) causes an error. Always separate with commas.
  • Case sensitivity: table is not the same as Table. Function names like Table.AddColumn must be capitalized exactly.
  • Using each outside a row context: each only works where a row is implied, like in Table.AddColumn or Table.SelectRows. If you use it in a standalone function, you get an error.
  • Type mismatch: If you declare a parameter as number and pass text, Power Query throws an error. Remove the type annotation or convert the value with Number.FromText().

When you see an error, read the message carefully—it often points to the exact line and column. Use the “Diagnose” option in the Advanced Editor to step through your code.

Performance and Best Practices

M functions are lazy by default; they only compute when needed. But poorly written functions can slow down your query. Best practices:

  • Use Table.Buffer() only when you reuse a table multiple times; otherwise it wastes memory.
  • Avoid calling a function row-by-row inside a loop if you can use a built-in like Table.TransformColumns.
  • Keep parameter types explicit to catch errors early.
  • Name your functions clearly, like fnCleanDate instead of F1.

For large datasets, prefer native M functions over custom ones because the engine optimizes built-ins. Custom functions are fine for small to medium data, but if you process millions of rows, test the performance.

Frequently Asked Questions

Can I use M code in a regular Excel cell? No. M code only runs inside Power Query. To use a similar calculation in a cell, you write a normal Excel formula. If you want to call a Power Query function from a cell, you can load the function’s output to a table and reference that table, but you cannot invoke M directly in a cell.

Is M the same as DAX? No. DAX is used for calculated columns and measures in Power Pivot and Power BI. M is for data transformation before loading. They are different languages. You might use M to clean data, then DAX to calculate a measure on that data.

How do I pass a table to an M function? You declare the parameter as as table, like (tbl as table) => Table.RowCount(tbl). Then when you invoke it, you pass a table value, such as MyFunction(Source) where Source is a query that returns a table.

Can I debug an M function step by step? Power Query does not have a traditional debugger. Instead, you can break your function into smaller steps inside a let expression and view each step’s result in the Applied Steps pane. Or you can test the function with simple literal inputs to isolate the problem.

Does M code work in Excel for Mac? Yes, Power Query is available in Excel for Microsoft 365 on Mac, but the interface differs slightly. The Advanced Editor and M language are the same. However, some connectors (like certain database drivers) may not be available on Mac. The M code itself is identical.

What is the difference between each and a full function? each is a shorthand for a function that takes one parameter named _. For example, each [Price] * 2 is equivalent to (_) => _[Price] * 2. Use each for simple row operations; use a full function when you need multiple parameters or a reusable block.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.