Power Query uses a formula language called M (short for Mashup) to clean, reshape and combine data before you load it into Excel or Power BI. You do not have to write M by hand: open the Power Query Editor, make transformations through the buttons, and every click writes a step of M in the formula bar and in the Applied Steps list. From there you can edit those steps, add your own expressions and type a query from scratch. This guide walks you through that process: getting into the editor, reading the M your clicks produce, editing and writing expressions safely, and loading the result. It covers both Excel and Power BI Desktop, because the editor works the same way in each.

What You'll Need

  • Excel for Microsoft 365, Excel 2016 or later on Windows, or Power BI Desktop. The Mac version of Excel has a more limited Power Query Editor, so the manual M editing described here may not be available on every build; check the Data tab before you start. If M editing is missing there, use a Windows PC or Power BI Desktop.
  • A source of data: a table in a worksheet, a CSV file, a folder of files, or a database you have permission to query.
  • For Power BI: Power BI Desktop installed from Microsoft's official download page, with a report open.
  • Familiarity with where the editor lives: in Excel it is under the Data tab, in the Get & Transform Data group; in Power BI it is the Transform Data button on the Home ribbon.

Step-by-Step

  1. Bring in your data. In Excel, put the cursor inside a formatted table (or select the range) and choose Data > From Table/Range. In Power BI, use Home > Get Data and pick the connector for your source. Menu labels vary slightly by version and region, but these names are stable across recent releases.
  2. Confirm the data is recognised correctly. If the Power Query Editor does not open immediately, choose Data > Queries & Connections, then double-click your query to open it. Inside the editor you will see the data preview in the middle, a Queries pane on the left, and the Applied Steps list on the right.
  3. Read the Applied Steps list. Each entry is one transformation: Source, Navigation, Changed Type, and so on. Clicking a step shows the preview as the data looked at that moment. This list is the real story of your query, and every entry is a line of M.
  4. Turn on the formula bar. If you do not see it, go to View on the editor ribbon and tick Formula Bar. Now click any step and read the M in the bar, for example = Table.SelectRows(Source, each [Status] = "Active"). This is how you connect buttons to code: click a step, read its line.
  5. Make one change with the ribbon and watch the code appear. For example, use Home > Remove Rows > Remove Top Rows and enter a number. A new step appears in the list and a matching line appears in the formula bar. This teaches you the syntax far faster than reading documentation.
  6. Edit a generated step. Click the step in Applied Steps, then edit the text in the formula bar and press Enter. Simple, safe edits to start with are changing a number, a column name inside square brackets, or a text value in double quotes. The preview refreshes immediately, which tells you whether your syntax is acceptable.
  7. Understand the building blocks. M is case-sensitive. Column names go in square brackets: [Unit Price]. Text goes in double quotes. each introduces a function applied to every row. A step like each [Quantity] * 2 multiplies each row. Functions are named by what they act on first, such as Table.SelectRows, Table.AddColumn or Text.Trim.
  8. Add your own column with M. Use Add Column > Custom Column, name it, and type the expression. A simple example: if [Status] = "Active" then 1 else 0. Press OK: a new tip step appears and the column is added. If the expression is wrong you get an error at that step, not silently wrong data, which is one of the language's advantages.
  9. Check your column types after each step. The leftmost icon above each column shows its type. Because M is strict about types, text that looks like a date will not sort like a date. Use Transform > Data Type to set date, whole number, decimal or text deliberately, and keep the Change Type step near the end so it runs on clean data.
  10. Write a full query by hand only when you need to. Start from a blank query (Home > New Source > Blank Query, then Advanced Editor) and read the three-line skeleton: the let keyword starts the list of named steps, each name is assigned with = and separated from the next by a comma, and the in keyword names the step whose result is returned. Editing the final name after in changes what the query outputs.
  11. Test with a smaller case first. Before applying a filter or calculation to thousands of rows, keep the first few rows, check the output by eye, and only then remove the test filter. In error-prone areas such as date parsing or splitting text, this saves a lot of rework.
  12. Load the result. Choose Home > Close & Load > Close & Load To, then pick a table on a new or existing worksheet or add it to the data model. In Power BI, use Close & Apply. The query is saved with the workbook or report file, not into the source data.
  13. Refresh and re-open to confirm. Right-click the query in Queries & Connections and choose Refresh to confirm it re-runs cleanly. If you need to change it, re-open the editor and edit the same steps; nothing is lost.

