M Query (formally known as the Power Query Formula Language) is a functional language used by Excel's Power Query engine to perform data transformation and preparation. While most users rely on the graphical user interface (GUI) to click buttons for filtering or merging, M Query is the actual code running in the background. By editing this code directly, you can create complex conditional logic, dynamic file paths, and advanced data transformations that the standard ribbon buttons cannot achieve.

What You'll Need

  • Microsoft Excel (Power Query is built-in for versions 2016 and later; for 2010 or 2013, the Power Query add-in must be installed).
  • A data source to transform (such as an Excel table, a CSV file, or a database connection).
  • Basic familiarity with the Power Query Editor window.

Step-by-Step

  1. Import Your Data: Go to the Data tab on the Excel ribbon. Select Get Data and choose your source (e.g., From File > From Workbook). Once the preview window appears, click Transform Data to open the Power Query Editor.
  2. Generate Initial M Code: Perform a few basic actions using the GUI, such as removing a column or filtering a row. Look at the Applied Steps pane on the right; each step you click is actually a line of M code being written for you.
  3. Open the Advanced Editor: To see and edit the full M script, go to the View tab in the Power Query Editor and click Advanced Editor. (Note: Menu labels may vary slightly depending on your version of Excel or region).
  4. Understand the M Structure: The Advanced Editor displays the code in a "let...in" block.
    • The let block defines your variables and transformation steps. Each line typically ends with a comma, except for the final step before the in statement.
    • The in block tells Power Query which step's result to actually load back into the Excel sheet.
  5. Edit the Code: You can now manually change the logic. For example, if you want to change a hard-coded file path to a variable or create a complex if...then...else statement, type it directly into the editor. Ensure that every step refers to the name of the previous step (e.g., Step2 = Table.SelectRows(Step1, ... )).
  6. Validate and Apply: Click Done in the Advanced Editor. If there is a syntax error, Excel will notify you with a warning. If the code is correct, the data preview will update. Click Close & Load on the Home tab to bring the transformed data into your Excel worksheet.

Common Mistakes

  • Case Sensitivity: M is strictly case-sensitive. Writing table.selectrows instead of Table.SelectRows will result in a syntax error.
  • Missing Commas: Every line in the let block must end with a comma, except for the very last step before the in keyword. Forgetting a comma is the most frequent cause of "Token Eof expected" errors.
  • Incorrect Step Referencing: If you rename a step in the Applied Steps pane or manually in the code, you must update every subsequent step that refers to that name, otherwise the query chain breaks.
  • Confusing M with DAX: M Query is used for preparing data (ETL), while DAX is used for analyzing data in Power Pivot. They are different languages with different syntaxes.

Tips

  • Use the Formula Bar: You don't always need the Advanced Editor. Enable the Formula Bar via the View tab to edit a single step's M code without leaving the main editor window.
  • Comment Your Code: Use // for single-line comments or `/* ... */` for multi-line comments in the Advanced Editor. This is vital for remembering why you wrote a complex piece of logic six months later.
  • Step Naming: If your step names contain spaces (e.g., "Filtered Rows"), M wraps them in quotes and a hashtag: #"Filtered Rows". To make your code cleaner, you can rename steps to avoid spaces.
  • Leverage the GUI First: Always try to perform the action using the buttons first. Then, open the Advanced Editor to see how Power Query wrote the code. This is the fastest way to learn the M syntax.

Troubleshooting

If your query fails to load or displays an error, try these steps in order:

  1. Check for Syntax Highlighting: In the Advanced Editor, look for red underlines or highlighted text; these usually indicate a missing comma or a misspelled function.
  2. Isolate the Step: In the Applied Steps pane, click through the steps one by one from the top. The first step that shows an error is where the M code is broken.
  3. Verify Data Types: Many M errors occur because a function expects a number but receives text. Check the data type icon (e.g., 1.2 or ABC) next to the column header.
  4. Refresh the Connection: If the error is "DataSource.Error," check that the source file hasn't been moved, renamed, or opened by another user in a way that locks the file.

Frequently Asked Questions

Is M Query the same as VBA?

No. VBA is used to automate Excel's interface and workbook actions. M Query is specifically designed for data transformation and connecting to external data sources.

Do I need to be a programmer to use M Query?

No. Most users can do 90% of their work using the buttons in the Power Query Editor. Learning M Query is only necessary for advanced logic and dynamic automation.

Can I use M Query to merge data from different files?

Yes. M Query is highly efficient at combining multiple files from a folder or merging tables from different sources based on a common key.

Where can I find a list of all M functions?

The official Microsoft documentation provides a comprehensive library of M functions, categorized by whether they operate on tables, lists, records, or text.

Does M Query slow down my Excel workbook?

M Query runs in the background and only processes data during a refresh. While very large datasets can increase refresh time, it is generally more memory-efficient than using thousands of complex Excel formulas.