Sub topic 8
Join kinds and the rows each one keeps
This chapter assumes you can load tables into the Power Query Editor and have merged two queries on a single key column. Here you will learn what each join kind keeps, so you can pick the right one before you run the merge.
What a join kind decides
A merge matches rows from a first table to rows from a second table using key columns. The join kind decides what happens to rows that find a match and rows that do not. Power Query offers six kinds in the Merge dialog.
In the examples below, Orders is the first table and Customers is the second. Some orders have a customer ID that is missing from Customers. Some customers have never ordered.
The six join kinds
| Join kind | Rows kept | Typical use |
|---|---|---|
| Left outer | All rows from the first table, plus matching rows from the second | Add customer details to every order |
| Right outer | All rows from the second table, plus matching rows from the first | Same as left outer with the tables swapped |
| Full outer | All rows from both tables | Compare two lists and see every difference |
| Inner | Only rows that match in both tables | Keep only valid, matched records |
| Left anti | Rows from the first table with no match in the second | Find orders with an unknown customer |
| Right anti | Rows from the second table with no match in the first | Find customers who never ordered |
Where a row has no partner, the columns from the other table are null after you expand the merged column.
Left outer: the default choice
Left outer is the default in the Merge dialog. It never drops a row from the first table, so it is the safest way to enrich data.
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Source, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedEvery order stays. Orders with an unknown customer show null in Name and Region.
Inner: matches only
An inner join drops any row that lacks a partner, from either table. Use it when unmatched rows are noise. Be careful, because rows disappear silently. The next chapters cover how to check for that.
Anti joins: find what does not match
An anti join returns rows from one table only, and adds no usable data from the other. The merged column it creates holds empty tables. You can remove it.
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftAnti)
in
SourceThis returns the customers who have no orders. The same result with the tables reversed uses right anti.
Common errors
- Choosing inner when you meant left outer. Your row count drops and nothing warns you.
- Expecting a right outer to keep the first table. Right outer keeps the second table. Check which query you picked first.
- Forgetting that duplicates multiply rows. If a key appears twice in the second table, a left outer join returns the first-table row twice after expanding.
- Using an anti join and then expanding. The nested column is empty, so there is nothing to expand.
Exercise
You have a Products table and a Sales table. You want a list of products that have never been sold. Which join kind do you use, and which table is first?
Show answer
Use a left anti join with Products as the first table and Sales as the second. It returns only the Products rows that have no matching row in Sales. A right anti join with Sales first and Products second gives the same result.