Search code examples
google-sheetsgoogle-sheets-formulaformula

How to create multiple sequences of varying lengths with arrayformula


Let's say I have a list of 3 values in a column A. The amount of values is arbitrary, so there can be more. So for example:

 |A|B
-+-+-
1|4|
2|2|
3|3|

In the column B, I would like to get a sequence, which would include increasing sequences with the lengths of the values given in column A. So the above example would be:

The expected sequences:
4 = 1,2,3,4
2 = 1,2
3 = 1,2,3

The expected table:

  A|B
---+-
1|4|1
2|2|2
3|3|3
4| |4
5| |1
6| |2
7| |1
8| |2
9| |3

I am currently achieving this by having the following function in B1: ={SEQUENCE(A1);SEQUENCE(A2);SEQUENCE(A3)} but as you can see, if I were to add another value to the column A, I would need to modify the said function. How can I achieve this dynamically?


Solution

  • Another approach (for google sheets). In B1

    =Arrayformula(transpose(split(textjoin("~", 1, if(column(1:1)<=A1:A, column(1:1),)), "~")))
    

    enter image description here