Sub topic 9
Preparing key columns so rows match
This chapter assumes you can start a merge in the Power Query Editor, know what a join kind is, and understand column data types. Here you learn why a merge fails to match rows that look identical, and how to fix the key columns before you merge.
Why matching fails
A merge compares key values exactly. Power Query treats "AB12", "ab12", and "AB12 " as three different values. The number 101 and the text "101" are different too. When keys differ in any of these ways, the rows count as unmatched. A left outer join then fills the new columns with null, and an inner join drops the rows.
The common causes are:
- Different data types in the two key columns, such as number against text.
- Leading or trailing spaces.
- Different letter case.
- Non-printing characters, often from copied web or system data.
Check the data types first
Look at the icon in each key column header. Both columns must show the same type. If one is text and the other is a whole number, Power Query lets you start the merge but finds no matches. Change the type with Transform > Data Type. If IDs have leading zeros, such as 00123, use text on both sides. Converting to a number removes the zeros.
Trim and normalize text keys
Apply the cleanup to the key column in each query, before the merge. This example trims spaces and converts to uppercase:
let
Source = Table.TransformColumns(Customers, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}})
in
SourceText.Trim removes spaces at both ends. Text.Upper makes the case consistent. Do the same to the matching column in the other query. Cleaning only one side leaves the problem in place.
For the Editor menus, select the column and use Transform > Format > Trim and Transform > Format > UPPERCASE. Use Clean to remove non-printing characters.
Merging on several columns
Sometimes one column is not unique. A product code may repeat across regions. In that case, select several columns in each table and merge on the combination. In the Merge dialog, hold Ctrl and click the column headers in the same order in both tables. The numbered labels show the pairing.
let
Source = Table.NestedJoin(Prices, {"Region", "Product"}, Targets, {"Region", "Product"}, "Targets", JoinKind.Inner)
in
SourceA row matches only when every listed column matches. Order matters: the first column in one list pairs with the first in the other. Each column pair also needs matching types and cleaned values.
A repeatable routine
- Confirm that the key columns have the same data type in both queries.
- Trim and clean text keys in both queries.
- Normalize case in both queries.
- Merge, then compare row counts and look for nulls in the expanded columns.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| All rows unmatched | Text key against number key | Set both columns to the same type |
| A few rows unmatched | Trailing spaces or case differences | Trim and normalize both sides |
| Leading zeros disappear | Key converted to a number | Use text type on both sides |
| Multi-column merge finds nothing | Column order differs between lists | Select columns in the same order |
Exercise
A left outer merge of Orders to Customers returns null in every expanded column. The key column shows a number icon in Orders and a text icon in Customers. What should you do?
Show answer
Change both key columns to the same data type. Use text if the IDs have leading zeros, otherwise use whole number. Then trim spaces on the text side, repeat the merge, and confirm that the nulls disappear.
Exercise
Customers contains the ID "ab12 " and Orders contains "AB12". Write the M step that fixes the Customers key.
Show answer
Table.TransformColumns(Customers, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}}). Apply the same transformation to the Orders key column.