05 · Power Query Basics¶
Power Query (Data → Get & Transform) is Excel's tool for importing, cleaning, and reshaping data through a recorded, repeatable set of steps — instead of manual copy/paste cleanup you'd have to redo every time the source data refreshes.
1. Worked dataset¶
Build this table on a sheet named Raw, A1:C7, then select it and
Insert → Table (name it RawData):
| A | B | C | |
|---|---|---|---|
| 1 | name | region | sales |
| 2 | alan | east | 1200 |
| 3 | PRIYA | East | 900 |
| 4 | sam | WEST | 1500 |
| 5 | Alan | east | |
| 6 | priya | East | 800 |
| 7 | Sam | west | 1100 |
Note the inconsistent capitalization and one blank sales cell — this is realistic messy source data.
2. Load into Power Query¶
- Click any cell inside
RawData, then Data → From Table/Range. This opens the Power Query Editor with the applied step Source already recorded. - Every action taken from here on is recorded as a numbered step in the Applied Steps pane on the right — this is what makes Power Query repeatable: re-running the query on refreshed data replays every step automatically.
3. Cleaning steps¶
- Select the
namecolumn header → Transform → Format → Capitalize Each Word (or Lowercase then Capitalize). This normalizesalan/Alan/PRIYAinconsistencies toAlan,Priya. - Select the
regioncolumn → Transform → Format → UPPERCASE, givingEAST/WESTconsistently, or Lowercase foreast/west— pick one convention and apply it everywhere soEastandeastare no longer treated as two different groups downstream. - Select the
salescolumn → right-click header → Replace Values, replace blank/null with0(or use Transform → Fill → Down if a blank should inherit the row above's value — here a missing sales figure should become0, not inherit a neighbor's value, so use Replace Values,null→0). - Verify: after cleaning, row 5 (originally
Alan, east, <blank>) should showAlan, EAST, 0.
4. Grouping¶
- Select the
regioncolumn → Transform → Group By. Group byregion, New column nameTotalSales, OperationSum, Columnsales. - Manual check: EAST rows are 1200, 900, 0, 800 → sum
2900. WEST rows are 1500, 1100 → sum2600. The grouped table should show two rows:EAST, 2900andWEST, 2600.
5. Load back to Excel¶
- Home → Close & Load (or Close & Load To… to choose a new sheet or PivotTable). This writes the cleaned/grouped result as a new table on a new sheet, linked to the query.
- Refresh: edit a value back in
RawData(e.g. change Sam's West sale from 1100 to 1300), then Data → Refresh All. The query re-runs every recorded step automatically and the grouped WEST total updates from2600to2800— this is the core value of Power Query over manual cleanup: the transformation logic is saved and replayed, not repeated by hand.
Cheat sheet¶
| Task | Ribbon path |
|---|---|
| Load a table into Power Query | Data → From Table/Range |
| Fix text casing | Transform → Format → Capitalize/UPPER/lower |
| Replace blanks/errors | Right-click column → Replace Values |
| Aggregate by category | Transform → Group By |
| Send result back to a sheet | Home → Close & Load |
| Re-run after source changes | Data → Refresh All |
How It Actually Works¶
Power Query is architecturally separate from the worksheet calculation engine — it runs on its own engine (Microsoft's Mashup Engine, using the M language under the hood) that executes a recorded sequence of transformation steps against source data before anything reaches a worksheet cell. Every action you take in the Power Query editor (remove a column, filter rows, change a type) is appended as one more step in an ordered list, each step consuming the output table of the step before it — this is why steps can be reordered, edited, or deleted independently, and why the editor can always show you the exact intermediate table at any step by simply not executing steps after it. Crucially, Power Query uses lazy evaluation: opening the editor and clicking through steps doesn't necessarily re-pull all the source data each time — the engine builds a query plan and can push filtering and column selection back to the source (a database, for example) when possible, only materializing the final result when you click Close & Load. That load step is also where Power Query hands off to the worksheet: it writes a static result table (or a Table connected to the query for future refreshes) — clicking Refresh re-runs the entire M step sequence from the original source, not an incremental update.
Exercise¶
Add a discount column to RawData with values 0.1, 0.05, 0, 0.1,
0.05, 0 for rows 2–7, load it through Power Query, add a custom
column netSales = sales * (1 - discount), then Group By region
summing netSales. Manually verify EAST: 1200*0.9=1080,
900*0.95=855, 0*1=0, 800*0.95=760 → sum 2695.