Sub topic 7
Merging two queries on a key column
This chapter assumes you can load tables into the Power Query Editor, know what applied steps are, and understand the idea of a lookup or join. It covers the basic merge. Join kinds, key cleaning, and verification get their own chapters.
What a merge does
A merge adds columns to one table by matching its rows against another table. You pick a key column in each table. Power Query compares the values and, for each row in the first table, finds the rows in the second table with the same key.
The result does not add the second table's columns directly. It adds one new column that holds a nested table for each row. You then expand that column to bring in the fields you want. The next chapters cover the expand step in detail.
Merging in the editor
Both queries must already exist in the workbook. Connection-only queries work well here, since you only need the final merged result loaded.
- Open the Power Query Editor and select the query that should keep all its rows. Here it is Orders.
- Choose Home > Merge Queries. Use Merge Queries as New if you want to keep the original query unchanged.
- In the top table, click the
CustomerIDcolumn header. It turns highlighted. - Choose Customers in the second dropdown, then click its
CustomerIDcolumn header. - Leave the join kind as Left Outer, which keeps every Orders row.
- Click OK.
A new column named Customers appears at the end of the Orders table. Each cell shows the word Table. Click the expand icon in the column header, select Name and Region, and clear "Use original column name as prefix" if you want clean names.
The first table in a merge is the "left" table. It is the one whose rows you are trying to keep, so choose it with care.
The M code behind it
The editor generates two steps. The first is the merge and the second is the expansion.
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Source, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedTable.NestedJoin takes the left table, its key columns as a list, the right table, its key columns, the name for the new column, and the join kind. The key columns are written as lists, which is why a merge on several columns only needs more names in the braces. Table.ExpandTableColumn takes the table, the nested column, the columns to pull out, and the names to give them.
In a left outer merge, an order whose CustomerID has no match gets null in the expanded Name and Region columns.
Common errors
- Every row is null after expanding. The keys probably have different data types, such as text in one table and numbers in the other. Set both key columns to the same type before merging.
- Some rows do not match. Hidden spaces or different capitalization are the usual cause, because matching is exact and case-sensitive.
- The merge dialog lists no tables. The query must be in the same workbook. A query in an error state may also be missing from the list.
Exercise
In the M code above, what does the "Customers" argument in Table.NestedJoin name, and what does each cell in that column hold before you expand it?
Show answer
It names the new column added to the Orders table. Each cell holds a nested table containing the Customers rows whose CustomerID matches that order's CustomerID. The nested table is empty when there is no match.
Exercise
You merge Orders (left) with Customers (right) using Left Outer. After expanding, 12 rows have null in Name. What are the two most likely causes?
Show answer
Either those orders have a CustomerID that does not exist in Customers, or the keys differ in data type, spacing, or case so they fail to match.