Skip to content

Cross-Joining Tables

This page shows how to cross-join two tables, which pairs every row of one table with every row of the other. The result is also called the Cartesian product.

The example builds a budget table for every store in a portfolio, week by week over part of a year.

There is more than one way to do this. The approach here starts with a small table of budgets, with one record per store, and multiplies it out into a larger table that repeats those budgets for every week.

Start with a simple table of each store’s budget:

An Excel table with columns Store Number, Sales Budget and Margin Budget for stores 1 to 5

A long, narrow table is usually easier to work with, so unpivot the budget columns into rows:

  • Click in the table, then on Excel’s Data tab click From Table/Range.
  • Right-click the Store Number column and choose Unpivot Other Columns.
  • Rename the Attribute column to Budget Item.

The unpivoted query with columns Store Number, Budget Item and Value, giving a Sales Budget row and a Margin Budget row for each store

Then save it without loading it to a sheet:

  • Name the query Store_Budgets.
  • Click Close & Load To, and choose Only Create Connection.
  • Make sure that Add this data to the Data Model is unticked.

The Import Data dialog with Only Create Connection selected and Add this data to the Data Model unticked

A connection-only query can be used by other queries without its data being stored anywhere in the workbook. Loading a query to a sheet makes a copy of its data, which takes up space, so it is worth doing only when that copy is needed. Otherwise, leave the query as a connection only.

The budget is the same every week, and it is needed for the next 20 weeks. The weekly calendar identifies each week by a number in the form YYYYWW, so the numbers jump at the turn of the year from 201952 to 202001, as the red arrow shows.

A column of Week IDs from 201944 to 202011, with a red arrow marking the jump from 201952 to 202001

3. Create the list of week IDs in Power Query

Section titled “3. Create the list of week IDs in Power Query”

Power Query can create a list itself, without importing one from Excel, by writing it in braces. For example, = {1..5} gives the numbers 1 to 5. To build the list of weeks:

  • On Excel’s Data tab, go to Get Data, then From Other Sources, then Blank Query.
  • Rename the query to Weeks.
  • Type this into the formula bar:
= {201944..201952,202001..202011}

The two ranges, separated by a comma, give one list that skips the numbers between 201952 and 202001:

The formula bar showing the list formula, and a list of 20 week numbers from 201944 to 202011

The query is a list at this point, not a table. To turn it into a table, click To Table on the Transform tab, and name the new column Week ID.

The Weeks query as a one-column table called Week ID, with 20 rows

Save this query as a connection only, as well.

The weeks table has 20 rows and the budget table has 10. A cross join pairs every week with the whole budget table, so the result has every combination of the two, 20 × 10 = 200 rows.

A diagram with red lines joining weeks 201944 and 201950 to each of the ten rows of the budget table

  • Open the Weeks query and add a custom column. The formula is just the name of the other query, Store_Budgets.

The Custom Column dialog creating a column called Custom with the formula = Store_Budgets

  • Click OK. Each row of the weeks table now holds the whole budget table as a Table object.

The Weeks query with a Custom column holding a Table in each of its 20 rows

  • Click the expand icon on the new column to produce every combination. The status bar at the bottom left confirms that the query now has 200 rows.

The expanded query with Week ID, Store Number, Budget Item and Value columns, and a status bar reading 4 columns, 200 rows

The two tables are now cross-joined. Close & Load the query to send the result to Excel.

This example follows Matt Allington’s post Cross Join with Power Query on Excelerator BI.