Search This Blog

Tuesday, May 29, 2018

How to generate all combinations from two separate lists [Pivot Table Trick]

https://chandoo.org/wp/generate-all-combinations-from-two-lists-excel/

Time for a quick but very useful tip. Ever wanted to create all combinations from two (or more) lists? a la Cartesian product of both lists.
Here is a ridiculously simple way to do it.

Make Cartesian product of two tables in Excel

Note: You need Excel 2013 or above for this.
  1. Convert two lists to tables, if not already done.
  2. Select any cell in one of the tables and go to Insert > Pivot Table (Use ALT + NV shortcut)
  3. Make sure to check “Add this data to the Data Model” option before clicking ok.
    add-pivot-to-data-model
  4. From your pivot table field list, switch to ALL view.
    see-all-tables-pivot-table-field-list
  5. Add both (or all fields) to row label area.
  6. Now, change the pivot table layout to “Show in tabular form” and check “Repeat all item labels” option.
    pivot-table-layout-settings
  7. Turn off sub totals & grand totals.
  8. Viola, your cross product is ready. All combinations are generated by Excel for you. Use them as you see fit.
join-combinations-of-two-tables-excel

No comments:

Post a Comment