Sub topic 13
Appending and Merging Queries
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. Those topics come from earlier chapters. Here you will combine tables in two ways: stacking rows (append) and adding columns by matching a key (merge).
Append and merge solve different problems
- Append stacks tables on top of each other. Use it when the tables describe the same kind of thing, such as monthly sales files with the same layout. The result has more rows.
- Merge joins tables side by side by matching key columns. Use it when one table holds facts (orders) and another holds descriptions (customers). The result has more columns.
Both commands are on the Home tab of the editor. Each one adds a step to the query and writes M code, so you can read and adjust what it produces.
Appending queries
Select Append Queries on the Home tab. Choose Two tables or Three or more tables, then pick the tables. Power Query lets you append into the current query or into a new query with Append Queries as New.
The generated step uses Table.Combine:
let
Jan = #table({"OrderID", "Amount"}, {{1, 100}, {2, 50}}),
Feb = #table({"OrderID", "Amount"}, {{3, 75}, {4, 20}}),
Combined = Table.Combine({Jan, Feb})
in
CombinedThe result has four rows and the same two columns. Table.Combine takes a list of tables, so you can add more tables to the list.
Columns are matched by name
Append lines up columns by name, not by position. The match is case-sensitive, so Amount and amount are different columns. If a name appears in only some tables, the result keeps the column and fills the missing cells with null.
let
Jan = #table({"OrderID", "Amount"}, {{1, 100}, {2, 50}}),
Feb = #table({"OrderID", "Amt"}, {{3, 75}, {4, 20}}),
Combined = Table.Combine({Jan, Feb})
in
CombinedThis returns three columns: OrderID, Amount, and Amt. Amount is null for the February rows, and Amt is null for the January rows. The usual fix is to rename Amt to Amount in the February query before appending.
Types after an append
If the same column has different types in the source tables, for example number in one and text in another, the combined column can end up as the any type or hold values you did not expect. Set the data type again on the appended result, and fix the individual sources first if values fail to convert.
Appending many files
If the tables are files in one folder, you do not append each by hand. The Folder connector and its Combine Files option generate a helper query and a function that applies your cleaning steps to every file. Clean one sample file, and the same steps run on all of them. Later files with different column names cause the same null-column problem described above.
Merging queries
Select Merge Queries on the Home tab. In the dialog:
- Pick the first table (the top one) and click its key column.
- Pick the second table and click its matching key column.
- Choose a Join Kind.
- Select OK.
The result adds one new column, named after the second table. Each cell holds a nested table with the matching rows. Click the expand icon in the column header to choose which columns to bring in, and clear Use original column name as prefix if you do not want prefixed names.
let
Orders = #table(
{"OrderID", "CustomerID", "Amount"},
{{1, "C1", 100}, {2, "C2", 50}, {3, "C9", 75}}
),
Customers = #table(
{"CustomerID", "Name"},
{{"C1", "Ada"}, {"C2", "Alan"}}
),
Merged = Table.NestedJoin(
Orders, {"CustomerID"},
Customers, {"CustomerID"},
"Customers", JoinKind.LeftOuter
),
Expanded = Table.ExpandTableColumn(Merged, "Customers", {"Name"}, {"CustomerName"})
in
ExpandedThe dialog produces two steps: Table.NestedJoin creates the nested column, and Table.ExpandTableColumn flattens it. Order 3 has customer C9, which does not exist in Customers, so its CustomerName is null.
Join kinds
| Join kind | M value | Rows in the result |
|---|---|---|
| Left Outer | `JoinKind.LeftOuter` | All rows from the first table, plus matches from the second |
| Right Outer | `JoinKind.RightOuter` | All rows from the second table, plus matches from the first |
| Full Outer | `JoinKind.FullOuter` | All rows from both tables |
| Inner | `JoinKind.Inner` | Only rows that match in both tables |
| Left Anti | `JoinKind.LeftAnti` | Rows from the first table with no match in the second |
| Right Anti | `JoinKind.RightAnti` | Rows from the second table with no match in the first |
Left Outer is the default and the most common choice for adding descriptive columns to a fact table. Anti joins are useful for cleaning. A Left Anti join of orders against customers lists orders whose customer is missing, which is a quick data quality check.
Why a merge returns the wrong number of rows
- Duplicate keys in the second table. If
Customerscontained two rows forC1, eachC1order would appear twice after expanding. Remove duplicates on the key in the lookup table first. Use the duplicates techniques from the earlier chapter. - Text that looks equal but is not. Matching is case-sensitive and counts spaces.
"c1 "does not match"C1". Trim and fix case on both key columns before merging. - Different data types. A key stored as number in one table and text in the other will not match. Set both columns to the same type first.
- Null keys. Rows whose key is null match other null keys in a merge. Remove or handle null keys before joining if that is not what you want.
Fuzzy matching
The merge dialog has an option, Use fuzzy matching to perform the merge. It matches keys that are similar rather than identical, which helps with typos or inconsistent spellings. It also has a similarity threshold setting and can produce wrong matches. Use it only when you cannot clean the keys directly, and review the results.
Merge on several columns
To match on more than one column, hold Ctrl and select the columns in the same order in both tables. The M code then lists several columns, for example {"Region", "ProductID"}. The order of columns in each list decides how they pair, so keep it consistent.
Append or merge into a new query
Both commands have an as New variant. It leaves the source queries unchanged and creates a third query with the combined result. This keeps the sources available for other uses and makes the combination easy to find. Chapter 14 covers how to organize such source and result queries. Make sure the sources are still loaded or staged the way you intend, because each source query that is not disabled for load adds a table to the model.
Summary of decisions
- Same layout, more rows: append.
- Look up extra columns by key: merge, usually Left Outer.
- Clean and type the key columns, and the names of appended columns, before combining.
- Check the row count after every merge. A left merge with a unique lookup key should keep the first table's row count.
Exercise
You merge Orders (1,000 rows) with Products using a Left Outer join on ProductID, then expand the Name column. The result has 1,040 rows. What is the most likely cause, and what should you do?
Show answer
Products contains duplicate ProductID values, and each duplicate multiplies the matching order rows. Go back to the Products query and remove duplicates on ProductID, after trimming and standardizing the key. Then redo the merge and confirm the result has 1,000 rows.
Exercise
You append Jan and Feb. The result has a column Amount and a column Amt, each with nulls for some rows. Why does this happen, and how do you fix it?
Show answer
Append matches columns by name, and the February table calls the column Amt. Power Query keeps both names and fills the missing cells with null. Rename Amt to Amount in the February query (or in a step before the append), then append again.
Exercise
Which join kind shows orders whose CustomerID has no row in Customers, and which M value does it use?
Show answer
Left Anti, written JoinKind.LeftAnti, with Orders as the first table and Customers as the second.