How to Merge Queries in Power Query in Excel Easily
To merge queries in Power Query in Excel, use the
Merge Queries feature found in the Home tab of the Power Query Editor. Select the two queries you want to combine, choose the matching columns, and pick the join type to combine data based on your needs.Syntax
The basic syntax for merging queries in Power Query uses the Table.NestedJoin function in M language. It looks like this:
Table.NestedJoin(table1, key1, table2, key2, newColumnName, joinKind)
Here’s what each part means:
- table1: The first table you want to merge.
- key1: The column(s) in the first table to match on.
- table2: The second table to merge with the first.
- key2: The column(s) in the second table to match on.
- newColumnName: The name of the new column that will hold the merged data.
- joinKind: The type of join (e.g.,
JoinKind.Inner,JoinKind.LeftOuter,JoinKind.RightOuter,JoinKind.FullOuter).
m
Table.NestedJoin(Source1, {"ID"}, Source2, {"ID"}, "MergedData", JoinKind.LeftOuter)Example
This example shows how to merge two queries named Customers and Orders by matching the CustomerID column. It uses a left outer join to keep all customers and add matching orders.
m
let Customers = Excel.CurrentWorkbook(){[Name="Customers"]}[Content], Orders = Excel.CurrentWorkbook(){[Name="Orders"]}[Content], Merged = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "OrderDetails", JoinKind.LeftOuter), Expanded = Table.ExpandTableColumn(Merged, "OrderDetails", {"OrderID", "OrderDate"}, {"OrderID", "OrderDate"}) in Expanded
Output
A table showing all customers with their matching orders' OrderID and OrderDate columns added; customers without orders show blank in those columns.
Common Pitfalls
- Not matching columns correctly: The columns used to join must have the same data type and matching values.
- Wrong join type: Choosing the wrong join (like inner instead of left outer) can exclude data you want to keep.
- Forgetting to expand merged columns: After merging, you must expand the new column to see the joined data.
m
/* Wrong way: Not expanding merged data */ let Merged = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "OrderDetails", JoinKind.LeftOuter) in Merged /* Right way: Expanding merged data */ let Merged = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "OrderDetails", JoinKind.LeftOuter), Expanded = Table.ExpandTableColumn(Merged, "OrderDetails", {"OrderID", "OrderDate"}, {"OrderID", "OrderDate"}) in Expanded
Quick Reference
| Step | Description |
|---|---|
| Open Power Query Editor | Go to Data tab > Get Data > Launch Power Query Editor |
| Select Merge Queries | In Home tab, click 'Merge Queries' or 'Merge Queries as New' |
| Choose Tables | Pick the two queries to merge from dropdowns |
| Select Matching Columns | Click columns in both tables to match |
| Pick Join Type | Choose join type like Left Outer, Inner, etc. |
| Expand Merged Column | Click expand icon to select columns to add |
| Close & Load | Load the merged query back to Excel |
Key Takeaways
Use the Merge Queries feature in Power Query Editor to combine tables by matching columns.
Choose the correct join type to control which rows appear in the merged result.
Always expand the merged column to see the combined data from the second table.
Matching columns must have the same data type and compatible values for a successful merge.
You can merge queries as new or directly into an existing query depending on your needs.