Sub topic 8
Removing duplicate rows
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. Earlier chapters covered text cleaning and types. Duplicates come after them for a reason, as explained below.
What counts as a duplicate
Power Query treats two rows as duplicates when the selected columns hold equal values. If you select the whole table, every column must match. If you select one column, such as OrderID, rows are duplicates when that column matches, even if other columns differ.
Select the columns, then choose Home > Remove Rows > Remove Duplicates. The editor generates a Table.Distinct step. Right-click a column header and choose Remove Duplicates to do the same for that column alone.
Clean first, then deduplicate
Comparison is exact. It is case-sensitive, and it counts spaces. "Bob Jones", "BOB JONES" and "Bob Jones " are three different values. If you remove duplicates before cleaning text, near-duplicates survive.
The order that works is:
- Set data types.
- Trim, clean, and fix case.
- Handle nulls.
- Remove duplicates.
The example below follows that order. It trims and capitalizes names, fills null regions, and then removes duplicates. It also converts Amount to a number and replaces the resulting error with 0.
let
Source = #table(
{"Customer", "Region", "Amount"},
{
{" alice smith ", "West", "120"},
{"BOB JONES", null, "abc"},
{"BOB JONES", null, "abc"}
}
),
CleanedText = Table.TransformColumns(Source, {{"Customer", each Text.Proper(Text.Trim(Text.Clean(_))), type text}}),
FilledRegion = Table.ReplaceValue(CleanedText, null, "Unknown", Replacer.ReplaceValue, {"Region"}),
RemovedDuplicates = Table.Distinct(FilledRegion),
ChangedType = Table.TransformColumnTypes(RemovedDuplicates, {{"Amount", type number}}, "en-US"),
ReplacedErrors = Table.ReplaceErrorValues(ChangedType, {{"Amount", 0}})
in
ReplacedErrorsTable.Distinct(FilledRegion) has no column list, so it compares entire rows. The two Bob Jones rows are identical, so one remains. To deduplicate on specific columns, pass a list: Table.Distinct(FilledRegion, {"Customer"}).
Which row is kept
Table.Distinct keeps the first row it meets for each key and drops the rest. If you need the newest record, sort first, for example by date descending, and then remove duplicates. Power Query can reorder rows in later steps, because it may evaluate the data lazily. To make the choice stable, wrap the sorted table in Table.Buffer before Table.Distinct.
let
Source = #table(
{"CustomerID", "UpdatedOn"},
{{1, #date(2026, 1, 5)}, {1, #date(2026, 3, 9)}, {2, #date(2026, 2, 1)}}
),
Sorted = Table.Buffer(Table.Sort(Source, {{"UpdatedOn", Order.Descending}})),
Latest = Table.Distinct(Sorted, {"CustomerID"})
in
LatestCheck before you delete
Use data profiling to see whether a column should be unique. In the column distribution, the count of distinct and unique values tells you how many rows repeat. If a column called OrderID shows fewer distinct values than rows, you have repeated keys.
Look at what you will remove. Key columns can match while amounts differ. Removing those rows silently discards data, which is not the same as dropping an exact copy.
Common errors
- Whole-row comparison fails because of hidden spaces or case differences. Clean text first.
- Deduplicating on one column removes valid rows that share only that value.
- A blank key is a null. Nulls compare as equal, so all rows with a null key collapse into one.
Exercise
A Customers table has rows "Dana Lee" and "dana lee " with the same CustomerID. You run Remove Duplicates on all columns and both rows remain. Why, and what do you change?
Show answer
The comparison is exact, so the different case and the trailing space make the rows distinct. Add a text-cleaning step first, with Text.Trim and Text.Proper, and then run Remove Duplicates. Alternatively, deduplicate on CustomerID only, after confirming that it should be unique.
Exercise
You want one row per CustomerID, keeping the most recent UpdatedOn. Which steps do you use?
Show answer
Sort UpdatedOn in descending order, wrap the sorted table in Table.Buffer so the order is stable, then run Table.Distinct on the CustomerID column.