Foundations › Power BI › Power Query: Prepare the Data
Power Query: Prepare the DataPower Query in Depth: Cleaning and Shaping Data
Power Query is the data-cleaning factory before data enters the model. Each step you take is recorded in the Applied Steps list, and every refresh replays those steps automatically. Clean once, benefit forever.
let
Source = Excel.Workbook(File.Contents("C:\Data\Customers.xlsx"), null, true),
Customers = Source{[Item="Customers",Kind="Sheet"]}[Data],
Promoted = Table.PromoteHeaders(Customers, [PromoteAllScalars=true]),
Trimmed = Table.TransformColumns(Promoted, {{"City", Text.Trim, type text}}),
Proper = Table.TransformColumns(Trimmed, {{"City", Text.Proper, type text}})
in
ProperThis is the M code written for you behind each step. Each line = one step in Applied Steps. You do not have to memorise it, but reading it helps.
| Step | Used for | Example |
|---|---|---|
| Promote Headers | Make the first row the column headers | A file whose first row has Product and Units |
| Changed Type | Set the column type | Text to number or date |
| Trim / Clean | Remove spaces and hidden characters | ' Ahmed ' to 'Ahmed' |
| Replace Values | Replace one value with another | N/A to blank |
| Remove Duplicates | Drop repeated rows | A customer registered twice |
| Split Column | Split one column into several | Full Name to First and Last |
| Merge Queries | Join two tables on a key (like VLOOKUP) | Orders with Products |
| Append Queries | Stack similar tables | The three monthly files |
| Group By | Summarise rows | Total sales per product |
| Unpivot | Turn columns into rows | Month columns into a Month column |
Mission 1: a dirty customers table with extra spaces, cities in mixed case, a blank row and duplicate rows. Clean it with the buttons and watch the code appear.
Clean the customers table: Trim extra spaces, capitalize City, remove the blank row, then remove duplicates. You should end with 5 customers.
Row 1 holds the real column names. Promote headers, then make Units and Price numbers.
The table is wide (one column per month). Unpivot to get: Product | Month | Units.
Split Full Name at the space into two columns: First Name and Last Name.
When you have one file per month with the same columns, use Append to stack them. When you need information from another table through a shared key, use Merge. Then Group By to summarise. Try all three in order.
You have three monthly files with identical columns. Append February and then March onto January (8 rows).
Merge Orders with Products on ProductID and expand the Product and Category columns.
Group sales by product: Product | Total (sum of Amount).
Advanced For advanced readers: a monthly file that only changes its name
Instead of copying cleaning steps 12 times, let Power Query read a whole folder (Get data > Folder) and apply the same steps to every file. The new file then appears automatically in the next refresh.
❓ You have a wide table with columns Jan, Feb, Mar and you want a Month column and a Units column. Use:
Unpivot turns columns into rows and is the opposite of Pivot. Long tables suit the model and analysis best.
Do it in real Power BI Desktop
- Every Power Query step is replayed on each refresh.
- Trim before Merge, Changed Type before calculations.
- Append stacks similar files, Merge brings columns, Group By summarises, Unpivot lengthens a table.
- Avoid long, unexplained step chains; name steps clearly.
Need a real Power BI dashboard for your business?
I design a data model and an executive dashboard fitted to your business and your real data.