Sub topic 13
Building a pipeline that appends and then merges
This chapter assumes you can append and merge two queries, and that you know the join kinds and how to clean key columns. Here you chain those steps into one pipeline that you can refresh and check.
Plan the order first
Append first, then merge. Appending produces one complete fact table. Merging then adds customer columns to every row in a single pass. If you merge first, you must repeat the merge for each yearly table.
The example uses three source queries:
Sales_2023andSales_2024have the same columns, includingCustomerIDandAmount.CustomershasCustomerID,Name, andRegion.
Build the stages as separate queries
Give each stage its own query so you can inspect it. Set the intermediate queries to Connection only (Close & Load To...). Only the final query needs to load to a table.
- Create
AllSaleswith Append Queries as New on the two sales queries. - Create
Customers_Cleanby referencingCustomersand trimming and upper-casingCustomerID. - Create
SalesWithCustomersby mergingAllSaleswithCustomers_Cleanusing a left outer join, then expandingNameandRegion.
A left outer join keeps every sale, even when no customer matches. An inner join would drop those sales without warning.
The M code
AllSales:
let
Source = Table.Combine({Sales_2023, Sales_2024})
in
SourceCustomers_Clean:
let
Source = Table.TransformColumns(Customers, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}})
in
SourceSalesWithCustomers:
let
Cleaned = Table.TransformColumns(AllSales, {{"CustomerID", each Text.Upper(Text.Trim(_)), type text}}),
Merged = Table.NestedJoin(Cleaned, {"CustomerID"}, Customers_Clean, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedBoth sides of the merge get the same cleaning. If you clean only one side, keys such as "ab12 " and "AB12" still fail to match.
Check the result
A left outer merge should return as many rows as AllSales. If Customers_Clean has duplicate CustomerID values, the expand multiplies sales rows. Add a small check query:
let
Before = Table.RowCount(AllSales),
After = Table.RowCount(SalesWithCustomers),
Result = #table(
type table [Before = Int64.Type, After = Int64.Type, Difference = Int64.Type],
{{Before, After, After - Before}}
)
in
ResultA Difference above 0 means duplicate keys in Customers_Clean. To find unmatched sales, filter SalesWithCustomers for null Name. A null can also appear when the customer's name is itself blank, so confirm against the key.
Refresh behavior
A refresh reruns every step in the chain. Adding a Sales_2025 query later means editing the Table.Combine list in AllSales and nothing else. The merge and expand steps are unchanged. If a new source has a differently named column, append adds it as a new column filled with null in the other rows, and the merge keeps working.
Exercise
Your pipeline appends two sales queries, then left-merges with Customers. The final query has 1,000 more rows than AllSales. What is the most likely cause, and how do you confirm it?
Show answer
CustomersClean has duplicate CustomerID values, so each matching sale row is repeated once per duplicate customer row. Confirm by grouping CustomersClean by CustomerID with a row count and looking for counts above 1, then remove the duplicates or aggregate the merged column instead of expanding it.