Sub topic 8
Removing duplicate rows
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and have already set data types and trimmed text. Those two steps matter here. Duplicate removal compares values exactly, so unclean text produces wrong results.
Why duplicates matter
Duplicate rows inflate totals and break relationships. If a customer table has two rows for the same CustomerID, a one-to-many relationship from that table to a sales table cannot be created. Power BI requires the "one" side to hold unique values. Duplicated fact rows, such as an order loaded twice, inflate sums and counts without raising any error.
Duplicates usually come from one of these sources:
- The same file appended twice, or overlapping extracts from a source system.
- Joins in the source that repeat a row once per match.
- Data entry that produced near-copies, such as
Ada Lovelaceandada lovelace. - History tables that hold several versions of one record.
The first three need removal. The fourth needs a decision about which version to keep.
Profile before you remove
Turn on View > Column distribution. The column header then shows two counts:
- Distinct is the number of different values, with each value counted once.
- Unique is the number of values that appear exactly once.
If Distinct equals the row count, the column has no duplicates. If Unique is lower than Distinct, some values repeat. Check this on the column you intend to treat as a key before you remove anything. Column profiling reads only the first 1,000 rows by default. Switch to Column profiling based on entire data set in the status bar to get accurate counts.
Removing duplicates in the interface
There are two entry points, and they behave differently.
- Whole-row duplicates. Select the table icon in the top-left corner of the preview, then choose Remove Duplicates. Alternatively, press Ctrl+A on the column headers and use Home > Remove Rows > Remove Duplicates. A row is removed only if every column matches an earlier row.
- Key-column duplicates. Select one or more columns, then choose Remove Duplicates. Rows are compared on the selected columns only. The first row for each key is kept, and every other column in the removed rows is discarded.
The second option is the dangerous one. Removing duplicates on CustomerID alone silently drops differences in Email, City, or any other column.
To inspect the duplicates before deleting them, use Home > Keep Rows > Keep Duplicates. It returns only the rows whose selected columns repeat. Check the result, then delete that step or reference the query from a separate one.
The M behind it
Both interface actions generate Table.Distinct. With one argument it compares whole rows:
let
Source = #table(
{"CustomerID", "Name", "City"},
{
{1, "Ada Lovelace", "London"},
{1, "Ada Lovelace", "London"},
{2, "Alan Turing", "Wilmslow"},
{2, "alan turing", "Wilmslow"}
}
),
RemovedDuplicates = Table.Distinct(Source)
in
RemovedDuplicatesThe result has three rows. The exact copy of Ada is removed. The two Alan rows survive because Alan Turing and alan turing differ in case.
With a list of column names as the second argument, only those columns are compared:
Table.Distinct(Source, {"CustomerID"})Comparison is case-sensitive and exact
Table.Distinct treats values as different if they differ in case, in leading or trailing spaces, or in non-printing characters. It also treats null and an empty string "" as different values. Two null values do count as equal.
The fix is to normalize the text first, then remove duplicates. This example trims and lowercases the name, which collapses both Alan rows:
let
Source = #table(
{"CustomerID", "Name", "City"},
{
{1, "Ada Lovelace", "London"},
{1, "Ada Lovelace", "London"},
{2, "Alan Turing", "Wilmslow"},
{2, " alan turing ", "Wilmslow"}
}
),
Normalized = Table.TransformColumns(Source, {{"Name", each Text.Lower(Text.Trim(_)), type text}}),
RemovedDuplicates = Table.Distinct(Normalized)
in
RemovedDuplicatesThis result has two rows, but the names are now all lowercase. If you need to keep the original capitalization, add a helper column that holds the normalized key, run Table.Distinct on that column, and then remove the helper column.
Choosing which row survives
Table.Distinct keeps the first row it meets for each key. For data in a stable order that is predictable. For a history table, "first" rarely means what you want. To keep the most recent version of each record, sort so the newest row comes first, then remove duplicates on the key:
let
Source = #table(
type table [CustomerID = Int64.Type, Email = text, ModifiedDate = date],
{
{1, "[email protected]", #date(2026, 1, 5)},
{1, "[email protected]", #date(2026, 3, 9)},
{2, "[email protected]", #date(2026, 2, 1)}
}
),
SortedNewestFirst = Table.Sort(Source, {{"ModifiedDate", Order.Descending}}),
Buffered = Table.Buffer(SortedNewestFirst),
LatestPerCustomer = Table.Distinct(Buffered, {"CustomerID"})
in
LatestPerCustomerTable.Buffer loads the sorted table into memory and fixes its order. Without it, the engine may evaluate the sort and the distinct step in a way that does not preserve the sort, so the row you keep can change between refreshes. Buffering has a cost on large tables, because the whole table must fit in memory. Use it only where the choice of surviving row matters.
Decision guide
The diagram shows how to pick an approach.
The table summarizes the options:
| Goal | Approach | Risk |
|---|---|---|
| Drop exact copies of rows | `Table.Distinct(Source)` | Near-copies survive if text is unclean |
| Make a key column unique | `Table.Distinct(Source, {"Key"})` | Other columns of dropped rows are lost |
| Keep the newest record | Sort, `Table.Buffer`, then `Table.Distinct` on key | Memory use on large tables |
| Review duplicates first | Keep Rows > Keep Duplicates | Must remove the step afterward |
Placement and performance
Put duplicate removal after the steps that change values: data types, trim, clean, case, and null handling. Placing it before them makes it compare uncleaned values, and it will miss duplicates. Place it before expensive steps such as merges, so those steps process fewer rows.
On relational sources such as SQL Server, a whole-table Table.Distinct can often fold into a SELECT DISTINCT query that runs at the source. Distinct on a subset of columns generally cannot fold, because SQL has no equivalent that keeps the first row. A step that does not fold forces Power Query to load all rows first. Right-click the step and check whether View Native Query is available to see whether folding still applies.
Common errors
- Duplicates remain after the step. The values differ by case, trailing spaces, or invisible characters. Add trim, clean, and case steps before it.
- Row count is lower than expected. Duplicates were removed on too few columns. Check the columns listed in the formula bar.
- A different row is kept after each refresh. The surviving row depends on source order. Sort on an explicit column and buffer the table.
- Relationship still reports duplicates. The key column has
nullvalues that act as one repeated key, or the distinct step was applied to a different query than the one loaded to the model.
Exercise
A Products query has columns ProductID, Name, and LastUpdated. ProductID 7 appears three times with different LastUpdated dates. You want one row per ProductID containing the most recent version. Write the M steps after Source, and explain why Table.Buffer is included.
Show answer
Sort newest first, buffer, then remove duplicates on the key: Sorted = Table.Sort(Source, {{"LastUpdated", Order.Descending}}), Buffered = Table.Buffer(Sorted), Latest = Table.Distinct(Buffered, {"ProductID"}). Table.Distinct keeps the first row it finds per key. Table.Buffer holds the sorted table in memory so the sort order is fixed when Table.Distinct runs. Without it, the row that survives can vary between refreshes.
Exercise
A Customers table has Smith and Smith (with a trailing space) in the Name column, and Remove Duplicates on all columns leaves both rows. Why, and what do you change?
Show answer
Table.Distinct compares values exactly, so the trailing space makes the two values different. Add a Trim step, using Text.Trim in Table.TransformColumns or Transform > Format > Trim, before the Remove Duplicates step. If case also varies, add Text.Lower or Text.Proper.