Table.AddJoinColumn Power Query Guide and Usage
In this article
The Table.AddJoinColumn function in Power Query (M language) allows you to create a new column in a table where each cell contains a nested table of matching rows from another table. Unlike a standard Merge operation that flattens data or requires immediate expansion, this function preserves the relationship as a Table object within the cell, enabling complex aggregations, conditional filtering, or nested transformations before the data is ever expanded into the main view.
What You'll Need
- Microsoft Excel (with Power Query) or Power BI Desktop.
- Two distinct tables already loaded into the Power Query Editor.
- Basic familiarity with the Formula Bar and the Advanced Editor.
- A common key column (ID, Email, SKU, etc.) present in both tables to facilitate the join.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify Your Tables: Ensure you have your primary table (the one receiving the new column) and your secondary table (the source of the data being joined). For this example, assume Table A is "Customers" and Table B is "Orders".
- Insert a Custom Column: While the primary table is selected in the Queries pane, go to the Add Column tab on the ribbon and select Custom Column.
- Enter the Function Syntax: In the Custom Column formula box, type the
Table.AddJoinColumnfunction. The basic syntax is:Table.AddJoinColumn(table, key1, otherTable, key2, newColumnName, joinKind). - Define the Arguments:
- table: This is the current table. In a Custom Column dialog, you usually refer to the previous step name (e.g.,
#"Previous Step Name"). - key1: The name of the column in the primary table used for matching (e.g.,
"CustomerID"). - otherTable: The name of the second table you are joining from (e.g.,
Orders). - key2: The name of the matching column in the second table (e.g.,
"CustomerID"). - newColumnName: The name you want for the resulting column (e.g.,
"CustomerOrders"). - joinKind: The type of join. Use
JoinKind.LeftOuterfor a standard join where all rows from the first table are kept.
- table: This is the current table. In a Custom Column dialog, you usually refer to the previous step name (e.g.,
- Apply and Validate: Click OK. You will see a new column where every cell says "Table". Do not click the expand icon (two arrows) yet if you intend to perform calculations on the nested table first.
- Perform Nested Transformations (Optional): If you want to count the rows in the nested table instead of expanding them, you can add another Custom Column using
Table.RowCount([CustomerOrders]). This allows you to see how many orders each customer has without duplicating the customer rows. - Expand the Data: If you do need the actual data, click the expand icon in the column header, select the specific columns you want from the joined table, and choose whether to aggregate or expand to new rows.
Common Mistakes
One of the most frequent errors is confusing Table.AddJoinColumn with Table.NestedJoin. While they appear similar, Table.NestedJoin is the function typically generated by the user interface when you use the "Merge Queries" button. Table.AddJoinColumn is often used in more programmatic M scripts to append a join to an existing table structure without replacing the entire table step. Using the wrong one can lead to syntax errors if you are manually editing the M code in the Advanced Editor.
Another common mistake is neglecting data type alignment. If the key column in the first table is a "Text" type and the key column in the second table is an "Integer" type, the join will fail to find matches. Power Query does not automatically cast types during a join; it will simply return empty tables in the new column. Always ensure your joining keys have identical data types before applying the function.
Users also frequently forget that the joinKind argument is case-sensitive. Writing joinkind.leftouter instead of JoinKind.LeftOuter will result in a "Token Literal expected" or "Identifier not found" error. M is a case-sensitive language, and these constants must be written exactly as defined in the library.
Tips
To optimize performance, filter your secondary table as much as possible before performing the join. If the secondary table contains millions of rows but you only need a subset of columns or a specific date range, apply those filters first. This reduces the memory overhead required to create the nested tables in the primary table.
Use the nested table structure to create "Summary Columns." Instead of expanding a join and then grouping the data (which can be slow), use Table.AddJoinColumn and then use Table.Sum or Table.Average on a specific column within that nested table. For example, List.Sum([CustomerOrders][OrderAmount]) will give you the total spend per customer in a single step without increasing the row count of your primary table.
When working with very large datasets, consider using Table.Buffer on the secondary table before the join. By wrapping the secondary table in Table.Buffer(TableName), you force Power Query to load that table into memory once, preventing the engine from re-scanning the source table for every single row of the primary table, which can drastically speed up the refresh time.
Troubleshooting
If the resulting column contains only empty tables despite knowing that matches exist, first check for leading or trailing spaces in your key columns. Use the "Trim" transformation (Transform > Format > Trim) on both tables' key columns. Hidden spaces are the most common reason for join failures in Power Query.
If you encounter a "Formula.Error: The column 'ColumnName' of the table wasn't found," double-check the spelling of your key columns. Remember that column names are case-sensitive. If your column is named "customerid" but you wrote "CustomerID" in the function, the join will fail.
If the query becomes extremely slow or freezes (the "Evaluating..." message persists indefinitely), you may be experiencing "Data Mashup" performance issues. Try to reduce the number of columns in the secondary table to only the key and the necessary data columns. The more data Power Query has to carry inside those nested tables, the more RAM it consumes.
If the function returns an error regarding the joinKind, ensure you have not omitted this argument. While some functions have defaults, being explicit with JoinKind.LeftOuter or JoinKind.Inner ensures the engine knows exactly how to handle non-matching rows.
Frequently Asked Questions
How is Table.AddJoinColumn different from Merge Queries?
Merge Queries is the UI-driven process that typically uses Table.NestedJoin. Table.AddJoinColumn is a specific M function that adds a join result as a new column to an existing table, often used when building complex custom functions or dynamic scripts where the table structure needs to be modified incrementally.
Can I perform a Many-to-Many join with this function?
Yes. If the key column in both tables contains duplicate values, Table.AddJoinColumn will create a nested table containing every matching row from the second table for every matching row in the first, effectively creating a many-to-many relationship.
Will this function increase the number of rows in my main table?
No, not initially. The function adds a new column where each cell is a table. The row count of your primary table remains the same. The row count only increases if you later "Expand" that nested table to new rows.
Can I use this function for an Inner Join?
Yes. By changing the joinKind argument to JoinKind.Inner, the function will only return matches. However, note that unlike a standard Inner Join in SQL, Table.AddJoinColumn will still keep all rows from the original table but will leave the joined column empty (an empty table) for rows that don't match.
Does this function support joining on multiple columns?
Not directly within a single Table.AddJoinColumn call, as it expects a single column name for the keys. To join on multiple columns, you should create a "Composite Key" in both tables (e.g., merge First Name and Last Name into one column) and then use that composite column as the key for the join.