Power Query course · Lesson 10 of 17

Merge queries: every join kind explained

Join two queries on matching keys with Merge Queries: Left, Right, Full Outer, Inner, Left Anti and Right Anti, multi-column keys and fuzzy matching.

Intermediate 18 min read

Merge brings columns from one query into another wherever a key matches, like XLOOKUP or VLOOKUP but for whole tables at once. It is how you add customer countries to orders, product categories to sales lines, cost centres to HR records, or find which invoices are missing from the bank statement. Databases call this a join, and Power Query offers six kinds. This lesson shows each one on the same two small tables, so you can see exactly which rows survive.

ABC
1OrderCust IDAmount
2SO-801C011200
3SO-802C02450
4SO-803C05980
5SO-804C012300

Before, left table 'Orders'. C05 does not exist in Customers.

ABC
1Cust IDCustomerCountry
2C01Nordic FoodsNorway
3C02Agora MarketGreece
4C03Baltic TradeLatvia

Before, right table 'Customers'. C03 has no orders.

How to merge

  1. Open the Orders query in the editor.
  2. Choose Home › Combine › Merge Queries › Merge Queries as New (or Merge Queries to add the step to Orders itself).
  3. In the top half, Orders is already selected. Click the Cust ID header to mark it as the key.
  4. In the drop-down below, choose Customers and click its Cust ID header.
  5. Choose the Join Kind (default Left Outer).
  6. Read the message at the bottom, for example The selection matches 3 of 4 rows from the first table. It is your first quality check.
  7. Click OK. A new column named Customers appears, with the word Table in each cell.
  8. Click the expand icon in the Customers header, tick the columns you want (here Country), untick Use original column name as prefix, and click OK.
Power Query M
let
    Source = Table.NestedJoin(Orders, {"Cust ID"}, Customers, {"Cust ID"}, "Customers", JoinKind.LeftOuter),
    Expanded = Table.ExpandTableColumn(Source, "Customers", {"Country"}, {"Country"})
in
    Expanded
Table.NestedJoin adds a column of matching tables; Table.ExpandTableColumn turns the chosen columns into normal columns.

The six join kinds

Left Outer: all from the first, matching from the second

The everyday lookup. Every order is kept; customers' data is added where a match exists, null where it does not. If you are coming from VLOOKUP, this is the one you want.

ABCD
1OrderCust IDAmountCountry
2SO-801C011200Norway
3SO-802C02450Greece
4SO-803C05980null
5SO-804C012300Norway

After, Left Outer: 4 rows. SO-803 gets null because C05 is unknown.

Right Outer: all from the second, matching from the first

The mirror image: every customer is kept, with their orders where they exist. A customer with two orders appears twice; a customer without orders appears once with nulls in the order columns. To see the customer ID for that row you must expand it from the nested table, because the left key column is null there.

ABCDE
1OrderCust IDAmountCustomers.Cust IDCountry
2SO-801C011200C01Norway
3SO-804C012300C01Norway
4SO-802C02450C02Greece
5nullnullnullC03Latvia

After, Right Outer, with Cust ID and Country expanded (prefix kept, then Customers.Country renamed to Country): 4 rows. SO-803 is gone; C03 appears without an order.

Full Outer: everything from both

ABCDE
1OrderCust IDAmountCustomers.Cust IDCountry
2SO-801C011200C01Norway
3SO-802C02450C02Greece
4SO-803C05980nullnull
5SO-804C012300C01Norway
6nullnullnullC03Latvia

After, Full Outer, expanded the same way: 5 rows. Matches are combined; unmatched rows from either side are kept with nulls.

Full Outer is the tool for reconciliations: list the invoices from the accounting system and the payments from the bank in one table, then filter where either side is null.

Inner: only rows that match

ABCD
1OrderCust IDAmountCountry
2SO-801C011200Norway
3SO-802C02450Greece
4SO-804C012300Norway

After, Inner: 3 rows. Unmatched rows from both sides are dropped.

