Introduction to M Code: Formula Bar, Advanced Editor & Custom Columns
Overview & Learning Objectives
Every click you make in the Power Query graphical interface generates code in "M" (the Mashup functional language). While the GUI handles 90% of basic transformations, opening the Advanced Editor and reading M code allows you to create dynamic file paths, debug schema mismatches, and automate complex ETL logic.
- Understand the foundational structure of an M query: the `let` and `in` blocks.
- Inspect and edit generated transformation functions in the Formula Bar.
- Create custom conditional columns using M syntax (`if ... then ... else ...`).
Industry Standard
Standard: Senior BI engineers at Amazon and Deloitte write custom M code in the Advanced Editor to parameterize environment endpoints (dev, staging, prod) and bypass manual GUI clicks.
Real-World Example: Corner Scoop Ice Cream
Corner Scoop categorizes daily revenue transactions into VIP and Regular tiers using custom logic in Power Query.
| Order ID | Bill Amount (₹) | M Code Expression | Evaluated Customer Tier |
|---|---|---|---|
| ORD-8801 | 650 | if [Amount] >= 500 then "VIP" else "Standard" | VIP |
| ORD-8802 | 320 | if [Amount] >= 500 then "VIP" else "Standard" | Standard |
| ORD-8803 | 980 | if [Amount] >= 500 then "VIP" else "Standard" | VIP |
Syntax & Core Workflow
let Source = Csv.Document(...), Step1 = Table.PromoteHeaders(Source), in Step1
Parameters Breakdown
let block: Defines sequential variables and step names that execute transformations step by step.in block: Specifies the final step variable whose resulting table is rendered and loaded to the model.#"Step Name": M syntax for referencing step names containing spaces or special characters.Table.AddColumn(): Core M function used to append a computed custom column across all rows.
Sample Walkthrough (Q&A)
Question: Is M code case-sensitive?
Formula / Action: Yes; in M, "Table.SelectRows" works, but "table.selectrows" throws a fatal syntax error.
Result: Strict case-sensitivity across all keywords, function names, and column identifiers.
Explanation: Unlike DAX or SQL which are case-insensitive, M is strictly case-sensitive. "if", "then", and "else" must always be written in lowercase.
Common Mistakes & Limitations
- Writing uppercase "IF ... THEN ... ELSE" in Power Query custom column formulas, which fails immediately in M.
- Forgetting a trailing comma at the end of a line in the `let` block, causing a syntax breakdown in the Advanced Editor.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Open Advanced Editor | Alt -> Home -> Advanced Editor | Option -> View -> Advanced Editor |
| Format M Code in Editor | Shift + Alt + F | Shift + Option + F |
Pro Tips & Modern Alternatives
- Use the "Column From Examples" button in the Add Column ribbon tab to let Power Query automatically write the M code pattern for you.
- Add comments to complex M steps using `// single line comment` or `/* multi-line comment */` to document ETL logic.