Search code examples
google-sheetsgoogle-sheets-formulatransposearray-formulasgoogle-sheets-query

Repeat rows N of times based on a given if condition in google sheets


So I have a column that contains the following rows

7-A
9-B
10-C

What I want to do is "If the first digit from left is 7 repeat 6 times, if the first digit from left is 9 repeat 8 times, if 10 repeat 9 times,"

I was able to do that with the below formula however it only works with one repetition setting

=sort(trim(transpose(split(query(arrayformula(REPT(A2:A600&"*",6)),,9999),"*"))),1,0)

But I need to change the repetition based on the mentioned condition

If there's a way to do it with script editor that would be also great

Appreciate it


Solution

  • try:

    =ARRAYFORMULA(QUERY(FLATTEN(IFNA(SPLIT(REPT(IF(A2:A="",,A2:A&"♀"), 
     REGEXEXTRACT(A2:A, "\d+")-1), "♀"))), "where Col1 is not null", 0))
    

    enter image description here


    update:

    =ARRAYFORMULA(QUERY(FLATTEN(IFNA(SPLIT(REPT(IF(A2:A="",,A2:A&"♀"), 
     VLOOKUP(REGEXEXTRACT(A2:A, "\d+")*1, 
     {5, 6; 6, 6; 7, 6; 8, 6; 9, 8; 10, 9; 11, 9}, 2, 0)), "♀"))), 
     "where Col1 is not NULL", 0))
    

    0