Watch out. Inner joins silently lose data. Here 980 of revenue disappears because one customer ID was missing from the master list. Use Inner deliberately, as a filter (for example, keep only sales of products in a promotion list), not as a lookup.

Left Anti: rows in the first with no match

ABCD
1OrderCust IDAmountCustomers
2SO-803C05980Table

After, Left Anti: only SO-803. The nested Customers table is empty; you can remove that column.

Left Anti is an exception report: orders with unknown customers, employees missing from payroll, invoices not yet paid. Run it before a Left Outer merge to make sure the master list is complete.

Right Anti: rows in the second with no match

ABCDE
1OrderCust IDAmountCustomers.Cust IDCustomers.Customer
2nullnullnullC03Baltic Trade

After, Right Anti (expanded): only C03, the customer with no orders this period.

Useful for 'who did not buy this month?' or 'which products never sold?'.

Join kindRows from OrdersRows from CustomersRows in the example
Left OuterAllMatching4
Right OuterMatchingAll4
Full OuterAllAll5
InnerMatchingMatching3
Left AntiNon-matching onlyNone1
Right AntiNoneNon-matching only1

Duplicates in the lookup table multiply rows

A merge returns every matching row. If Customers accidentally contains C01 twice (say, an old and a new address), each C01 order becomes two rows after expanding, and your revenue total doubles for that customer. A VLOOKUP would have returned the first match only.

Tip. Before merging, check that the key is unique in the lookup table: select Cust ID in Customers and look at View › Column distribution; distinct and unique counts should equal the row count. Or apply Remove Rows › Remove Duplicates on the key in the lookup query.

When keys do not match

  • Types: the key columns must have the same type. A number 1001 does not match the text 1001. Set both to Text for codes.
  • Case: c01 does not match C01. Uppercase both keys first.
  • Spaces and invisible characters: trim and clean both keys (Cleaning text and numbers in Power Query).
  • Leading zeros: 00417 versus 417. Pad with Text.PadStart or convert both to numbers.

Merging on several columns

Sometimes one column is not enough: a price list is valid per Product and Year. In the merge dialog click Product, then Ctrl+click [Cmd+click on Mac] Year in the first table, and do the same in the second table in the same order. Small numbers 1 and 2 appear in the headers to show the pairing.

Power Query M
= Table.NestedJoin(Sales, {"Product", "Year"}, Prices, {"Product", "Year"}, "Prices", JoinKind.LeftOuter)
The key lists are matched by position: Product with Product, Year with Year.

Expand or aggregate

The expand icon offers two modes. Expand brings columns across (one output row per match). Aggregate summarises the matching rows instead, for example the Sum of Amount of all orders per customer when you merge from Customers to Orders. Aggregate never multiplies rows, so it is the safe choice for 'total per key' questions.

Fuzzy matching

When names are typed inconsistently (Nordic Foods AS, Nordic Foods A/S, nordic foods), tick Use fuzzy matching to perform the merge in the dialog. Under Fuzzy matching options you can set the Similarity threshold (from 0 to 1, default 0.8; lower finds more but riskier matches), Ignore case, Match by combining text parts, the Maximum number of matches and a Transformation table that maps known variants to a standard name.

Watch out. Fuzzy matching is a helper, not proof. Always review the matched pairs, ideally by keeping both name columns side by side, before using the result for invoicing or payroll.
Note. Merge works the same in Excel for Windows and Microsoft 365 for Mac. Fuzzy matching is available in current Microsoft 365 builds; older perpetual versions may not show the option.

Check yourself

1. Which join kind keeps every order and adds the country where it exists?

2. Which join kind lists customers who placed no orders, when Orders is the first table?

3. The Customers table lists C01 twice. What happens to C01 orders after a Left Outer merge and expand?

4. The key is the number 1001 in one table and the text 1001 in the other. Will they match?

5. Which join kind would you use to reconcile invoices against bank payments and see gaps on both sides?