Excel Power Query M Code: A How-To Guide
In this article
Power Query M code is the formula language behind Excel's Get & Transform tools. You do not have to write M from scratch: the fastest way to use it is to build a query with the Power Query Editor's buttons, then open the Advanced Editor to read, edit, or copy the generated M code. That achieves repeatable data cleaning that survives refreshes, and it lets you fix steps the interface cannot express.
What You'll Need
- Excel for Microsoft 365, Excel 2021, or Excel 2019 on Windows or Mac. Power Query is built in; no add-in is required. Labels can differ slightly by version, device, or region.
- A data source such as a table in the workbook, a CSV file, or a database you can connect to.
- Basic familiarity with Excel tables and column headers.
- Optional: the Power Query documentation open in a browser for the M function reference.
Step-by-Step
- Load your data into Power Query. Select a cell in your table, then go to Data > From Table/Range. For a file, use Data > Get Data > From File. The Power Query Editor opens in a separate window.
- Apply a few steps with the ribbon. Use Home > Remove Rows, Home > Keep Rows, Transform > Replace Values, or right-click a column header to change its type. Every action you take is recorded in the Applied Steps list on the right.
- Open the Advanced Editor. On the Home tab, click Advanced Editor. You will see the full M script: a
letblock containing named steps, each ending with a comma, and aninstatement that returns the final step name. - Read the structure before editing. Each line looks like
StepName = Operation(PreviousStep, ...). The step names are arbitrary labels; the operation names are the M functions. Note that the last step name afterinmust match one of your step names exactly. - Make a small, safe edit. For example, change a hard-coded filter value or rename a step for clarity. Keep the comma at the end of every step except the last one before
in. Click Done to apply. - Fix errors in place. If the editor shows a syntax error, check for a missing comma, a missing closing parenthesis, or a step name referenced before it is defined. The error message usually names the line.
- Add a custom column with M. Use Add Column > Custom Column and enter an expression such as
[Price] * [Quantity]. This writes M for you and is safer than hand-editing the script. - Use a custom function when you repeat logic. Write the logic once as a query, then invoke it from another query through Add Column > Invoke Custom Function. This avoids duplicating long expressions across steps.
- Close and load. Choose Home > Close & Load to send the result to a worksheet table or the Data Model. The query appears in the Queries & Connections pane.
- Refresh to confirm it is repeatable. Right-click the query and choose Refresh. If the source changes, the same M steps run again and the output updates.
Common Mistakes
- Deleting a comma or the
inline. M is strict about separators; one missing comma breaks the whole script. - Referencing a step before it exists. Steps run top to bottom, so a later step cannot feed an earlier one.
- Hard-coding values that change. A filter on a fixed date or name will silently return nothing next month. Prefer parameters or relative filters.
- Editing generated code without a backup. Copy the script into a text file before large changes so you can paste it back.
- Assuming the interface and the code stay in sync. If you hand-edit a step, the ribbon buttons may no longer describe it accurately.
Tips
- Name steps descriptively (for example
FilteredActiveRows) so the script reads like a recipe. - Use the formula bar in the editor to inspect a single step's expression without opening the full script.
- Keep transformations in the order data arrives: filter early to reduce rows, then change types, then add calculated columns.
- Use the M function reference to confirm argument order before typing a function by hand.
- Store reusable logic as a separate query rather than copying it into every file.
Troubleshooting
If the Advanced Editor will not accept your code, undo the last edit and reapply it one change at a time. If a query loads but returns wrong values, check column data types first, since text that looks numeric will not calculate. If refresh fails, confirm the source file path or credentials have not changed, then sign out and back in to the data source. If the editor is slow or unresponsive, close it, save the workbook, and reopen. For a query that worked before and now errors, compare the current script with your saved copy to spot the change.
Frequently Asked Questions
Do I need to learn M to use Power Query?
No. Most cleaning can be done with the ribbon. M becomes useful when you need logic the buttons do not offer or want to reuse a transformation.
Where do I see the M code for a query?
Open the query in the Power Query Editor and click Advanced Editor on the Home tab.
Can I copy M code between workbooks?
Yes. Copy the script from the Advanced Editor and paste it into a new blank query's Advanced Editor, then update any source references.
Why does my query return no rows after an edit?
A filter or type change is probably excluding everything. Step through the Applied Steps one at a time to see where rows disappear.
Is M the same as DAX?
No. M prepares and shapes data in Power Query; DAX calculates in the Data Model. They serve different stages.
Does editing M code affect the original data?
No. Power Query transformations apply to the query output, not to the source file, unless you explicitly write back to it.