This is a frequently asked and answered question: how can I generate all permutations in Excel:
2011 2016 2017 2017 superuser 2018 2021
and now in 2022 which did not get an answer before it was closed as a duplicate, which is unfortunate because LAMBDA really changes the way that this question would be answered.
I have infrequently had the same need and get frustrated by having to reinvent a complex wheel. So, I will re-pose the question and put my own answer below. I will not mark any submissions as the answer, but invite good ideas. I am sure that my own approach can be improved upon.
Restating the 2022 question
I am trying to create a loop in excel with formulas only. What I am trying to achieve is described below. Let's say I have 3 columns as inputs: (i) Country; (ii) variable; and (iii) year. I want to expand from these inputs to then assign values to these parameters.
Inputs:
Country | Variable | Year |
---|---|---|
GB | GDP | 2015 |
DE | area | 2016 |
CH | area | 2015 |
Outputs:
Country | Variable | Year |
---|---|---|
GB | GDP | 2015 |
GB | GDP | 2016 |
GB | area | 2015 |
GB | area | 2016 |
DE | GDP | 2015 |
DE | GDP | 2016 |
DE | area | 2015 |
DE | area | 2016 |
How can I do that efficiently using Excel?
Expanding the 2018 question
I have three columns, each of which has different kinds of master data as shown below:
Now, I want to have all possible combinations of these three cells - like
aa kk jj
aa kk ff
aa ll jj
aa ll ff
aa mm jj
...
Can this be done with a formula. I found one formula with 2 columns, but i'm not able to extend it to 3 columns correctly
Formula with 2 columns:
=IF(ROW()-ROW($G$1)+1>COUNTA($A$2:$A$15)*COUNTA($B$2:$B$4),"",
INDEX($A$2:$A$15,INT((ROW()-ROW($G$1))/COUNTA($B$2:$B$4)+1))&
INDEX($B$2:$B$4,MOD(ROW()-ROW($G$1),COUNTA($B$2:$B$4))+1))
where G1 is the cell to place the resulting value
Common Requirements
What these have in common is that they are both trying to create an ordered set of permutations from an ordered set of symbols. They both happen to need 3 levels of symbols, but the 2018 question was asking for help to go from 2 levels to 3 and the 2021 question was asking to go from 3 levels to 5. The 2022 question is just asking for 3 levels, but the output needs to be a table.
What if we go up to 6 levels like this?
L1 | L2 | L3 | L4 | L5 | L6 |
---|---|---|---|---|---|
A | F | K | P | U | 1 |
B | G | L | Q | V | 2 |
C | H | R | W | 3 | |
D | X | 4 | |||
E |
This would generate 1'440 permutations.
L1 | L2 | L3 | L4 | L5 | L6 |
---|---|---|---|---|---|
A | F | K | P | U | 1 |
A | F | K | P | U | 2 |
A | F | K | P | U | 3 |
A | F | K | P | U | 4 |
A | F | K | P | V | 1 |
A | F | K | P | V | 2 |
A | F | K | P | V | 3 |
A | F | K | P | V | 4 |
A | F | K | P | W | 1 |
... | ... | ... | ... | ... | ... |
Making a general formula that takes in any number of levels (columns) is hard. Just go through the answers provided - they each required some rocket science and until now, all the solutions had hard-coded limits on the number of columns of symbols. So can LAMBDA give us a general solution?