Sub topic 11
Verifying a Merge: Missing and Duplicated Rows
This chapter assumes you can build a merge on a key column, choose a join kind, and expand the merged column. A merge can finish without errors and still return the wrong number of rows. Counting rows before and after is the fastest check.
What can go wrong
A merge changes row counts in two ways.
- Missing rows. An inner join drops every row with no match. A left outer join keeps all left rows, but unmatched rows get
nullin the expanded columns. - Duplicated rows. If one left row matches several right rows, expanding the column produces one output row per match.
The expected count depends on the join kind and on whether the right table's key is unique.
| Join kind | Expected row count |
|---|---|
| Left outer | At least the left table's rows. Equal if the right key is unique. |
| Inner | Only matched rows. Can be fewer or more than the left table. |
| Left anti | Left rows with no match. Never more than the left table. |
Step 1: Record the row counts
Before merging, select each query in the Queries pane. The Editor shows the row count in the status bar only for a limited preview, so add a check query instead. This query counts rows of the left table:
let
Source = Table.RowCount(Orders)
in
SourceRun the same check on the result of the merge, after the expand step. Compare the two numbers against the table above.
Step 2: Check the right key for duplicates
Duplicated rows come from a right key that repeats. Group the right table by its key and look for counts above 1:
let
Grouped = Table.Group(Customers, {"CustomerID"}, {{"Rows", each Table.RowCount(_), Int64.Type}}),
Repeats = Table.SelectRows(Grouped, each [Rows] > 1)
in
RepeatsIf Repeats is empty, the key is unique and a left outer merge keeps the left row count. If it has rows, decide what the repeats mean. Remove true duplicates with Remove Rows > Remove Duplicates on the key column. If the repeats are valid, such as several orders per customer, aggregate the nested table instead of expanding it.
Step 3: Find the unmatched rows
A left anti join lists the left rows that found no partner. This is the direct way to see what an inner join would lose:
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftAnti)
in
SourceLook at the keys in this output. Common causes are trailing spaces, different case, and a text key compared with a number key. Fix them as described in the key preparation chapter, then run the check again.
Worked example
Suppose Orders has 1,200 rows. After a left outer merge with Customers and an expand, the result has 1,265 rows. The count rose by 65, so some customer keys repeat. The group-by check shows that 60 CustomerID values appear twice in Customers, and some appear three times. Removing duplicates on CustomerID in Customers brings the merge back to 1,200 rows.
A count that matches does not prove the merge is right. A left outer merge can return 1,200 rows with every Name null if no keys matched. Always filter an expanded column for null as well.
Exercise
A left outer merge of 500 Orders rows with Customers returns 540 rows. Name the likely cause and one way to confirm it.
Show answer
The Customers key has repeated values, so some orders matched more than one customer row and the expand produced extra rows. Confirm by grouping Customers on CustomerID with a row count and filtering for counts above 1.