Sub topic 12
Merging queries to look up related data
This chapter assumes you can open the Power Query Editor, read Applied Steps, and set data types. It also assumes you have loaded at least one query from an Excel table or CSV file. A merge is the Power Query equivalent of a lookup: it brings columns from one table into another by matching values in a key column.
What a merge does
A merge combines two queries side by side, using a key column in each. Append stacks rows. Merge adds columns.
Suppose you have an Orders query with one row per order, and a Customers query with one row per customer. Orders holds only a CustomerID. You want the customer name next to each order. A merge matches CustomerID in both queries and brings the name across.
The result of a merge is a new column that holds nested tables. Each cell contains the matching rows from the second query. You then expand that column to pick the fields you want.
Running a merge in the editor
- In the Power Query Editor, select the
Ordersquery. - On the Home tab, choose Merge Queries. Choose Merge Queries as New if you want a separate result query and want to keep
Ordersunchanged. - In the top table, click the
CustomerIDcolumn header. - In the dropdown, pick
Customers, then click itsCustomerIDcolumn header. - Choose a join kind. For a lookup, use Left Outer.
- Click OK.
- Click the expand icon in the new column header. Clear Select All Columns, tick
Name, and clear Use original column name as prefix if you want a clean name.
Each of these actions adds an Applied Step. The M code looks like this:
let
Merged = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customer", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Customer", {"Name"}, {"CustomerName"})
in
ExpandedTable.NestedJoin takes the first table and its key list, then the second table and its key list. It then takes the name for the new column and the join kind. Table.ExpandTableColumn takes the nested column, the fields to pull out, and the new names for those fields.
Choosing a join kind
The join kind controls which rows survive when keys do not match on both sides.
| Join kind | Rows kept | Typical use |
|---|---|---|
| Left Outer | All rows from the first query, plus matches from the second | Lookup. Keeps every order even if the customer is missing. |
| Right Outer | All rows from the second query, plus matches from the first | Same as Left Outer with the roles reversed. |
| Full Outer | All rows from both queries | Reconciling two lists. |
| Inner | Only rows with a match in both | Keep only orders that have a known customer. |
| Left Anti | Rows in the first query with no match in the second | Find orders with no customer record. |
| Right Anti | Rows in the second query with no match in the first | Find customers with no orders. |
Left Outer is the safest default for a lookup. Inner silently drops rows, so a row count that shrinks after the merge is a sign you used the wrong kind or your keys do not match.
Matching on more than one column
Sometimes one column is not enough to identify a row. To match on several columns, select the first column in each table, then hold Ctrl and click the next column in each. Power Query numbers the selected columns, and it pairs them by order. The first selected column on top matches the first selected column below.
In M, the keys are lists:
let
Merged = Table.NestedJoin(Sales, {"Region", "Product"}, Targets, {"Region", "Product"}, "Target", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Target", {"TargetUnits"})
in
ExpandedThe order of the keys matters. {"Region", "Product"} on one side must line up with {"Region", "Product"} on the other.
Common problems
Most merge problems come from keys that look equal but are not. Check these before you suspect the join itself.
- Mismatched data types. A key that is a number in one query and text in the other will not match. Set both to the same type first. The data types chapter covers how.
- Extra spaces and hidden characters.
"C001 "does not equal"C001". ApplyText.TrimandText.Cleanto both keys before merging. - Different letter case. Merges are case sensitive by default.
"north"does not match"North". Convert both columns to the same case, or use the fuzzy matching option described below. - Duplicate keys in the second query. If
Customershas two rows for the same ID, each matching order is repeated once per duplicate when you expand. Your row count goes up. Remove duplicates from the lookup table first, or check for them. - Nulls. Rows with a null key can match each other in a Power Query merge. If both tables have blank keys, you may get unwanted matches. Filter out null keys first.
A quick check is to compare row counts. After a Left Outer lookup against a table with unique keys, the result should have exactly as many rows as the first query.
Fuzzy matching
The merge dialog has an option, Use fuzzy matching to perform the merge. It matches values that are similar, such as "Acme Corp" and "Acme Corporation". You can set a similarity threshold between 0 and 1, where higher values require closer matches.
Fuzzy matching is useful for messy free-text names, but it can create wrong matches. Review the results, and prefer exact matching on clean ID columns whenever you have them.
Keeping merges tidy
- Name the new column something clear, such as
Customer, before expanding. You will not see this name again after you expand it. - Expand only the columns you need. Extra columns make the result harder to read and slower to refresh.
- Set data types on the expanded columns. Expanded columns often come through as type any until you set them.
- Keep the lookup query as Connection only if you do not need it as a separate sheet. The loading chapter explains this option.
A merge depends on both source queries. When you refresh, Power Query re-runs both and re-does the join, so the lookup updates when either table changes.
Exercise
You run a Left Outer merge of Orders (500 rows) against Customers on CustomerID. After expanding, the result has 530 rows. What is the most likely cause, and how do you fix it?
Show answer
The Customers query contains duplicate CustomerID values. Each order that matches a duplicated ID is repeated once per duplicate row when you expand. Remove duplicates on CustomerID in the Customers query before merging, then confirm the result has 500 rows.
Exercise
A Left Outer merge returns null in every expanded column, although both queries clearly contain the ID C001. Name two checks you would make.
Show answer
First, confirm both key columns have the same data type, since text and number never match. Second, trim whitespace and clean hidden characters from both keys with Text.Trim and Text.Clean. Case differences are another possible cause.