Power Query M is the formula language that sits behind every step you click in Power Query, and you use it by opening the Advanced Editor, reading the generated code, and writing or adjusting expressions yourself. You do not need M to clean data at all — the ribbon buttons write M for you — but learning to read and edit it lets you build transformations the interface cannot do, such as looping over a folder of files, writing conditional columns in one expression, or reusing one query inside many others as a function. M runs inside Excel (Data > Get & Transform Data), Power BI Desktop (Transform Data), and the Power BI service's dataflows and Dax Studio-free environments that host Power Query; the language itself is identical, though menu wording varies slightly by version, device and region.
What You'll Need
- A copy of Excel with Power Query (built in from Excel 2016 onward, under Data > Get & Transform Data > Launch Power Query Editor) or Power BI Desktop, which is free to install from Microsoft's official site.
- A data source you can load: a CSV file, an Excel workbook, a folder of files, or a database you can already connect to.
- Familiarity with the Power Query Editor window — the Queries pane on the left, the formula bar at the top, and the Applied Steps list on the right.
- Willingness to work on a copy of your data. M transformations normally do not alter the source, but a misplaced step can reshape a query in ways that are hard to undo, so keep an untouched source file.
Step-by-Step
- Load your data and open the editor. In Excel, select your table or use Data > Get & Transform Data > From Table/Range; in Power BI Desktop, choose Home > Transform Data. You land in the Power Query Editor. Every click you make from here on is recorded as a step in the Applied Steps pane on the right.
- Enable the formula bar. If no formula bar is visible, go to the View tab and turn on Formula Bar. This bar shows the M expression behind whichever step is selected — click any step in Applied Steps and read it.
- Read a simple step. Click a step such as "Changed Type" and look at the formula bar. It will read something like . Notice the pattern: every step takes the name of the previous step as its first argument. That chaining is the whole architecture of M.
- Open the Advanced Editor. Go to Home > Advanced Editor (also available from the Query ribbon). This shows the complete query as a
let ... in ...block: a list of step names on the left of equals signs, each referencing the step above it, and a final expression afterinnaming the step that is returned. Click Done to close without changes. - Make your first manual edit — safe and reversible. Select a step, then edit the formula bar directly. A reliable first experiment is changing a hard-coded value: in a filtered step you will see something like
Table.SelectRows(Source, each [Region] = "North"). Change"North"to"South"and press Enter. The step is re-evaluated and the preview updates. Press Ctrl+Z to undo if you prefer. - Add a conditional column with an
ifexpression. Use Add Column > Custom Column, give it a name, and enter an M expression such asif [Amount] > 1000 then "High" else if [Amount] > 100 then "Medium" else "Low". Column references use square brackets; text values use double quotes. Click OK and the new column appears as a step. - Combine a query with another query. In the Advanced Editor or formula bar you can reference another query by its name directly, for example
Table.NestedJoin(Source, {"ID"}, Customers, {"ID"}, "Cust", JoinKind.LeftOuter). The Merge Queries dialog writes this for you; reading it teaches you the argument order: source table, its key column as a list in braces, the second table, its key, the new column name, and the join kind. - Build a reusable function. Right-click a query in the Queries pane and choose Create Function (where available), or manually add a
(parameter as text) =>line above aletblock in the Advanced Editor. A function lets you point many queries at one transformation. Verify the parameter is typed and that the body still references it by name. - Combine every file in a folder. Use Data > Get Data > From File > From Folder in Excel, or Home > Get Data in Power BI Desktop. Choose the folder, click Combine, and inspect the generated query. Power Query creates a helper function (usually named after the sample file) plus a query that invokes it on every file. Renaming the helper function is a common need — rename it in the Queries pane, not in code, so references update.
- Check for errors before loading. Look for a yellow error bar under the column headers and click it to see which rows failed. Use Home > Keep Rows > Keep Errors to isolate them, fix the offending transform or add a try/otherwise expression such as
try Number.From([Amount]) otherwise null, then remove the filter step. - Set data types deliberately last. Apply column types at the end of your query so type conversion does not have to repeat on every step. Right-click a column header > Change Type > Using Locale when dates or decimals come in a non-default format.
- Load the result. Choose Home > Close & Load (Excel) or Home > Close & Apply (Power BI Desktop). In Excel, Close & Load To lets you send the output to a table, a pivot table or the data model only, depending on what you need.
Common Mistakes
Editing step names carelessly. Renaming a step in code without updating the step that references it breaks the whole chain. Rename in the Applied Steps pane, which updates references for you.
Hard-coding file paths. A query with a literal path like "C:\Users\Name\Desktop\sales.csv" fails the moment the file moves or another person opens the workbook. Use a parameter (Home > Manage Parameters) for the path, and prefer the folder-connector pattern for multi-file work.
Case sensitivity. M is case-sensitive for keywords and step names: if, then, else, let, in, and table names must match exactly. Table names are written in quotes and case matters there too.
Type mismatches inside comparisons. Comparing a text value to a number with > raises an error rather than returning false. Convert first with Number.From or Value.FromText, or use try ... otherwise.
Using = instead of ==. M uses = for assignment and equality in expressions like [Region] = "North"; unlike some languages you will not write ==, but you will see evaluation errors if you confuse assignment syntax in a let block.
Forgetting the final step. A let block must end with in followed by the name of the step you want to return. If the output looks wrong, check that in names the last intended step.
Tips
Press Ctrl while hovering over a function name to see the signature and argument order in the formula bar. Use the #shared record in the Advanced Editor to list every function and constant available in your environment — type #shared into a blank query and expand the record. Keep step names descriptive so the Applied Steps list reads like a recipe. Break long expressions into several named steps instead of one giant formula; each step is easier to read and debug. When a query gets slow, apply filters early and remove unused columns before merging. Use Table.Buffer sparingly — it can help when a small table is read repeatedly, but buffering large tables costs memory. Keep a "raw" query that only connects to the source, and reference it from downstream queries, so a broken transform never touches the connection step.
Troubleshooting
If that did not work: start with the simplest checks. Look at the error message text — M errors usually name the function and the problem (for example "We cannot convert the value null to type Text") — and click the failing step to see which one it is. Step back through Applied Steps one at a time to find the first step whose preview is wrong. If the preview is stale, refresh the query. If the file or database is unavailable, correct the source path or credentials first; errors from a missing source mask every later problem.
For language errors, open the Advanced Editor and check for a missing comma between let-step assignments, an unclosed parenthesis or brace, or a step referenced before it is defined. The editor highlights syntax problems when you click Done. If a query works for you but fails for someone else, look for credentials the source requires — they may need to set their own permissions for the source, which is a per-user setting, not something shared in the file. If the workbook or report slows dramatically, check for a step that expands a very wide nested table or a merge with duplicates on both sides.
Note that if a source needs database authentication, permissions are managed through the credentials dialog rather than in code. Never put passwords in an M expression.
Frequently Asked Questions
Do I have to learn M to use Power Query?
No. The ribbon buttons cover most cleaning tasks and write M for you. Learning to read it helps when you need something the interface cannot produce, or when you want to fix a step you half understand.
Is M the same in Excel and Power BI?
The language is the same; the available connectors and some menu wording differ. A query copied between the two generally works, provided every connector used exists in both.
Is M the same as DAX?
No. M shapes and loads data before it reaches the model; DAX calculates measures and columns after loading. They do different jobs.
Can I use M to loop or repeat an action?
There is no traditional loop. M does the equivalent with list functions such as List.Transform and List.Accumulate, or by applying a function to rows of a table.
What does each mean in M?
each introduces a one-parameter shorthand function that uses the underscore, so each [Amount] * 2 is the same as (row) => row[Amount] * 2. Interface-generated steps use it constantly.
Why does my query return the wrong column types?
The Changed Type step applies to the columns and names present when it was created. If a column is renamed or removed later, re-apply the type step at the end of the chain so the names match.