Sub topic 13
Appending and Merging Queries
This chapter assumes you can connect Power BI Desktop to a CSV or Excel file, read the Applied Steps pane, and set data types. It covers the two ways to combine queries: append stacks rows, and merge adds columns.
Append or merge?
Pick the operation by the shape of the result you need.
| Operation | What it does | Typical use | Result |
|---|---|---|---|
| Append | Stacks tables on top of each other | Monthly files with the same columns | More rows |
| Merge | Joins tables on matching key columns | Adding customer details to orders | More columns |
Appending queries
Append works best when the tables have the same column names and types. On the Home tab, choose Append Queries. Use Append Queries as New to keep the originals untouched and create a third query.
Columns are matched by name, not by position. If one table has Amount and the other has Total, the result has both columns, and each is null for the rows from the other table. Rename columns to match before you append.
The generated M is short:
let
Jan = #table({"OrderID", "Amount"}, {{1, 100}, {2, 250}}),
Feb = #table({"OrderID", "Amount"}, {{3, 80}}),
Combined = Table.Combine({Jan, Feb})
in
CombinedCheck the data types after you append. If Amount is text in one table and a number in the other, the combined column can end up as any type, so set the type again.
Merging queries
Choose Merge Queries (or Merge Queries as New). Select the first table, select the second, then click the key column in each. Both key columns must have the same data type, or no rows will match.
Next, choose a join kind:
- Left Outer: all rows from the first table, plus matches from the second. This is the usual choice for lookups.
- Inner: only rows that match in both tables.
- Full Outer: all rows from both tables.
- Left Anti: rows in the first table with no match in the second. Use it to find orphan records.
The merge adds one column that holds a nested table for each row. Click the expand icon next to its header and select the columns you want. Clear Use original column name as prefix if you don't want prefixed names.
let
Orders = #table({"OrderID", "CustomerID"}, {{1, "C1"}, {2, "C2"}, {3, "C9"}}),
Customers = #table({"CustomerID", "Name"}, {{"C1", "Ada"}, {"C2", "Alan"}}),
Merged = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customer", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Customer", {"Name"}, {"CustomerName"})
in
ExpandedOrder 3 keeps its row, but CustomerName is null because C9 has no match.
Common errors
- No matches after a merge. The keys differ in type, or in hidden spaces or letter case. Trim and set the types on both key columns first. Matching is case-sensitive.
- More rows than expected after a merge. The second table has duplicate keys, so each match creates an extra row. Remove duplicates on the key column of the lookup table first.
- Null columns after an append. The column names did not match.
Exercise
You merge Orders with Customers using Left Outer on CustomerID. Orders has 500 rows, but the result has 530. What is the most likely cause, and how do you fix it?
Show answer
The Customers table has duplicate CustomerID values, so some orders match more than one customer row. Remove duplicates on CustomerID in the Customers query (or in a staging copy of it), then redo the merge.
Exercise
You append a Jan query and a Feb query. The result has columns Amount and Total, with nulls in each. What happened, and what do you do?
Show answer
One query names the column Amount and the other names it Total. Append matches columns by name, so it keeps both. Rename Total to Amount in the Feb query and append again.