Table.NestedJoin Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

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

Download

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 are JoinKind.Inner (only rows that have a match), JoinKind.LeftAnti (only rows of table1 with no match), and JoinKind.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.

Employees table with Emp ID, Name and Dept Code

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

Departments table with Dept Code, Department and Floor

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

Employees with a new Dept Details column holding a Table in each 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.

Enrollments table with Student and Course Code

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

Courses table with Code, Course Name and Credits

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)

Each student with Course Name and Credits added from the Courses table

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.

Timesheet with Employee, Project Code and Hours

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

ActiveProjects table with Project Code and Project Name

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.

Inner join keeps four timesheet rows; the PRJ-09 row is dropped

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.

Invoices table with Invoice No, Client and Amount

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

Payments table with Invoice No, Amount Paid and Method

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)

Left anti join returns the two unpaid invoices, INV-302 and INV-304

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

Download

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.

Sales table with Region, Quarter and Actual

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.

Targets table with Region, Quarter and Target

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)

Target column matched on both Region and Quarter next to each Actual value

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.

Customers table with Customer ID and Customer

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

Tickets table with Ticket ID, Customer ID and Issue

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)

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.

Members table with Member ID and Member

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

Donations table with Member ID and 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)

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.

Registrations table with Name and lowercase Email

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

Attendance table with mixed-case emails and Check-in Time

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

Two no-shows after lowercasing both email columns: 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.

Old price list with Product and Old Price

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

New price list with Product and New 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

Status column: Price Changed, No Change, Price Changed, Added and Removed

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:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.