Sub topic 11
Verifying a merge: missing and duplicated rows
This chapter assumes you can already build a merge in the Power Query Editor, pick a join kind, and expand the nested table column. It also assumes you know what a key column is. The goal here is to check a finished merge and catch the two most common failures: rows that disappear and rows that multiply.
Why a merge can silently go wrong
A merge never shows an error when it produces the wrong row count. Power Query only reports a failure when a step cannot run. A merge that drops 200 orders, or turns 1,000 customers into 3,400 rows, runs without any message. The totals in your report are then wrong, and nothing warns you.
Two things go wrong:
- Missing rows. Rows you expected in the result are absent, or they are present but the columns you expanded contain null.
- Duplicated rows. One row in the left table becomes several rows in the result.
Both have predictable causes. You can detect both by comparing row counts before and after the merge.
What each join kind should do to the row count
Start with a prediction. Before you look at the result, decide what row count you expect. The join kind and the key uniqueness set the answer.
| Join kind | Rows in the result |
|---|---|
| Left outer | At least the left row count. Equal if the right key is unique. |
| Right outer | At least the right row count. Equal if the left key is unique. |
| Inner | Only matched rows. Can be fewer than either table, or more if keys repeat. |
| Full outer | All rows from both sides. At least the larger of the two tables. |
| Left anti | Only left rows with no match. Never more than the left row count. |
| Right anti | Only right rows with no match. Never more than the right row count. |
The key phrase is "if the right key is unique". A left outer merge keeps every left row, but if a left row matches three right rows, it appears three times after you expand. That is the main source of duplicated rows.
Step 1: Record the row counts before the merge
Power Query shows the row count only in the status bar at the bottom of the editor, and only when you select a step. For a reliable record, use these steps:
- Select the last applied step of the left query. Note the count shown in the status bar. Power Query may show "Column profiling based on top 1000 rows" instead of the full count. In that case, change the profiling option in the status bar to "Column profiling based on entire data set".
- Do the same for the right query.
- Write both numbers down, with the expected result.
For example, suppose Orders has 1,250 rows and Customers has 300 rows. You run a left outer merge of Orders to Customers. You expect 1,250 rows, because each order has one customer and CustomerID is unique in Customers.
Step 2: Check the result after the merge
Select the merge step and then the expand step, and compare counts at each point.
- The merge step itself (the nested table column, before expanding) never changes the row count of the left table. If the count differs here, you are looking at the wrong step or the wrong join kind.
- The expand step is where duplication appears. Expanding a nested table with several rows creates one output row per nested row.
If the count after expanding is above 1,250, some right-side keys are not unique. If it is below 1,250, you likely used an inner join, or a later filter removed rows.
Finding missing matches
After a left outer merge and expand, a row with null in every expanded column usually means no match was found. Checking this takes three moves.
- Click the column header filter on an expanded column, such as Name.
- Choose to show only null values.
- Inspect the key values in those rows.
The key values you see are the ones that failed to match. Typical causes:
- A trailing space, as in
"AB12 "against"AB12". - A case difference, as in
"ab12"against"AB12". Merges are case sensitive. - A data type difference, such as text
"1001"against number1001. - A real gap, where the customer is simply missing from the Customers table.
Fix the first three in the key column of each source query before merging, as described in the earlier chapter on preparing keys. Do not fix them after the merge.
Listing unmatched rows with an anti join
An anti join gives you the unmatched rows directly, so you can read them as a list. This left anti merge returns customers with no orders:
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftAnti)
in
SourceTo audit the Orders-to-Customers merge, swap the tables. This query returns every order whose CustomerID has no match in Customers:
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftAnti)
in
SourceKeep this as a separate check query set to Connection only. An empty result means every order matched. A non-empty result lists the exact keys to investigate. The row count of this query plus the matched count must equal the Orders row count.
Finding duplicated rows
Duplication comes from repeated keys in the table you merge in. To find the repeats, group the right table by its key and count.
- Select the Customers query, then choose Transform > Group By.
- Group by CustomerID with the operation Count Rows.
- Sort the Count column in descending order.
Any key with a count above 1 will multiply left rows when you expand. The M code that Group By generates looks like this:
let
Source = Customers,
Grouped = Table.Group(Source, {"CustomerID"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
Sorted = Table.Sort(Grouped, {{"Count", Order.Descending}})
in
SortedIf the top row shows a count of 1, the key is unique and duplication is not possible from that side.
Deciding what to do with repeated keys
Repeated keys are sometimes a data error and sometimes valid. Decide which before you change anything.
- Data error. Two Customers rows share one ID because of a double entry. Fix the source, or remove duplicates on the key with Home > Remove Rows > Remove Duplicates. Check which of the duplicate rows is correct first, because Remove Duplicates keeps the first one it finds.
- Valid many-to-one or one-to-many. A customer really has many orders. Expanding is correct only if you want one row per order. If you want one row per customer, aggregate instead.
To aggregate, use the aggregate option on the nested column, or this M step, which returns one row per customer:
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftOuter),
Totals = Table.AggregateTableColumn(Source, "Orders", {{"Amount", List.Sum, "Total Amount"}})
in
TotalsHere the row count after the aggregate step equals the Customers row count, by design. Customers with no orders get null in Total Amount.
A reconciliation routine
The checks above combine into a short routine for any merge.
- Predict the row count from the join kind and key uniqueness.
- Run a Group By count on the key of the table you merge in. Confirm whether it is unique.
- Merge, then compare the row count at the merge step against the left table's count.
- Expand and compare again. Explain any increase by the repeated keys you found.
- Filter an expanded column to null and inspect the unmatched keys. Cross-check against a left anti merge.
- Confirm that matched rows plus unmatched rows equal the left table's row count.
The diagram below shows the decision flow for the row count after expanding.
Common mistakes
- Trusting the preview. The data preview shows only the first rows. A problem in row 40,000 is invisible there. Use the row count and the null filter instead.
- Checking only the final query. Check at the merge step and the expand step separately. This tells you which step caused a change.
- Fixing keys after the merge. Cleaning the key inside the merged result does not rerun the match. Clean the source queries.
- Removing duplicates on the whole row. If rows differ in any other column, they are not duplicates by full-row comparison. Remove duplicates on the key column only, after you have decided which row to keep.
- Using the wrong profiling mode. The default "top 1000 rows" mode reports counts for a sample, not for the whole table.
Exercise
You run a left outer merge of Orders (1,250 rows) to Customers (300 rows) on CustomerID. After expanding the Customers column, the result has 1,310 rows. What is the likely cause, and which query would you build to confirm it?
Show answer
Some CustomerID values appear more than once in Customers, so some orders matched several customer rows and were repeated when the nested table was expanded. Confirm it by grouping Customers by CustomerID with Count Rows and sorting the count in descending order. Any count above 1 identifies a repeated key. The 60 extra rows come from those repeats.
Exercise
After a left outer merge, 45 rows have null in the expanded Name column. List three causes you would check in the key values, and name the join kind that lists these rows directly.
Show answer
Check for a trailing or leading space in the key, a case difference such as "ab12" against "AB12", and a data type mismatch such as text "1001" against number 1001. A real missing customer is a fourth possibility. A left anti join of Orders against Customers returns exactly the unmatched orders.