The Table.NestedJoin function in Power Query joins two tables on a matching column, like a lookup. Instead of adding the matching values directly, it adds one new column where each cell holds a small table of the matching rows.
It’s the function the Merge Queries button writes for you behind the scenes. In this article, I’ll show you how it works, which join kind to pick, and what you can do with that nested column.
Follow along with the example file
Table.NestedJoin Function Examples.xlsx
Table.NestedJoin Function Syntax
Table.NestedJoin(table1 as table, key1 as any, table2 as any, key2 as any, newColumnName as text, optional joinKind as nullable number, optional keyEqualityComparers as nullable list) as table
- table1 – the main table you want to keep and add information to (the left table).
- key1 – the column in table1 to match on, as a list in curly braces, such as
{"Dept Code"}. To match on more than one column, list them all, such as{"Region","Quarter"}. - table2 – the table you want to look up (the right table).
- key2 – the matching column in table2, in the same format and in the same order as key1. The column names don’t have to be the same as in key1.
- newColumnName – the name of the new column that holds the matching rows from table2.
- joinKind (optional) – which rows to keep. If you leave it out, you get
JoinKind.LeftOuter, which keeps every row of table1. The other common ones areJoinKind.Inner(only rows that have a match),JoinKind.LeftAnti(only rows of table1 with no match), andJoinKind.FullOuter(all rows from both tables). - keyEqualityComparers (optional) – Microsoft says this argument is currently intended for internal use only, so you can skip it.
What it returns: A table with all the columns of table1 plus the new column. Each cell in the new column holds a table with the matching rows from table2, and that table is empty when there’s no match.
The Table.NestedJoin function is part of the core Power Query M library, so it works in every version of Excel that has Power Query (Get & Transform), including Microsoft 365, and in Power BI.
When to Use the Table.NestedJoin Function
Use this function when you need to:
- Bring details from a lookup table into your main table, such as department names for a list of employees
- Keep only the rows that have a match in another table
- Find rows that have no match, such as invoices that haven’t been paid
- Match two tables on more than one column, such as region and quarter
- Count or total the matching rows for each record without expanding them
- Compare two versions of a list to see what was added or removed
Example 1: Merge Two Tables
Let’s start with the basics. Below is an Employees table with each person’s department code, and I want to bring in the department details from a second table.

And here is the Departments table, with the code, department name, and floor for each department.

Both tables are on their own sheets in the example file. In Excel, go to Data > Get Data > From Other Sources > Blank Query, then in the Power Query Editor open the Advanced Editor (Home > Advanced Editor) and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content],
Departments = Excel.CurrentWorkbook(){[Name="Departments"]}[Content],
Merged = Table.NestedJoin(Source,{"Dept Code"},Departments,{"Dept Code"},"Dept Details")
in
Merged
Result: The Employees table with a new Dept Details column that shows Table in every row

Here, the first two steps read the two Excel tables by name. Then Table.NestedJoin matches the Dept Code column of Employees with the Dept Code column of Departments, and puts the matching rows in a new column called Dept Details.
Each Table in that column is the matching row from the Departments table. Click on the white space next to the word Table, and you’ll see a preview of what it holds at the bottom of the editor.
If you click the word Table itself, Power Query opens that one table as a new step. Delete that step in the Applied Steps pane to get back.
I didn’t give a join kind, so it used the default left outer join and kept all five employees. Gerald Campbell’s code, LGL, isn’t in the Departments table, so his Dept Details cell holds an empty table.
This nested column only shows up in the Power Query Editor. If you load this query to a sheet, the Dept Details column is left out.
The next example shows how to pull its columns out.
Example 2: Get the Columns from the Matching Table
Here’s what you’ll do after most merges. Below is a list of course enrollments with each student’s course code, and I want to add the course name and credits next to each student.

The course details are in a Courses table. Notice that its key column is called Code, not Course Code.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Enrollments"]}[Content],
Courses = Excel.CurrentWorkbook(){[Name="Courses"]}[Content],
Merged = Table.NestedJoin(Source,{"Course Code"},Courses,{"Code"},"Course"),
Expanded = Table.ExpandTableColumn(Merged,"Course",{"Course Name","Credits"})
in
Expanded
Result: Each student with the Course Name and Credits columns added (for example, Intro to Programming and 4 for CS101)