Common Mistakes

Renaming a source column after loading. If someone renames a column in the original worksheet or CSV, the hard-coded name in your step no longer matches and the query fails. Note that steps refer to the column names as they were at that point in the chain, so renaming with the ribbon mid-query can also break later steps. If you must rename, do it once and check the Applied Steps list for messages.

Assuming M ignores case. Table names, column names and text comparisons are case-sensitive. [status] and [Status] are different; a filter on "active" will not match "Active".

Treating null as zero. Empty cells are null, and arithmetic on null returns null. Use Replace Values or a conditional such as if [Amount] = null then 0 else [Amount] when blanks should count as zero.

Filtering with the wrong language operator. Use = for comparison and <> for not equal, not the spreadsheet habit of mixing things up. Text concatenation uses &.

Piling everything into Custom Column. Long nested if statements are hard to debug. Break them into several named custom columns or steps instead.

Editing the source file while the editor holds it open. For CSV and Excel sources the file must not be open elsewhere in a locked way. Close it and refresh.

Tips

Use the Advanced Editor to see the whole query as text: it is the fastest way to learn M and to copy an expression to another query. Keep step names descriptive by renaming them in the Applied Steps list, which makes later M editing readable. Build the query against a small sample file first, then point it at the real one. If a calculation is complex, stage it: add a column of raw inputs, then a second column that uses it. Pause and read the formula bar for each ribbon action you take; after a few days you will find yourself writing the expression first and using the button second.

Troubleshooting

An error message appears at a step. Click that step and read the message; Power Query usually names the offending column or value. Then check spelling and case, confirm the column still exists at that stage, and confirm the right data type. Errors almost always sit in the newest step, so remove or fix it and add it back once the rest is clean.

"The key didn't match any rows in the table." This usually means an item you referred to, such as a table or sheet name in the source, has been renamed or deleted. Correct the source reference in the step.

The preview is fine but the load fails. Usually a type change or a filter that behaves differently on full refresh. Re-open the editor, step through from the top watching the preview at each step, and look for the first place row counts change unexpectedly.

A refresh is very slow. Remove generic "Changed Type" steps that convert every column, filter to the columns and rows you need as early as possible, and avoid adding columns you later delete.

The editor is missing altogether. Update your Office installation; Power Query is built into modern Excel rather than installed separately, and losing it usually means an old build or the wrong edition. If M editing is limited in your version, use Power BI Desktop, which includes the full editor.

Frequently Asked Questions

Is M the same as DAX?

No. M prepares and shapes data when it is loaded; DAX calculates on the model afterwards. They run at different stages and use different syntax.

Do I need to learn M to use Power Query?

Not for most cleaning work, because the buttons write it for you. Learning a little M helps when you need to change a generated step, debug an error, or do something no button covers.

Is M case-sensitive?

Yes. Step names, column names and text comparisons distinguish capital and lowercase letters.

Can I undo a step?

Yes. Select the step above it and delete the unwanted step with the X next to it, or simply delete from the Applied Steps list. Nothing is applied to your source file, only to the query definition.

Where does the query live?

Inside the workbook or Power BI file. The source data is unchanged, so others need the same source available to refresh the query.

Does editing a step with a formula break the buttons?

Not permanently. Buttons still write new steps normally; but a hand-edited step that produces an unexpected shape, such as a table becoming a list, will cause later steps to fail until you correct it.