Power Query code, written in the M formula language, lets you clean, reshape, and combine data in Excel and Power BI with repeatable, auditable steps. You do not need to write M from scratch: the Power Query Editor records your actions as code, and you can edit that code directly when the interface is not enough. This guide shows you how to view, edit, and troubleshoot that code safely. You will need Excel for Microsoft 365, Excel 2016 or later, or Power BI Desktop, plus a data source such as a CSV file or an Excel table. No programming experience is required, but a basic comfort with formulas helps. Menu labels can differ slightly by version, device, or region, so treat the exact paths below as a guide.
What You'll Need
- A computer with Excel (Microsoft 365 or 2016+) or Power BI Desktop installed.
- A data source you can access, such as a CSV file, an Excel workbook, or a database you are allowed to query.
- Permission to edit the workbook or report where you will add the query.
- Optional: a text editor for drafting longer M expressions before pasting them in.
Step-by-Step
- Open the Power Query Editor. In Excel, select any cell in your data and go to Data > Get & Transform Data > From Table/Range. In Power BI Desktop, use Home > Transform Data. This opens a separate editor window where every action you take is recorded as a step.
- Load or connect to your data. If you are starting from a file, use Data > Get Data > From File and choose the file type. The Navigator dialog lets you preview tables; select the one you want and choose Transform Data rather than Load, so you land in the editor instead of loading raw data.
- Watch the Applied Steps list. On the right side of the editor, the Applied Steps pane shows each transformation as a named step, such as Source, Promoted Headers, and Changed Type. Click any step to see the data as it looked at that point. This list is the backbone of your query.
- View the M code. Go to Home > Advanced Editor. A dialog opens showing the full M expression for the query, including a
letblock that defines each step and aninblock that returns the final result. Read it top to bottom: each line usually references the step above it. - Make a small edit and test it. In the Advanced Editor, change a step name or adjust a value, then select Done. For example, if a step is named
Changed Type, you can rename it toChanged Type to Textfor clarity. If the editor shows an error, the change is not applied until it is valid. - Write a simple custom column in M. Go to Add Column > Custom Column. In the formula box, enter an expression such as
[Price] * [Quantity]to create a total. Select OK. The new column appears, and a new step is added to Applied Steps. You can later open the Advanced Editor to see exactly how that column was generated. - Use the formula bar for quick edits. If the formula bar is hidden, enable it from the View tab. Select a step in Applied Steps, and the formula bar shows its M expression. You can edit it there and press Enter to apply. This is faster than opening the Advanced Editor for small changes.
- Combine queries with a merge or append. Use Home > Merge Queries to join two tables on a matching column, or Home > Append Queries to stack tables with the same columns. The editor writes the M code for you. Check the resulting step in the Applied Steps pane and preview the data to confirm the join type and column matches are correct.
- Check data types explicitly. Select a column, then use the data type icon in the column header or Transform > Data Type to set Text, Whole Number, Date, and so on. Do this after cleaning steps, because changing types too early can cause errors when later steps expect text.
- Close and load. When the preview looks right, go to Home > Close & Load. In Excel, choose Close & Load To to send the result to a table, a PivotTable, or the Data Model. In Power BI Desktop, use Close & Apply. The query then refreshes with the workbook or report.
Common Mistakes
One frequent mistake is editing the wrong step. If you change a step near the top, every step below it re-runs against different data, which can break later transformations. Always click the step you intend to change and confirm the preview updates. Another common error is deleting a step that other steps depend on; the query will fail until you restore it or rewrite the references. Also, avoid hard-coding file paths or server names in M code if the query will be shared, because other users may not have the same drive letters or permissions. Finally, do not assume the Advanced Editor validates everything: it checks syntax, but a query can still return wrong results if a join or filter is logically incorrect, so preview each step.
Tips
Rename steps to describe what they do, such as Filtered Out Blanks instead of Filtered Rows1. Clear names make the M code readable and easier to debug months later. Use the Advanced Editor to copy a query's code into a text file as a backup before large changes. When writing custom M functions, keep them small and test them on a single column before applying them to the whole table. If a query is slow, check whether a step is sorting or merging large tables unnecessarily; moving a filter earlier can reduce the rows processed by later steps. In Excel, remember that queries refresh on open only if you enable that option in the query properties, so verify the setting if your data looks stale.
Troubleshooting
If the Advanced Editor shows a syntax error, read the message for the line number and check for missing commas, brackets, or quotation marks. M is case-sensitive for keywords such as let and in, so match the case exactly. If a step returns an error such as "We cannot convert a value of type Text to type Number," the column contains non-numeric text; clean or replace those values before changing the type. If a merge returns more rows than expected, the join columns likely contain duplicate values; check both tables for duplicates before merging. If refresh fails with a credentials error, go to Data > Get Data > Data Source Settings and update the permissions for that source. If the query works in the editor but not after loading, confirm the load destination and that the workbook or report has permission to access the source. As a last resort, use Home > Advanced Editor to copy the code, create a new blank query, and paste it in to isolate whether the problem is in the code or in the query's connection settings. Clearing credentials or deleting a query cannot be undone easily, so back up the workbook first.
Frequently Asked Questions
Do I need to learn M to use Power Query?
No. Most transformations can be done through the ribbon and menus, which generate M code automatically. Learning the basics of M helps when you need custom logic or want to debug a step.
Can I edit Power Query code directly in Excel?
Yes, through the Advanced Editor and the formula bar inside the Power Query Editor. You cannot edit the M code from the main Excel grid; you must open the editor first.
Why does my query show different results after I edit a step?
Each step builds on the one above it. Editing an earlier step changes the data that later steps receive, so the final result can change. Check the preview at each step to confirm the effect.
Is Power Query code the same in Excel and Power BI?
The M language is the same, but some connectors and load options differ between the two applications. A query copied between them may need small adjustments to the source or destination settings.
How do I undo a change in the Power Query Editor?
Use Ctrl+Z in the editor, or delete the step from the Applied Steps pane. Deleting a step is not always reversible after you close the editor, so keep a backup copy of the query code if you are making major changes.