Forum Discussion
kta87
3 years agoHelper I
Create Table with all the possible group combinations
Hi Everyone, Is there any function in power query that can create a table with all the possibile group combinations? I have one table (Table1) with Customer ID, DOC ID and value, and I've man...
nickvanmaele
2 years agoAdvocate II
I have done some more research, and the most elegant method is described on Microsoft Learn on this page.
The above URL works with an example of two tables having only one column each. My example below is showing how to do this using two tables with multiple columns each.
If you have two tables:
- Table A with columns "ID", "Person Name"
- Table B with columns "Task ID", "Task Name"
The set of all possible combinations of two lists or two columns is called the "Cartesian product".
If you want a table with rows that contain all possible combinations of "Person Name" and "Task Name", then do this:
- In Power Query, ensure that you create queries referring to Table A and Table B. Let's call these queries "pqA" and "pqB" respectively. They show the tables identically as they are without any modifications.
- Create a new query by referencing "pqA". Call the new query "pqCartesianProduct". It will initially look identical to "pqA".
- In query "pqCartesianProduct", add a new Custom Column.
- In the Custom column formula field, type "= pqB[Task Name]".
Note: the auto-suggest feature will not help you here. It will try to "correct" you but suggest wrong things. Just go ahead and type the query name and the correct column name in square brackets. Pay attention to case sensitivity. - After clicking done, each row in column "Custom" of query "pqCartesianProduct" will now show "List".
- Expand the Custom column, and choose the option to create new rows.
- Done.