Gomaa Intelligence.Data Hub
عربي

Foundations › Power BI › Power Query: Prepare the Data

Power Query: Prepare the Data

Power Query in Depth: Cleaning and Shaping Data

Lesson 3 / 14· 14 min

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.

Power Query (M)
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
    Proper

This 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.

StepUsed forExample
Promote HeadersMake the first row the column headersA file whose first row has Product and Units
Changed TypeSet the column typeText to number or date
Trim / CleanRemove spaces and hidden characters' Ahmed ' to 'Ahmed'
Replace ValuesReplace one value with anotherN/A to blank
Remove DuplicatesDrop repeated rowsA customer registered twice
Split ColumnSplit one column into severalFull Name to First and Last
Merge QueriesJoin two tables on a key (like VLOOKUP)Orders with Products
Append QueriesStack similar tablesThe three monthly files
Group BySummarise rowsTotal sales per product
UnpivotTurn columns into rowsMonth 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:

Do it in real Power BI Desktop

Summary
  • 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.

Request an analytics service ↗
Get your data analyzed by an expert ↗