Here, key1 is {"Course Code"} and key2 is {"Code"}. The key columns don’t need the same name, they only need to hold matching values.
The Merged step adds a nested Course column, just like in Example 1. Then the Table.ExpandTableColumn function takes the Course Name and Credits columns out of each nested table and adds them as regular columns.
In this result, the rows came back grouped by course code, not in the original order. If you need a specific order, add a step after the expand that uses the Table.Sort function to sort by a column, such as Student.
Example 3: Keep Only the Rows That Have a Match
Sometimes you only want rows that exist in both tables. Below is a timesheet with the hours each person logged against a project code, and I want to keep only the hours logged on active projects.

The active projects are listed in a separate ActiveProjects table. Project PRJ-09 was closed, so it’s not in this list.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Timesheet"]}[Content],
Projects = Excel.CurrentWorkbook(){[Name="ActiveProjects"]}[Content],
Merged = Table.NestedJoin(Source,{"Project Code"},Projects,{"Project Code"},"Project",JoinKind.Inner)
in
Merged
Result: Four timesheet rows. Adam Wright’s 5 hours on PRJ-09 are gone.

Here, JoinKind.Inner keeps a row only when its key is found in both tables. PRJ-09 isn’t in ActiveProjects, so that row is dropped.
This works like a filter based on another table. You can expand the Project column to get the project names, or remove it if you only needed the filter.
Example 4: Find Rows That Have No Match
This next one is a very practical use of Table.NestedJoin. Below is a list of invoices, and I want to find the ones that haven’t been paid yet.

Every payment received is recorded in a Payments table, along with the invoice number it pays.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Invoices"]}[Content],
Payments = Excel.CurrentWorkbook(){[Name="Payments"]}[Content],
Unpaid = Table.NestedJoin(Source,{"Invoice No"},Payments,{"Invoice No"},"Payment",JoinKind.LeftAnti)
in
Unpaid
Result: Two unpaid invoices, INV-302 (Harbor Foods, 850) and INV-304 (Oakridge Labs, 640)

Here, JoinKind.LeftAnti does the opposite of an inner join. It keeps only the rows of the first table whose key is not found in the second table.
Since these rows have no match, the Payment column always holds empty tables. You can remove it with the Table.RemoveColumns function, and you’re left with a clean list of invoices to follow up on.
Follow along with the example file
Table.NestedJoin Function Examples.xlsx
Example 5: Match on Two Columns
Here’s a case where one column isn’t enough to find the right match. Below is a sales table with the actual sales for each region and quarter, and I want to bring in the target for that same region and quarter.

The targets are in a Targets table. Each region appears more than once, so matching on Region alone would pull in the targets for both quarters.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
Targets = Excel.CurrentWorkbook(){[Name="Targets"]}[Content],
Merged = Table.NestedJoin(Source,{"Region","Quarter"},Targets,{"Region","Quarter"},"Target Info"),
Expanded = Table.ExpandTableColumn(Merged,"Target Info",{"Target"})
in
Expanded
Result: A Target column next to each Actual value (50000, 50000, 40000, 40000, and 45000)

Here, both keys are lists of two columns. A row matches only when both the Region and the Quarter are the same, so North Q2 gets the North Q2 target and nothing else.
The columns in key2 are matched by position with the columns in key1. So keep them in the same order, Region first and Quarter second in both lists.
I didn’t give a join kind here, so it’s the default left outer join, which only keeps rows from the first table. That’s why the West Q2 target, which has no sales row, doesn’t show up.
Example 6: Count the Matching Rows
Since each cell in the nested column is a table, you can work with it directly, without expanding it. Below is a list of customers, and I want to count how many support tickets each of them raised.

The tickets are in a Tickets table, where one customer can have several tickets.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Customers"]}[Content],
Tickets = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
Merged = Table.NestedJoin(Source,{"Customer ID"},Tickets,{"Customer ID"},"Matched Tickets"),
Counted = Table.AddColumn(Merged,"Ticket Count",each Table.RowCount([Matched Tickets]),Int64.Type)
in
Counted
Result: A Ticket Count column (1, 3, 0, and 1)

Here, the Matched Tickets cell for Ethan Tucker holds a table with his three tickets. The Table.AddColumn function then runs the Table.RowCount function on that nested table for each row, which gives 3.
Madison Phillips has no tickets, so her nested table is empty and the count is 0. If you had expanded the column instead, you’d get one row per ticket, and you’d have to group the rows again to count them.
Example 7: Total a Column from the Matching Rows
This one is similar, but instead of counting the matching rows, I’ll add up a column in them. Below is a list of club members, and I want to see the total each member has donated.

