Sub topic 5
Promoting headers and removing unwanted rows and duplicates
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. You should also know what a table, a row, and a column are in Excel.
Why headers matter
Power Query treats the first row of imported data as ordinary data unless you tell it otherwise. Raw files often arrive with generic names such as Column1 and Column2, and the real column names sit in row one. Later steps such as type changes and Group By refer to columns by name, so fix the headers early.
Promoting headers
On the Home tab, choose Use First Row as Headers. Power Query adds a Promoted Headers step. The CSV example from the importing chapter relies on this step:
let
Source = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.Csv]),
Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
Typed = Table.TransformColumnTypes(Promoted, {{"OrderID", Int64.Type}, {"Amount", type number}})
in
TypedPromoteAllScalars=true makes Power Query convert numbers and dates in the first row to text so they can become column names. Without it, a non-text value in that row may not be promoted.
Promote headers before you set types. Power Query often adds its own type step right after promotion, and that step breaks if the column names change later.
Removing unwanted rows
Exported reports often include title lines, blank rows, or totals. The Remove Rows menu on the Home tab offers these options:
- Remove Top Rows: drops a fixed number of rows from the start. Use it for report titles above the real header row.
- Remove Bottom Rows: drops rows from the end, such as a grand total line.
- Remove Blank Rows: drops rows where every column is empty or null.
- Remove Errors: drops rows that contain an error in the selected columns.
Remove top rows first, then promote headers. If a title line sits above the header row, promoting first turns the title into your column names.
You can also filter with the drop-down arrow on a column header. Clear the check box for a value such as "Total" to exclude those rows.
Removing duplicates
Select one or more columns, then choose Remove Rows > Remove Duplicates. Power Query keeps the first row for each distinct combination of values in the selected columns and drops the rest.
The selection decides what counts as a duplicate. To remove repeated orders, select only OrderID. If you select every column, two rows are duplicates only when all values match.
Comparison is case-sensitive, and a trailing space makes values differ. "North" and "north " are therefore treated as different values. Clean the text first, then remove duplicates.
Order of steps
Common errors
- "The column was not found" after refresh: a source header was renamed. Edit the step or rename the column back.
- Title text as column names: you promoted headers before removing the top rows.
- Duplicates remain: hidden spaces or case differences make values differ. Trim and clean first.
Exercise
A CSV file has two title lines above its header row, and some customer names appear twice as "Smith" and "Smith " (with a trailing space). In what order do you remove the title lines, promote headers, trim text, and remove duplicates, and why?
Show answer
Remove the top 2 rows, promote headers, trim the text, then remove duplicates. Removing the title lines first lets the real header row be promoted. Trimming before removing duplicates makes "Smith" and "Smith " identical, so Power Query can detect them as duplicates.