Why Power Query?#
In the cleaning lesson you fixed Raw_Data by hand. Next month there's a new messy export - and you'd do it all again. Power Query records each cleaning step and replays them on new data with one Refresh.
Power Query is built into Excel (Data → Get & Transform) and into Power BI - the same skills work in both.
Power Query = cleaning का "रिकॉर्डर"। आप एक बार cleaning के कदम करते हैं (spaces हटाना, duplicates हटाना, date ठीक करना...), Power Query हर कदम याद रखता है।
अगले महीने नई file आए, तो बस Refresh दबाइए - सारे कदम अपने-आप दोबारा चल जाएँगे। घंटों का काम सेकंडों में। और यही Power Query आगे Power BI में भी बिल्कुल वैसे ही इस्तेमाल होता है।
The Power Query workflow#
Get Data → Transform in the Power Query Editor → Close & Load → Refresh when data changes
(source) (steps recorded in Applied Steps) (to a table/pivot)
1. Get data#
Data → Get Data → From File (Excel workbook, CSV, Folder, PDF), From Database (SQL Server, MySQL...), From Web, From Table/Range.
For our practice: Data → From Table/Range on the Raw_Data sheet (convert it to a Table first), or Get Data → From File → From Text/CSV → sales_raw.csv.
2. The Power Query Editor#
| Part | What it shows |
|---|---|
| Ribbon | Home, Transform, Add Column, View |
| Preview grid | first rows of the data |
| Column header icons | data type (ABC text, 123 whole number, 1.2 decimal, calendar date) |
| Applied Steps (right) | every step - click one to see the data at that point |
| Formula bar | the M code of the selected step (View → Formula Bar) |
Cleaning Raw_Data with Power Query#
| # | Step | How |
|---|---|---|
| 1 | Remove duplicates | Home → Remove Rows → Remove Duplicates (select all columns first with Ctrl+A) |
| 2 | Trim and fix case in city |
right-click column → Transform → Trim, then Capitalize Each Word |
| 3 | Fix Bangalore | right-click → Replace Values → Bangalore → Bengaluru |
| 4 | Fix segment case |
Transform → Capitalize Each Word |
| 5 | Clean sales |
Replace Values Rs. → (nothing), then , → (nothing); change type to Decimal Number |
| 6 | Fix mixed dates | column icon → Using Locale... → Date, English (India) |
| 7 | Handle blank region |
Merge with a city-region table (see below), or filter blanks for review |
| 8 | Rename the query | Sales_Clean |
| 9 | Close & Load | Home → Close & Load To... → Table (or PivotTable / Only Create Connection) |
Change data types last or deliberately - Power Query auto-adds a "Changed Type" step at import that may guess wrong (e.g. dates in US format). Check the column icons.
Add helper columns#
Add Column tab:
- Date → Year / Month Name / Quarter from
order_date - Column From Examples - type the desired result, Power Query writes the formula (like Flash Fill, but refreshable)
- Conditional Column - IF logic without formulas (e.g. Order size: if sales ≥ 50000 then "Very large"...)
- Custom Column - e.g.
[quantity] * [unit_price]
Reshaping data#
Unpivot - wide to tidy#
A monthly report with columns Region | Jan | Feb | Mar ... → select Region → Transform → Unpivot Other Columns → becomes Region | Attribute (month) | Value. Rename to Month and Sales.
Group By#
Transform → Group By - like a pivot inside Power Query: group by region, sum sales, count rows. Useful to create summary tables that load small.
Combining tables#
Merge Queries (like XLOOKUP / SQL JOIN)#
Add product cost from the Products table:
- Load both
SalesandProductsas queries. - In Sales: Home → Merge Queries → choose Products → click
productin both → Join Kind: Left Outer → OK. - Click the expand icon on the new column → tick
cost_price→ OK.
| Join kind | Keeps |
|---|---|
| Left Outer | all rows from the first table + matches |
| Inner | only matching rows |
| Left Anti | rows from the first table without a match (great for finding missing products!) |
| Full Outer | everything from both |
Append Queries (stack rows)#
Home → Append Queries stacks tables with the same columns - e.g. Sales_2024 + Sales_2025.
Combine a whole folder of files#
The most time-saving trick for monthly reports:
- Save monthly exports (Jan.csv, Feb.csv, ...) in one folder.
- Get Data → From File → From Folder → choose the folder → Combine & Transform.
- Apply cleaning steps once.
- Next month, drop the new file in the folder → Refresh All. Done.
Merge = दो tables को साथ-साथ जोड़ना (columns बढ़ते हैं) - जैसे XLOOKUP या SQL JOIN। Append = एक table के नीचे दूसरी table लगाना (rows बढ़ती हैं) - जैसे जनवरी + फ़रवरी + मार्च का data एक साथ।
From Folder सबसे बड़ा time-saver है: हर महीने की file एक folder में डालो, Refresh दबाओ - सारी files अपने-आप जुड़कर साफ़ हो जाएँगी।
Refreshing#
- Data → Refresh All (Ctrl + Alt + F5) re-runs every query and refreshes pivots.
- Query properties → Refresh data when opening the file.
- If the source file moves, update it in Data → Queries & Connections → right-click → Edit → Source step.
A peek at M code#
Every step is written in Power Query's language, M. You rarely need to write it, but reading it helps:
= Table.ReplaceValue(#"Trimmed Text", "Bangalore", "Bengaluru", Replacer.ReplaceText, {"city"})
= Table.TransformColumnTypes(#"Replaced Value", {{"sales", type number}})
Practice#
Clean sales_raw.csv (download from here) entirely in Power Query using the steps above, add Year and Month columns, then load it to a pivot table.
Check your result
- Rows after Remove Duplicates: 300
- Distinct cities (Transform → check the column filter list): 19
- Blank regions after removing duplicates: 9 (10 in the original file - one blank row was a duplicate). After a Left Outer merge with a city-region table: 0
- The
salescolumn type shows 1.2 (decimal) and the column sums without errors. - Bonus: use a Left Anti merge of Raw products against the Products table - it should return 0 rows (every product is known).
Summary#
- Power Query records cleaning steps and replays them on Refresh.
- Get Data → Transform (Applied Steps) → Close & Load.
- Key transforms: remove duplicates, trim/case, replace values, change type (with locale), add columns, unpivot, group by.
- Merge = join columns (like XLOOKUP); Append = stack rows; From Folder = combine monthly files automatically.
- The same Power Query exists in Power BI.