Power Query
Power Query Exercises
Fifteen messy-data problems in four phases, covering most of the Transform ribbon. Download the workbook, try each one in Power Query, then check your result here.
All names and numbers in the workbook are synthetic. Each sheet is numbered to match its exercise. In the M code, Source means the table already loaded from that sheet.
Phase 1: Basic cleaning and formatting
Get your data ready for analysis.
1 Remove rows and columns
Scenario: A legacy report has two title rows and empty rows.
Task: Remove the top 2 rows, promote headers and filter out null regions.
Before
| Column1 | Column2 | Column3 |
|---|
| Sales Report | null | null |
| As on 18-09-2025 | null | null |
| Region | Sales | Notes |
| South | 25000 | Good |
Show the result and M code
After
| Region | Sales | Notes |
|---|
| South | 25000 | Good |
| North | 32000 | Excellent |
| West | 18000 | Average |
Check: 3 rows remain.
Skipped = Table.Skip(Source, 2),
Headers = Table.PromoteHeaders(Skipped),
Kept = Table.SelectRows(Headers, each [Region] <> null)
2 Replace values and clean
Scenario: City names are inconsistent, such as BOM and Bombay, and names have stray spaces.
Task: Trim the names, then replace values to standardise the cities.
Before
| Name | City |
|---|
| Agastya Natt | BOM |
| Victor Sinha | Bombay |
| Ekani Chhabra | CHN |
| Yashvi Naik | Bombay |
Show the result and M code
After
| Name | City |
|---|
| Agastya Natt | Mumbai |
| Victor Sinha | Mumbai |
| Ekani Chhabra | Chennai |
| Yashvi Naik | Mumbai |
Check: Chennai 42, Mumbai 32, New Delhi 26.
Trimmed = Table.TransformColumns(Source, {{"Name", Text.Trim, type text}}),
City1 = Table.ReplaceValue(Trimmed, "BOM", "Mumbai", Replacer.ReplaceValue, {"City"})
// Repeat for Bombay, CHN, Madras and DEL
3 Fill down
Scenario: Merged cells leave regions blank, and Power Query reads them as null.
Task: Use Transform, Fill, Down so every city has a region.
Before
| Region | City | Sales |
|---|
| Meghalaya | Bhubaneswar | 476 |
| null | Bhiwandi | 597 |
| null | Allahabad | 257 |
| null | Bhopal | 769 |
Show the result and M code
After
| Region | City | Sales |
|---|
| Meghalaya | Bhubaneswar | 476 |
| Meghalaya | Bhiwandi | 597 |
| Meghalaya | Allahabad | 257 |
| Meghalaya | Bhopal | 769 |
Check: No nulls remain. 10 region names now cover 100 rows.
Filled = Table.FillDown(Source, {"Region"})
4 Split column
Scenario: HR exports names as Last, First, but Payroll needs two columns.
Task: Split FullName by the comma.
Before
| EmployeeID | FullName |
|---|
| EMP1000 | Master, Viraj |
| EMP1001 | Deep, Vrinda |
| EMP1002 | Gade, Leena |
| EMP1003 | De, Ati |
Show the result and M code
After
| EmployeeID | LastName | FirstName |
|---|
| EMP1000 | Master | Viraj |
| EMP1001 | Deep | Vrinda |
| EMP1002 | Gade | Leena |
| EMP1003 | De | Ati |
Check: Row 1 becomes Master and Viraj.
Split = Table.SplitColumn(Source, "FullName", Splitter.SplitTextByDelimiter(", "), {"LastName", "FirstName"})
5 Extract text
Scenario: Invoice numbers follow the pattern INV-YYYY-####.
Task: Extract the year (text between delimiters) and the sequence number (text after delimiter).
Before
| InvoiceNumber |
|---|
| INV-2020-0000 |
| INV-2021-0001 |
| INV-2024-0002 |
| INV-2025-0003 |
Show the result and M code
After
| InvoiceNumber | Year | Sequence |
|---|
| INV-2020-0000 | 2020 | 0000 |
| INV-2021-0001 | 2021 | 0001 |
| INV-2024-0002 | 2024 | 0002 |
| INV-2025-0003 | 2025 | 0003 |
Check: Invoices per year: 2020 12, 2021 23, 2022 20, 2023 13, 2024 15, 2025 17.
Year = Table.AddColumn(Source, "Year", each Text.BetweenDelimiters([InvoiceNumber], "-", "-")),
Seq = Table.AddColumn(Year, "Sequence", each Text.AfterDelimiter([InvoiceNumber], "-", 1))
Phase 2: Logic and enrichment
Create new data from existing data.
6 Column from examples
Scenario: Generate emails like firstname.lastname@company.com without writing formulas.
Task: Use Column From Examples and type the pattern for the first two rows.
Before
| Name | Company |
|---|
| Brijesh Pant | Pai-Goda |
| Anita Patil | Comar-Dugal |
| Robert Ramakrishnan | Thaman-Sengupta |
| Lajita Sahota | Thakur PLC |
Show the result and M code
After
| Name | Company | Email |
|---|
| Brijesh Pant | Pai-Goda | brijesh.pant@company.com |
| Anita Patil | Comar-Dugal | anita.patil@company.com |
| Robert Ramakrishnan | Thaman-Sengupta | robert.ramakrishnan@company.com |
| Lajita Sahota | Thakur PLC | lajita.sahota@company.com |
Check: Row 1: brijesh.pant@company.com.
// The equivalent M, if you want to see what the feature builds
Email = Table.AddColumn(Source, "Email", each Text.Lower(Text.Replace([Name], " ", ".")) & "@company.com")
7 Conditional column
Scenario: Categorise sales performance.
Task: Add Performance: above 2,00,000 is Excellent, above 1,00,000 is Good, otherwise Improvement.
Before
| Salesperson | Sales |
|---|
| Mohammed Chhabra | 99206 |
| Aarna Bansal | 79906 |
| Irya Dalal | 210570 |
| Jagvi Bhatia | 135136 |
Show the result and M code
After
| Salesperson | Sales | Performance |
|---|
| Mohammed Chhabra | 99206 | Improvement |
| Aarna Bansal | 79906 | Improvement |
| Irya Dalal | 210570 | Excellent |
| Jagvi Bhatia | 135136 | Good |
Check: Excellent 28, Good 48, Improvement 24.
Perf = Table.AddColumn(Source, "Performance", each if [Sales] > 200000 then "Excellent" else if [Sales] > 100000 then "Good" else "Improvement")
8 Date calculations
Scenario: HR needs tenure and retirement dates.
Task: Add Age, Retirement at 60 years, and Service Years from DateOfJoining to today.
Before
| EmpID | EmployeeName | DateOfBirth | DateOfJoining | Department |
|---|
| E1000 | Janaki Ramaswamy | 20-03-1968 | 19-01-2023 | Operations |
| E1001 | Nitara Lata | 30-09-2001 | 08-01-2025 | HR |
| E1002 | Yashasvi Mall | 07-06-1977 | 06-05-2008 | IT |
| E1003 | Hardik Sengupta | 15-09-1994 | 22-04-2016 | IT |
Show the result and M code
After
| EmpID | DateOfBirth | DateOfJoining | Retirement |
|---|
| E1000 | 20-03-1968 | 19-01-2023 | 20-03-2028 |
| E1001 | 30-09-2001 | 08-01-2025 | 30-09-2061 |
| E1002 | 07-06-1977 | 06-05-2008 | 07-06-2037 |
| E1003 | 15-09-1994 | 22-04-2016 | 15-09-2054 |
Check: Janaki Ramaswamy retires on 20-03-2028. Age is in completed years. Service is approximate, because days ÷ 365.25 only averages out leap years. Both depend on today’s date.
Today = Date.From(DateTime.LocalNow()),
// Today - [DateOfBirth] returns a duration, so work out completed years instead
Age = Table.AddColumn(Source, "Age", each let Y = Date.Year(Today) - Date.Year([DateOfBirth]) in if Date.AddYears([DateOfBirth], Y) > Today then Y - 1 else Y, Int64.Type),
Retire = Table.AddColumn(Age, "Retirement", each Date.AddYears([DateOfBirth], 60)),
Service = Table.AddColumn(Retire, "Service Years (approx)", each Duration.Days(Today - [DateOfJoining]) / 365.25)
Phase 3: Shape transformation
Change how the table looks, rows against columns.
9 Transpose
Scenario: Metrics are in rows and years in columns.
Task: Transpose and fix the headers.
Before
| Metric | 2023 | 2024 |
|---|
| Revenue | 500000 | 650000 |
| Expenses | 350000 | 420000 |
| Profit | 150000 | 230000 |
Show the result and M code
After
| Year | Revenue | Expenses | Profit |
|---|
| 2023 | 500000 | 350000 | 150000 |
| 2024 | 650000 | 420000 | 230000 |
Check: 2 rows, with Revenue, Expenses and Profit as columns.
Demoted = Table.DemoteHeaders(Source),
Flipped = Table.Transpose(Demoted),
Headers = Table.PromoteHeaders(Flipped)
10 Pivot
Scenario: Sensor data is tall, with metrics in one column.
Task: Pivot Metric so Temperature and Humidity become columns.
Before
| Date | Location | Metric | Value |
|---|
| 01-05-2025 | Ghaziabad | Temperature | 39.3 |
| 01-05-2025 | Ghaziabad | Humidity | 66.4 |
| 01-05-2025 | Aurangabad | Temperature | 30.9 |
| 01-05-2025 | Aurangabad | Humidity | 39.6 |
Show the result and M code
After
| Date | Location | Temperature | Humidity |
|---|
| 01-05-2025 | Ghaziabad | 39.3 | 66.4 |
| 01-05-2025 | Aurangabad | 30.9 | 39.6 |
| 01-05-2025 | Tiruppur | 65 | 89.9 |
| 01-05-2025 | Hazaribagh | 89.9 | 40.5 |
Check: 100 rows become 50.
Pivoted = Table.Pivot(Source, List.Distinct(Source[Metric]), "Metric", "Value", List.Sum)
11 Unpivot
Scenario: Sales are wide, with months as columns, so a pivot table cannot use them.
Task: Unpivot the months to get Month and Amount.
Before
| Product | Jan_Sales | Feb_Sales | Mar_Sales |
|---|
| Product_0 | 133 | 132 | 290 |
| Product_1 | 221 | 158 | 373 |
| Product_2 | 448 | 347 | 147 |
| Product_3 | 158 | 168 | 225 |
Show the result and M code
After
| Product | Month | Amount |
|---|
| Product_0 | Jan_Sales | 133 |
| Product_1 | Jan_Sales | 221 |
| Product_2 | Jan_Sales | 448 |
| Product_3 | Jan_Sales | 158 |
Check: 100 rows become 300. Total amount: 93,607.
Tall = Table.UnpivotOtherColumns(Source, {"Product"}, "Month", "Amount")
12 Group by
Scenario: Management wants total sales per category.
Task: Group by Category and sum Amount.
Before
| Category | Amount |
|---|
| Apparel | 14727.73 |
| Stationery | 2939.74 |
| Stationery | 7145.19 |
| Electronics | 10407.51 |
Show the result and M code
After
| Category | Total Sales |
|---|
| Apparel | 204988.91 |
| Electronics | 177004.82 |
| Furniture | 148169.63 |
| Groceries | 108829.52 |
Check: Apparel ₹2,04,988.91, five categories.
Grouped = Table.Group(Source, {"Category"}, {{"Total Sales", each List.Sum([Amount]), type number}})
13 Parse JSON
Scenario: Product attributes are stored as JSON text in one cell.
Task: Use Transform, Parse, JSON and expand the columns.
Before
| OrderID | Attributes |
|---|
| ORD200 | {"color": "WhiteSmoke", "storage_g… |
| ORD201 | {"color": "DeepPink", "storage_gb"… |
| ORD202 | {"color": "SlateBlue", "storage_gb… |
| ORD203 | {"color": "DarkSalmon", "storage_g… |
Show the result and M code
After
| OrderID | color | storage_gb | warranty_yrs |
|---|
| ORD200 | WhiteSmoke | 1024 | 3 |
| ORD201 | DeepPink | 1024 | 1 |
| ORD202 | SlateBlue | 512 | 1 |
| ORD203 | DarkSalmon | 256 | 1 |
Check: Storage: 512 GB 33, 1024 GB 26, 256 GB 21, 128 GB 20.
Parsed = Table.TransformColumns(Source, {{"Attributes", Json.Document}}),
Expanded = Table.ExpandRecordColumn(Parsed, "Attributes", {"color", "storage_gb", "warranty_yrs"})
Phase 4: Combining queries
Bring several tables together.
14 Append queries
Scenario: Sales come in separate Q1 and Q2 sheets.
Task: Append them into a single query.
Before
| Date | Product | Amount |
|---|
| 29-09-2025 | Veniam | 7136.78 |
| 21-10-2025 | Temporibus | 2701.39 |
| 10-11-2025 | Quasi | 8578.38 |
| 22-11-2025 | Atque | 20583.57 |
Show the result and M code
After
| Date | Product | Amount |
|---|
| 19-06-2025 | Temporibus | 10266.13 |
| 30-07-2025 | Ducimus | 10965.56 |
| 24-08-2025 | Temporibus | 12482.94 |
| 14-07-2025 | Praesentium | 20508.65 |
Check: 50 + 50 = 100 rows. Total: ₹12,52,221.58.
Combined = Table.Combine({Sales_Q1, Sales_Q2})
15 Merge queries
Objective: Join tables to answer business questions. See the Joins Playground first.
- A. Left outer: merge Orders and Customers to get the GST number, keeping it null if missing.
- B. Inner: merge Orders and Products, then calculate Total Sales (Qty × Price) for valid products only.
- C. Right anti: find dead stock, the products never ordered.
- D. Left anti: merge Customers and Orders to find customers who never bought anything.
Orders
| OrderID | CustomerID | ProductID | Quantity |
|---|
| ORD100 | CUST029 | 10 | 6 |
| ORD101 | CUST028 | 17 | 10 |
| ORD102 | CUST022 | 15 | 5 |
Products
| ProductID | ProductName | Price |
|---|
| 1 | Qui | 2492.53 |
| 2 | Culpa | 785 |
| 3 | Velit | 970.85 |
Customers
| CustomerID | CustomerName | GST_Number |
|---|
| CUST001 | Chaaya Memon | 29gDAjG6175U |
| CUST002 | Faqid Oza | 29nttBG5549Q |
| CUST003 | Manya Bala | 29bsXeJ3773c |
Show the results and M code
- A: 100 rows. 44 orders have a null GST number.
- B: Total Sales ₹21,36,978.91.
- C: Product IDs 26, 27, 28, 29, 30.
- D: CUST008 and CUST034.
A = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Cust", JoinKind.LeftOuter)
B = Table.NestedJoin(Orders, {"ProductID"}, Products, {"ProductID"}, "Prod", JoinKind.Inner)
C = Table.NestedJoin(Orders, {"ProductID"}, Products, {"ProductID"}, "Prod", JoinKind.RightAnti)
D = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Ord", JoinKind.LeftAnti)
// Then expand the new column to bring in the fields you need
Bonus: find the traps yourself
This smaller workbook hides the faults real files have: stray spaces in keys, keys with no match, a duplicate customer, a duplicate sale, amounts and dates stored as text, and duplicate pairs in a bridge table. Find them, fix them and reconcile to the total on its Answer key sheet.
Data quality practice (Excel)