How to Learn M Query (Power Query Formula Language)
M Query is the language behind Power Query in Excel & Power BI. Since you already work
with SQL, DAX, and automation, you can quickly learn M Query for data transformation
and ETL.
Step 1: Where to Write & Test M Query
Power BI → Transform Data (Power Query Editor) → Advanced Editor
Excel → Get & Transform (Power Query) → Advanced Editor
Online Playground → M Query Playground
Best Way to Learn → Use the UI (Power Query Editor) first, then check the M code in
Advanced Editor.
Step 2: Learn M Query Basics
M is case-sensitive and functional (not procedural like VBA).
Basic Syntax
let
x = 10,
y = 20,
result = x + y
in
result
let → Defines variables
in → Returns the final result
Defining Functions
let
AddNumbers = (a, b) => a + b
in
AddNumbers(5, 10) // Output: 15
Mini-Exercise: Create a function to multiply two numbers.
Step 3: Working with Tables & Records
Since M Query is for data transformations, focus on tables and records.
Creating a Table
let
Source = #table(
{"Name", "Age"},
{{"Ali", 30}, {"Sara", 25}}
in
Source
Accessing a Column
Source[Name] // Returns: {"Ali", "Sara"}
Filtering Data
let
FilteredTable = Table.SelectRows(Source, each [Age] > 25)
in
FilteredTable
Mini-Project: Filter a sales table where Amount > 1000.
Step 4: SQL-Like Operations in M Query
If you're used to SQL, here’s how M Query translates:
SQL Operation M Query Equivalent
SELECT * FROM
Source
Table
WHERE Age > 30 Table.SelectRows(Source, each [Age] > 30)
GROUP BY Table.Group(Source, "Category", {{"Total", each List.Sum([Sales]), type
Category number}})
Table.NestedJoin(Table1, "ID", Table2, "ID", "NewColumn",
JOIN (INNER)
JoinKind.Inner)
Mini-Exercise: Write an M Query to group sales data by region.
Step 5: Common Transformations in Power BI
Adding a New Column
Table.AddColumn(Source, "NewColumn", each [Sales] * 1.1, type number)
Replacing Values
Table.ReplaceValue(Source, "OldValue", "NewValue", Replacer.ReplaceText,
{"ColumnName"})
Merging Queries (SQL Joins)
Table.NestedJoin(Table1, "ID", Table2, "ID", "Merged", JoinKind.Inner)
Mini-Project: Create a calculated column for Profit = Sales - Cost.
Step 6: Performance Optimization in M Query
Since M Query can slow down with large datasets, optimize with:
Disable "Auto Data Type Detection"
Filter Data as Early as Possible
Use Table.Buffer() for Large Data
Prefer List.Accumulate() over Loops
Example:
let
ListSum = List.Accumulate({1..100}, 0, (state, current) => state + current)
in
ListSum
Resource: Performance Tuning Guide
Step 7: Real-World Applications Based on Your Work
Since you're a BI developer, use M Query for:
Automating Data Cleaning → Remove duplicates, fix column types
Complex ETL Workflows → Merge multiple datasets
Financial Reports → Transform raw financial data for Power BI
SQL Query Optimization → Pre-process data before loading to Power BI
Final Project Idea:
• Load an Excel file into Power Query
• Clean missing values
• Merge with a SQL dataset
• Create calculated columns
• Export final table to Power BI
Step 8: Keep Practicing & Explore Advanced M Query
Resources:
Official Power Query M Language Reference
Power BI M Query Cookbook
M Query GitHub Examples
Final Thoughts:
Since you already use SQL and Power BI, M Query will feel natural. Start with basic
queries, then data transformations, and finally complex ETL pipelines.
Would you like help with a specific M Query transformation or an advanced M Query
project?