Power Query M is the formula language behind Power Query in Excel, Power BI, and other Microsoft tools. You use it to clean, transform, and combine data—often by editing the code that Power Query generates for you. The shortest path to using M is to perform a few transformations through the Power Query Editor interface, then open the Advanced Editor to see the M code and adjust it. No installation is needed beyond having Power Query available in your version of Excel or Power BI.

What You'll Need

  • Excel 2016 or later (Power Query is built into the Data tab) or Power BI Desktop.
  • A data source such as a CSV file, Excel workbook, or database you can connect to.
  • Basic familiarity with the Power Query Editor ribbon: Home, Transform, Add Column.

Step-by-Step

  1. Load your data into Power Query. In Excel, go to Data > Get Data > From File > From Text/CSV (or another source). In Power BI Desktop, use Home > Get Data. Select your file and choose Transform Data rather than Load, so the Power Query Editor opens. Menu labels can differ slightly by version and platform.
  2. Apply one simple transformation. For example, select a column and use Home > Remove Columns, or Transform > Trim to remove extra spaces. Each action adds a step in the Applied Steps list on the right.
  3. Open the Advanced Editor. On the Home tab, click Advanced Editor. You will see the full M script: a let block containing step names, expressions, and a final in clause that returns the last step. This is the core structure of every M query.
  4. Read the generated code. Each step is written as StepName = Expression, separated by commas. The expression often calls a function like Table.SelectRows or Table.TransformColumnTypes. The step names match what you see in Applied Steps.
  5. Make a small edit. For example, change a hard-coded value in a filter step, or rename a step for clarity. Keep the comma placement and the final in clause intact. Click Done to apply. If there is a syntax error, Power Query will show a message and highlight the problem.
  6. Add a custom column with M. Go to Add Column > Custom Column. In the dialog, write an expression such as [Price] * [Quantity] or use a function like . This is the easiest way to write M without editing the whole script.
  7. Use the formula bar for quick edits. Select a step in Applied Steps; the formula bar shows its M expression. You can edit it directly there for simple changes.
  8. Learn a few core functions. Common ones include Table.SelectRows (filter), Table.TransformColumnTypes (change data types), Table.AddColumn (add calculated column), Table.Group (group and aggregate), and Text.Trim, Text.Upper, Date.From for value transformations. The full function reference is in Microsoft's online documentation.
  9. Handle errors. Use try ... otherwise ... to replace errors with a default value. For example, try Number.From([Amount]) otherwise 0. You can also use the interface: select a column, then Transform > Replace Errors.
  10. Close and load. When your query works, click Home > Close & Load (Excel) or Home > Close & Apply (Power BI). The transformed data is written to a table or the data model.

Common Mistakes

  • Forgetting commas between steps. Each step in the let block must end with a comma except the last one before in. A missing comma causes a syntax error.
  • Referencing a step that does not exist. Step names are case-sensitive and must match exactly. If you rename a step in code, update every reference to it.
  • Editing the wrong step. Changes in the Advanced Editor apply to the whole query. If you only want to adjust one transformation, edit that step's formula in the formula bar instead.
  • Ignoring data types. M is strongly typed in many operations. A column of numbers stored as text will cause errors in arithmetic. Use Table.TransformColumnTypes early.
  • Hard-coding file paths. If you share the workbook, absolute paths break. Use parameters or relative paths where possible.

Tips

  • Use the Advanced Editor to copy query logic between files—M is plain text and portable.
  • Name your steps meaningfully (double-click a step in Applied Steps) to make the code readable.
  • Comment your M code with // for single lines or /* ... */ for blocks.
  • Use the each keyword for row-level expressions, e.g. Table.SelectRows(Source, each [Sales] > 100).
  • Check the formula bar as you click ribbon buttons—it is a live M tutorial.

Troubleshooting

If your query fails, start with the error message: it often names the step and the function. Click the step before the error to see the data at that point. Use the Advanced Editor to inspect the code for missing commas or misspelled step names. If a function behaves unexpectedly, check its arguments against Microsoft's M function reference. For data type errors, add a type conversion step. If the query is slow, reduce the number of steps that scan the whole table, and filter early. If the Advanced Editor will not open, the query may be protected or the file may be in a restricted mode—check Trust Center settings in Excel. When all else fails, recreate the query step by step through the interface and compare the generated M code.

Frequently Asked Questions

Do I need to learn M to use Power Query?

No. Most transformations can be done through the interface. M is there when you need custom logic or want to understand and edit the generated steps.

Is M the same as DAX?

No. M is used in Power Query for data preparation; DAX is used in Power BI and Excel Power Pivot for calculations on the data model.

Can I use M in Excel without Power BI?

Yes. Power Query is built into Excel 2016 and later under the Data tab, and it uses M.

Where can I find a list of M functions?

Microsoft's online documentation has a complete Power Query M function reference, organized by category.

Why does my M code show an error after I edit it?

Common causes are missing commas, incorrect step names, or unmatched parentheses. Compare your code with the original generated version.

Can I undo changes in the Advanced Editor?

Yes, use Ctrl+Z in the editor before clicking Done, or close without applying. Once applied, you can delete or reorder steps in Applied Steps.