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

Column1Column2Column3
Sales Reportnullnull
As on 18-09-2025nullnull
RegionSalesNotes
South25000Good
Show the result and M code

After

RegionSalesNotes
South25000Good
North32000Excellent
West18000Average

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

NameCity
Agastya Natt BOM
Victor Sinha Bombay
Ekani Chhabra CHN
Yashvi Naik Bombay
Show the result and M code

After

NameCity
Agastya NattMumbai
Victor SinhaMumbai
Ekani ChhabraChennai
Yashvi NaikMumbai

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

RegionCitySales
MeghalayaBhubaneswar476
nullBhiwandi597
nullAllahabad257
nullBhopal769
Show the result and M code

After

RegionCitySales
MeghalayaBhubaneswar476
MeghalayaBhiwandi597
MeghalayaAllahabad257
MeghalayaBhopal769

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

EmployeeIDFullName
EMP1000Master, Viraj
EMP1001Deep, Vrinda
EMP1002Gade, Leena
EMP1003De, Ati
Show the result and M code

After

EmployeeIDLastNameFirstName
EMP1000MasterViraj
EMP1001DeepVrinda
EMP1002GadeLeena
EMP1003DeAti

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

InvoiceNumberYearSequence
INV-2020-000020200000
INV-2021-000120210001
INV-2024-000220240002
INV-2025-000320250003

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

NameCompany
Brijesh PantPai-Goda
Anita PatilComar-Dugal
Robert RamakrishnanThaman-Sengupta
Lajita SahotaThakur PLC
Show the result and M code

After

NameCompanyEmail
Brijesh PantPai-Godabrijesh.pant@company.com
Anita PatilComar-Dugalanita.patil@company.com
Robert RamakrishnanThaman-Senguptarobert.ramakrishnan@company.com
Lajita SahotaThakur PLClajita.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

SalespersonSales
Mohammed Chhabra99206
Aarna Bansal79906
Irya Dalal210570
Jagvi Bhatia135136
Show the result and M code

After

SalespersonSalesPerformance
Mohammed Chhabra99206Improvement
Aarna Bansal79906Improvement
Irya Dalal210570Excellent
Jagvi Bhatia135136Good

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

EmpIDEmployeeNameDateOfBirthDateOfJoiningDepartment
E1000Janaki Ramaswamy20-03-196819-01-2023Operations
E1001Nitara Lata30-09-200108-01-2025HR
E1002Yashasvi Mall07-06-197706-05-2008IT
E1003Hardik Sengupta15-09-199422-04-2016IT
Show the result and M code

After

EmpIDDateOfBirthDateOfJoiningRetirement
E100020-03-196819-01-202320-03-2028
E100130-09-200108-01-202530-09-2061
E100207-06-197706-05-200807-06-2037
E100315-09-199422-04-201615-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

Metric20232024
Revenue500000650000
Expenses350000420000
Profit150000230000
Show the result and M code

After

YearRevenueExpensesProfit
2023500000350000150000
2024650000420000230000

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

DateLocationMetricValue
01-05-2025GhaziabadTemperature39.3
01-05-2025GhaziabadHumidity66.4
01-05-2025AurangabadTemperature30.9
01-05-2025AurangabadHumidity39.6
Show the result and M code

After

DateLocationTemperatureHumidity
01-05-2025Ghaziabad39.366.4
01-05-2025Aurangabad30.939.6
01-05-2025Tiruppur6589.9
01-05-2025Hazaribagh89.940.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

ProductJan_SalesFeb_SalesMar_Sales
Product_0133132290
Product_1221158373
Product_2448347147
Product_3158168225
Show the result and M code

After

ProductMonthAmount
Product_0Jan_Sales133
Product_1Jan_Sales221
Product_2Jan_Sales448
Product_3Jan_Sales158

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

CategoryAmount
Apparel14727.73
Stationery2939.74
Stationery7145.19
Electronics10407.51
Show the result and M code

After

CategoryTotal Sales
Apparel204988.91
Electronics177004.82
Furniture148169.63
Groceries108829.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

OrderIDAttributes
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

OrderIDcolorstorage_gbwarranty_yrs
ORD200WhiteSmoke10243
ORD201DeepPink10241
ORD202SlateBlue5121
ORD203DarkSalmon2561

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

DateProductAmount
29-09-2025Veniam7136.78
21-10-2025Temporibus2701.39
10-11-2025Quasi8578.38
22-11-2025Atque20583.57
Show the result and M code

After

DateProductAmount
19-06-2025Temporibus10266.13
30-07-2025Ducimus10965.56
24-08-2025Temporibus12482.94
14-07-2025Praesentium20508.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.

Orders

OrderIDCustomerIDProductIDQuantity
ORD100CUST029106
ORD101CUST0281710
ORD102CUST022155

Products

ProductIDProductNamePrice
1Qui2492.53
2Culpa785
3Velit970.85

Customers

CustomerIDCustomerNameGST_Number
CUST001Chaaya Memon29gDAjG6175U
CUST002Faqid Oza29nttBG5549Q
CUST003Manya Bala29bsXeJ3773c
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)