Each donation is a separate row in a Donations table, with the member ID and the amount.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Members"]}[Content],
Donations = Excel.CurrentWorkbook(){[Name="Donations"]}[Content],
Merged = Table.NestedJoin(Source,{"Member ID"},Donations,{"Member ID"},"Matched Donations"),
Totals = Table.AddColumn(Merged,"Total Donated",each List.Sum([Matched Donations][Amount]),type number)
in
Totals
Result: A Total Donated column (125, null, 150, and 40)

Here, [Matched Donations][Amount] takes the Amount column of the nested table as a list. For Charles Barnes, that’s 50 and 75, and the List.Sum function adds them up to 125.
Tiffany Wood has no donations, so her list is empty, and the List.Sum function returns null for an empty list. That’s different from Example 6, where the Table.RowCount function gave 0 for an empty table.
If you want a 0 there, use List.Sum([Matched Donations][Amount]) ?? 0 instead. The ?? operator replaces a null with the value after it.
Example 8: Match Text When the Capitalization Is Different
Here’s a problem you’ll run into with text keys. Below is a list of people who registered for a webinar, and I want to find the ones who didn’t attend.

The attendance log has the email each person typed at check-in. Some of them used capital letters.

A left anti join on Email (like in Example 4) should do it. But if you run it on these two tables as they are, you get four people back, even though three of them checked in.
That’s because Table.NestedJoin matches text exactly, including capital letters. H.Robinson@example.com doesn’t match h.robinson@example.com, so Hailey shows up as a no-show.
The fix is to convert the emails in both tables to lowercase before the join. Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="Registrations"]}[Content],
Attendance = Excel.CurrentWorkbook(){[Name="Attendance"]}[Content],
LowerReg = Table.TransformColumns(Source,{{"Email",Text.Lower}}),
LowerAtt = Table.TransformColumns(Attendance,{{"Email",Text.Lower}}),
NoShows = Table.NestedJoin(LowerReg,{"Email"},LowerAtt,{"Email"},"Check-in",JoinKind.LeftAnti)
in
NoShows
Result: Two people who didn’t attend, Jennifer Reed and Anna Fisher

Here, the Table.TransformColumns function runs the Text.Lower function on the Email column of each table. Once both sides are lowercase, the three people who checked in match, and only the two real no-shows are left.
Important: Leading or trailing spaces break a match too, so “abc ” with a trailing space won’t match “abc”. Use the Text.Trim function on both key columns the same way as the Text.Lower function above.
Example 9: Compare Two Versions of a List
In this last example, I’ll use a full outer join to compare two lists. Below is last year’s price list for an office supplies store.

And here is this year’s price list. I want to know which products were added, which were removed, and which ones changed price.

Create a new blank query, open the Advanced Editor, and use this code:
let
Source = Excel.CurrentWorkbook(){[Name="OldPrices"]}[Content],
NewPrices = Excel.CurrentWorkbook(){[Name="NewPrices"]}[Content],
Merged = Table.NestedJoin(Source,{"Product"},NewPrices,{"Product"},"New",JoinKind.FullOuter),
Expanded = Table.ExpandTableColumn(Merged,"New",{"Product","New Price"},{"New Product","New Price"}),
Status = Table.AddColumn(Expanded,"Status",each if [Product]=null then "Added" else if [New Product]=null then "Removed" else if [Old Price]<>[New Price] then "Price Changed" else "No Change",type text)
in
Status
Result: A Status column that marks Laptop Sleeve as Added, Monitor Stand as Removed, Desk Lamp and USB Hub as Price Changed, and Wireless Mouse as No Change

Here, JoinKind.FullOuter keeps every product from both lists. The expand step then brings in the new list’s Product column as New Product, along with New Price.
The new name matters. Without it, you’d get an error saying the field ‘Product’ already exists, because the table already has a Product column.
Laptop Sleeve is only in the new list, so its Product and Old Price cells are null. Monitor Stand is only in the old list, so its New Product and New Price cells are null.
The Status column then checks those nulls. A null Product means the item is new, a null New Product means it was removed, and if both exist, it compares the two prices.
Other Excel Articles You May Also Like:
- Table.Join Function in Power Query M
- Table.ExpandRecordColumn
- Table.ColumnNames function
- Table.Combine – Use Table.Combine in Power Query M to stack multiple tables into one by appending rows.
- List.Combine – Use List.Combine in Power Query M to join lists into one.
- Table.Group – Guide to the Table.Group function in Power Query M.
- Excel.Workbook – Use Excel.Workbook in Power Query M to read sheets and tables from a workbook.
- Table.Distinct – Use Table.Distinct in Power Query M to remove duplicate rows.