Sub topic 13
Building a pipeline that appends and then merges
This chapter combines the two operations from earlier chapters into one repeatable pipeline. It assumes you can load data with Data > Get & Transform, know the difference between appending and merging, and have used join kinds and the Expand button. The goal is a chain of queries that stacks yearly sales tables, cleans the keys, adds customer details, and proves it did not lose or duplicate rows.
Why the order matters
Append first, then merge. If you merge each yearly table with the customer list and append the results afterward, you repeat every merge and every key-cleaning step once per table. If a third year arrives, you must copy the whole chain again. Appending first gives you one table to clean and one merge to maintain.
The usual shape of the pipeline is:
- Source queries, one per input table. These stay connection only.
- An append query that stacks the sources and fixes types.
- A cleaned lookup query for the table you will merge in.
- A merge query that joins the stacked table to the lookup.
- A check query that compares row counts.
Step 1: Keep staging queries connection only
Each source and each intermediate query is a stage, not a result. In the Power Query Editor, choose Close & Load To, then select Only Create Connection. Do this for Sales2023, Sales2024, SalesAll, Customers and CustomersClean. Load only the final query, Sales_Enriched, to a worksheet table or the Data Model.
This keeps the workbook small and avoids duplicate copies of the same rows on worksheets. A query can still reference a connection-only query, so nothing is lost.
Step 2: Append and standardize in one query
Append matches columns by name, not by position. If one year calls a column Cust ID and the other calls it CustomerID, the result has both columns, each filled with null for the other year's rows. Rename columns in the source queries before you append, so the append sees identical headers.
Then set data types and clean the key column in the same query:
let
Combined = Table.Combine({Sales_2023, Sales_2024}),
Typed = Table.TransformColumnTypes(
Combined,
{{"OrderID", Int64.Type}, {"CustomerID", type text}, {"OrderDate", type date}, {"Amount", type number}}
),
Keyed = Table.TransformColumns(Typed, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}})
in
KeyedSetting types after the append, rather than in each source, means one place to maintain them. Table.Combine takes the type of a column from the first table that contains it. If the two sources disagree (text in one, number in the other), set the type explicitly as above so the result is consistent.
Check for rows that failed conversion. A date column with a text value such as "n/a" produces an error cell, and error cells can break the later merge or refresh. Use Home > Keep Rows > Keep Errors on the typed step to inspect them.
Step 3: Clean the lookup table
The merge key must be prepared the same way on both sides. Apply the same trim and case rule to Customers, and remove duplicate keys so each customer appears once:
let
Source = Customers,
Cleaned = Table.TransformColumns(Source, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}}),
Deduped = Table.Distinct(Cleaned, {"CustomerID"})
in
DedupedTable.Distinct with a column list keeps the first row for each key. That is only safe if the duplicates are true repeats. If two rows for C102 carry different regions, decide which is correct before you discard one, or the choice becomes a silent business rule.
Step 4: Merge and expand
Merge the stacked table, which holds many rows per customer, into the lookup, which holds one row per customer. Use a left outer join so every sale stays in the result even when no customer matches.
let
Merged = Table.NestedJoin(
Sales_All, {"CustomerID"},
Customers_Clean, {"CustomerID"},
"Customer", JoinKind.LeftOuter
),
Expanded = Table.ExpandTableColumn(Merged, "Customer", {"Name", "Region"}, {"Customer Name", "Region"})
in
ExpandedName the new columns when you expand, as the last argument does here. If Sales_All already has a column called Name or Region, Power Query would otherwise rename the new one with a numeric suffix, or the expand would fail. Putting the many side on the left and the one side on the right is what keeps the row count stable: each sale matches at most one customer.
Step 5: Verify the row counts
A left outer merge should return exactly as many rows as the left table. Build a small check query that proves it:
let
Before = Table.RowCount(Sales_All),
After = Table.RowCount(Sales_Enriched),
Unmatched = Table.RowCount(Table.SelectRows(Sales_Enriched, each [Customer Name] = null)),
Result = #table(
type table [Check = text, Value = number],
{
{"Rows before merge", Before},
{"Rows after merge", After},
{"Rows without a customer match", Unmatched}
}
)
in
ResultRead the output this way:
| Observation | Likely cause | Fix |
|---|---|---|
| After is greater than before | Duplicate keys in the lookup | Deduplicate the lookup, or aggregate before expanding |
| After equals before, many unmatched | Keys differ in type, spaces or case | Re-check the key preparation on both sides |
| After is less than before | Inner join used by mistake | Switch to left outer |
| Unmatched is a few rows | Genuine orphans, such as a deleted customer | Inspect them with a left anti join |
A null Customer Name can also come from a matched customer whose name is blank. If that matters in your data, test a column that is never blank in the lookup, such as the key itself carried through from the lookup side.
To list orphan sales for review, merge in the opposite direction:
let
Source = Table.NestedJoin(Sales_All, {"CustomerID"}, Customers_Clean, {"CustomerID"}, "Customer", JoinKind.LeftAnti)
in
SourceRefresh behavior
When you refresh the workbook, Power Query re-runs the queries in dependency order. Several things follow from the pipeline's design:
- A new year's table is picked up by editing the list in the append step, or by pointing the append at a folder source, so the merge needs no change.
- A referenced query can be evaluated more than once during a refresh, once for each query that uses it. With slow sources such as large files or web calls, this adds time. Keeping the append as the single point of contact with the sources limits the repeats.
- If a source is renamed or a column disappears, the first step that names it fails. Because cleaning happens early, errors show up in the staging queries rather than in the final table.
- Combining sources with different privacy levels can raise a "Formula.Firewall" error. Setting the privacy levels in Data Source Settings, or loading the pieces to staging queries first as above, usually resolves it.
Exercises
Exercise
SalesAll has 10,000 rows. After a left outer merge with CustomersClean and an expand, Sales_Enriched has 10,240 rows. What is the most likely cause, and where should you fix it?
Show answer
The lookup contains duplicate CustomerID values. Each sale that matches a duplicated key is repeated once per matching customer row, which adds rows. Fix it in Customers_Clean by deciding which row is correct and using Table.Distinct on CustomerID, or aggregate the nested table with Table.AggregateTableColumn instead of expanding.
Exercise
Sales2024 has a column named "Cust ID" while Sales2023 has "CustomerID". You append them and then merge on CustomerID. What happens, and what should you have done?
Show answer
The append keeps both columns and fills each with null for the other year's rows, so 2024 sales have a null CustomerID and fail to match in the merge. Rename "Cust ID" to "CustomerID" in the Sales_2024 query before the append.