Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Power Query Language: M Code Basics Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Power Query is Microsoft’s data transformation engine within Excel, and its underlying language is called M. While the graphical interface allows users to import and clean data without writing code, understanding M enables advanced automation, custom functions, and precise control over data shaping that the UI cannot provide. You do not need to be a programmer to start; you only need Excel with Power Query enabled, which is standard in modern versions of Office.

What You'll Need

  • Microsoft Excel (Office 365, Excel 2016 or later)
  • A source file for testing (CSV, Excel workbook, or web URL)
  • Basic understanding of Excel tables and data types
  • Patience for debugging syntax errors

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > From File > From Text/CSV (or your preferred source). Click Transform Data instead of Load to open the editor window. Note that menu labels may vary slightly by version.
  2. Access the Advanced Editor: On the Home ribbon within the Power Query Editor, click Advanced Editor. This opens a text-based view showing the M code generated by your previous UI actions. This is where you will write and edit your logic.
  3. Understand the Structure: Observe the code pattern. It typically starts with let, followed by named steps separated by commas, and ends with in referencing the final step name. For example: Source = Csv.Document(...), ChangedType = Table.TransformColumnTypes(Source, ...).
  4. Edit a Simple Step: Locate the ChangedType line. Manually change a column type from type number to type text. Ensure you keep the comma at the end if more steps follow. Press Done to see if the preview updates correctly.
  5. Create a Custom Column using M: Close the Advanced Editor. In the main editor, go to Add Column > Custom Column. Instead of typing an Excel formula, use M syntax. For example, to concatenate two columns named [First] and [Last], enter: [First] & " " & [Last]. Click OK.
  6. Inspect Generated Code: Reopen the Advanced Editor. Notice how the UI action created a new step like AddedCustom = Table.AddColumn(PreviousStep, "FullName", each [First] & " " & [Last]). Study this to learn how UI actions translate to M functions.
  7. Write a Basic Function: To create reusable logic, go to View > New Query > Blank Query. Rename it to fnCleanText. In the formula bar, type: = (text as nullable text) => Text.Trim(Text.Lower(text)). Press Enter. This creates a function that trims and lowercases any input text.
  8. Apply the Function: Go back to your main query. Select a text column, then choose Transform > Invoke Custom Function. Select fnCleanText and map the input parameter to your selected column. Click OK to apply the transformation across all rows.
  9. Load to Excel: Once satisfied, click Close & Load on the Home ribbon. Choose Close & Load To... to specify whether to load into a table, pivot table, or just connection only.

Common Mistakes

One frequent error is confusing Excel formulas with M syntax. Excel uses semicolons or commas depending on locale, while M always uses commas to separate arguments and parentheses for grouping. Another common issue is case sensitivity; M treats Table.SelectRows differently than table.selectrows. Always use exact casing provided by IntelliSense. Additionally, beginners often forget to reference the previous step name correctly when inserting new lines manually, causing circular references or missing dependencies.

Tips

Use the Formula Bar for quick edits rather than constantly opening the Advanced Editor. It provides real-time validation and IntelliSense suggestions. When learning, record your actions in the UI first, then inspect the resulting M code to understand what happened behind the scenes. Save your queries frequently, as Power Query does not have a robust undo history once you close the editor. Use comments in M code by starting a line with // to document complex logic for future reference.

Troubleshooting

If you encounter a "Data Source Error," check if the original file path has changed or if the file is currently locked by another user. Update the source path in the Applied Steps pane under the Source step. If you receive a "Syntax Error" in the Advanced Editor, look for missing commas between steps or unclosed parentheses. The editor usually highlights the problematic line. For performance issues with large datasets, ensure you are filtering rows early in the query sequence rather than after loading all data. If a custom function fails, test it with simple static inputs in a blank query before applying it to a full table.

Frequently Asked Questions

Do I need to know coding to use Power Query?

No. Most tasks can be completed using the graphical interface buttons. Learning M is optional but recommended for advanced customization and troubleshooting.

Is M language different from DAX?

Yes. M is used for data preparation and transformation in Power Query, while DAX is used for calculations and modeling within Power Pivot or Power BI models. They serve different stages of the data workflow.

Can I copy M code between files?

You can copy individual steps or entire queries between workbooks using the clipboard, but ensure the data sources exist in the target environment to avoid connection errors.

How do I debug slow queries?

Check the Applied Steps list to see if unnecessary transformations are happening on large row sets. Move filters as early as possible in the sequence to reduce the volume of data processed in subsequent steps.

Does Power Query work in older Excel versions?

Power Query was introduced as an add-in for Excel 2010 and 2013. It is built-in for Excel 2016 and later. Older versions may require downloading the official Microsoft add-in from their website.