Search code examples
google-sheets-formula

Google sheets SCAN function


I'm trying to get the sheets to auto go down and give me quarters like this

1Q08
2Q08
3Q08
4Q08
1Q09
2Q09
3Q09
4Q09
1Q10

and etc, going down a column using one formula.

I think it is possible using the SCAN function, but having problems with it.

I've tried

SCAN("1Q08",B1:B9,LAMBDA(a,c,
  LET(
     quart, index(split(c,"Q"),,1),
     year, index(split(c,"Q"),,2),
     new_quart, if(quart=4,1,quart+1),
     new_year, if(quart=4,year+1,year),


     new_quart&"Q"&new_year
    )
  )
)

It's starting with a blank so it can't accumulate, but I put 1Q08 in the initial value. Is this possible to do?


Solution

  • Here's one approach:

    =let(initial_,8,seq_,4,years_,3,
        tocol(reduce(,sequence(years_,1,0),lambda(a,c,{a;index(sequence(seq_)&"Q"&text(initial_+c,"00"))})),1))
    
    • you may change the parameters initial_,seq_,years_ as per choice

    enter image description here