Sub topic 8
Join kinds and the rows each one keeps
This chapter assumes you can load tables into the Power Query Editor, you know what a key column is, and you have already built a basic merge with the Merge Queries dialog. Here you will learn what each join kind keeps, how to choose between them, and how to predict the row count before you run the merge.
What a join kind decides
A merge matches rows in a first table against rows in a second table by comparing key values. Some rows find a partner and some do not. The join kind tells Power Query which of those rows to keep.
The dialog lists six kinds. The table uses these names:
- Left table: the first query you pick in the Merge dialog.
- Right table: the second query you pick.
- Matched row: a row whose key value appears in both tables.
- Unmatched row: a row whose key value appears in only one table.
| Join kind in the dialog | M constant | Keeps |
|---|---|---|
| Left Outer | JoinKind.LeftOuter | All left rows, plus matching right rows |
| Right Outer | JoinKind.RightOuter | All right rows, plus matching left rows |
| Full Outer | JoinKind.FullOuter | All rows from both tables |
| Inner | JoinKind.Inner | Only matched rows |
| Left Anti | JoinKind.LeftAnti | Only left rows with no match |
| Right Anti | JoinKind.RightAnti | Only right rows with no match |
The dialog default is Left Outer. It is the safest choice for most lookups, because the left table keeps all of its rows.
A small example
Use two small tables. Orders has four rows. Customers has three rows.
| Orders: OrderID | CustomerID |
|---|---|
| 1 | C1 |
| 2 | C2 |
| 3 | C2 |
| 4 | C9 |
| Customers: CustomerID | Name |
|---|---|
| C1 | Ana |
| C2 | Ben |
| C3 | Cai |
Order 4 has no customer, because C9 is not in Customers. Customer C3 has no orders. Matching on CustomerID, the six kinds give these results when Orders is the left table:
| Join kind | Rows returned | Which rows |
|---|---|---|
| Left Outer | 4 | Orders 1, 2, 3, 4. Order 4 has a null customer. |
| Right Outer | 4 | Orders 1, 2, 3 and customer C3 with null order columns. |
| Full Outer | 5 | Orders 1 to 4, plus customer C3. |
| Inner | 3 | Orders 1, 2, 3 only. |
| Left Anti | 1 | Order 4 only. |
| Right Anti | 1 | Customer C3 only. |
Notice that Ben matches two orders. In the right outer and inner results, Ben appears once for each matching order. A merge never collapses rows on its own.
Left outer: the default lookup
A left outer join keeps every row of the left table. When a left row has a match, the right table's matching rows are attached. When it has none, the nested table column is empty and the expanded columns are null.
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Source, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedUse it when the left table is the one you are reporting on and the right table only adds detail. Orders with no matching customer stay visible, with null in Name and Region. That makes bad keys easy to find.
Right outer: the same idea, mirrored
Right outer keeps every row of the right table. It gives the same rows as a left outer join with the two tables swapped, although the column order differs. Most people swap the tables and use left outer so that the "main" table is always on the left. Use right outer when the query order is fixed for another reason.
Full outer: everything from both sides
Full outer keeps matched rows, unmatched left rows, and unmatched right rows. Use it to reconcile two lists, such as a budget and an actual ledger, when you want to see what is missing from either side. After expanding, check both sets of key columns. A row that came only from the right table has a null in every left column, including the key. The key is then available only in the expanded right-hand column.
Inner: only the matches
Inner keeps a row only when its key appears in both tables. It is the strictest kind.
let
Source = Table.NestedJoin(Prices, {"Region", "Product"}, Targets, {"Region", "Product"}, "Targets", JoinKind.Inner)
in
SourceUse inner when unmatched rows are noise, for example when you want only products that have both a price and a target. The risk is silent loss. An inner join does not warn you that rows were dropped, so compare row counts afterward.
Anti joins: finding what has no match
An anti join returns rows from one side that have no partner on the other side. Left anti keeps unmatched left rows. Right anti keeps unmatched right rows.
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftAnti)
in
SourceThis returns customers who never ordered. The nested Orders column is empty in every result row, so you can delete it instead of expanding it. Anti joins are also a fast way to audit a merge. Run a left anti join with the same keys and you get exactly the rows that a left outer join would show with nulls.
How the join kinds relate
The kinds are combinations of three groups of rows: matched, left-only, and right-only.
Read it from the left. Left Outer keeps matched plus left-only rows. Full Outer keeps all three groups. Inner keeps only the matched group, and each anti join keeps only one of the unmatched groups.
Predicting the row count
You can estimate the result before you run it. Let a table's key values be counted per key. For each key that appears in both tables, an inner join produces left count × right count rows. A key that appears twice on each side produces four rows.
- Count matched rows: for each shared key, multiply the two counts.
- Add left-only rows if the kind keeps them (left outer, full outer).
- Add right-only rows if the kind keeps them (right outer, full outer).
In the example, C2 has two orders and one customer, so it gives 2 × 1 = 2 rows. C1 gives 1. Matched total is 3. Left outer adds order 4, giving 4. Full outer adds customer C3, giving 5.
If the right table has duplicate keys, a left outer join can return more rows than the left table has. This is the most common cause of inflated totals after a merge.
Common errors
- Choosing inner by habit. Rows with mistyped or missing keys vanish with no message. Start with left outer, inspect the nulls, then decide.
- Nulls in key columns. Power Query treats a null key as matching another null key in a merge. Two rows with blank keys on opposite sides will pair up. Filter out null keys before merging if that is not what you want.
- Mixed data types. A key stored as number in one table and text in the other does not match. Set both to the same type first.
- Trailing spaces and case differences. The match is case-sensitive and exact by default, so "ab12 " and "AB12" do not match.
- Expecting a left anti join to return right columns. An anti join returns only the columns of the kept side, plus an empty nested column.
Choosing a kind
- Do you want to keep every row of one main table? Use left outer, with the main table on the left.
- Do you need to see mismatches on both sides? Use full outer, then filter for nulls.
- Do you want only rows present in both? Use inner.
- Do you want to list the rows that are missing a partner? Use left anti or right anti.
Exercise
Table A has keys 1, 2, 2, 3. Table B has keys 2, 3, 3, 4. With A on the left, how many rows does each of these joins return: inner, left outer, full outer, left anti?
Show answer
Matched keys: key 2 has 2 rows in A and 1 in B, giving 2 rows. Key 3 has 1 row in A and 2 in B, giving 2 rows. Matched total is 4. Inner returns 4. Left-only rows: key 1, which is 1 row. Left outer returns 4 + 1 = 5. Right-only rows: key 4, which is 1 row. Full outer returns 4 + 1 + 1 = 6. Left anti returns only the left-only rows, so 1.
Exercise
You run a left outer merge of Orders with Customers, and the result has more rows than Orders. What is the likely cause, and which check confirms it?
Show answer
The Customers table probably has duplicate CustomerID values, so each order matches more than one customer row. Group Customers by CustomerID and count rows, then look for counts above 1. Remove the duplicates or aggregate the nested column before expanding.