Table.NestedJoin Function in Power Query

The Table.NestedJoin function is used to combine two tables by joining the rows of the tables based on the equality of the keys. The result is a new table where a column is added containing nested tables of matching rows from the second table.

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          

The function has the following parameters:

ParameterDescription
table1The first table (left table).
key1The key column(s) from table1 used to match rows.
table2The second table (right table).
key2The key column(s) from table2 used to match rows.
newColumnNameThe name of the new column containing the nested tables.
optionalJoinKindThe type of join to perform (optional).

Example:

Power Query M

let
    Customers = Table.FromRecords({
        [CustomerID = 1, Name = "Ashish"],
        [CustomerID = 2, Name = "Alia"],
        [CustomerID = 3, Name = "Jaqueline"]
    }),
    Orders = Table.FromRecords({
        [CustomerID = 1, OrderID = 1001],
        [CustomerID = 1, OrderID = 1002],
        [CustomerID = 2, OrderID = 1003],
        [CustomerID = 4, OrderID = 1004]
    }),
    JoinedTable = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "OrderDetails", JoinKind.LeftOuter),
    return = Table.AddColumn(JoinedTable, "Matched Rows", each Table.RowCount([OrderDetails]))
in
    return 

The output of the above code is shown below:

Table.NestedJoin in Power Query

Result:

CustomerIDNameOrderDetails
1AshishTable with 2 rows (OrderID 1001, 1002)
2AliaTable with 1 row (OrderID 1003)
3JaquelineEmpty table (no matching orders)