Search code examples
excelgoogle-sheetslambdaexcel-formulagoogle-sheets-formula

Excel or Google Sheets—Is there a way to duplicate rows in place using only formulas?


Using only excel/google sheets formulas, I would like to take a table like this:

a b c
q r s
x y z

and turn it into something like this:

a b c
a b c
a b c
q r s
q r s
q r s
x y z
x y z
x y z

The point is that the rows are duplicated n times but maintain the sort order of the original table.


Solution

  • use in google sheets:

    =LAMBDA(y, z, INDEX(SPLIT(FLATTEN(TEXT(BYROW(y, LAMBDA(x, 
     TEXTJOIN("​",,x))), IFERROR(SEQUENCE(1, z)/0, "@"))), "​")))
     (A1:C3, 3)
    

    =LAMBDA(x;y;MAKEARRAY(ROWS(x)*y;COLUMNS(X);LAMBDA(r;c;INDEX(x;ROUNDUP(r/y);c))))(A1:C3;3)=MAKEARRAY(COUNTA(A:A)*3,3,LAMBDA(r,c,INDEX(A:C,ROUNDUP(r/3),c)))

    or try:

    =LAMBDA(x, y, REDUCE(x, SEQUENCE(y-1), 
     LAMBDA(a, b, IF(b, {a; x}))))
     (A1:C3, 3)