google-sheetsspreadsheet

Repeat rows in Google Spreadsheet based on a cell value (BUT repeated rows must be 7 days apart)


I have a Google spreadsheet with two sheets: 'Input Sheet' and 'Output Sheet'. I would like all rows in the 'Input Sheet' to be repeated and stored in the 'Output Sheet' based on a cell value in column H.

I got the repeater to work with the following formula (formula is in cell A2 of the 'Output Sheet'):

=ARRAYFORMULA(
     VLOOKUP(
        TRANSPOSE(SPLIT(QUERY(REPT(ROW('Input Sheet'!A2:A)&" ",'Input Sheet'!H2:H),,9^9)," ")),
        {ROW('Input Sheet'!A2:A),'Input Sheet'!A2:I},
        {1,2,3,4,5,6,7,8,9}+1,0
     )
)

However, in the 'Output Sheet', the values in columns F and G are just repeated based on the 'Input Sheet'. I would like column F and G values to be exact 1 week apart. For ex: cell F2 should be 4/7/2024 and cell G2 should be 4/13/2024. Cell F3 should be 4/14/2024 and cell G3 should be 4/20/2024, and so on...

A little help on how I can accomplish this please. Thank you!

I have included the spreadsheet below:

https://docs.google.com/spreadsheets/d/14jxO12zW7SKe6e5TpmLA6nIdPjWP17XZtghGvWmgn1M/edit?usp=sharing

Including a visual for my current and desired results below:

> CURRENT:

Week (Start) |  Week (End)  | Weeks

Apr 7, 2024  | Apr 27, 2024 | 3 (row 1)
Apr 7, 2024  | Apr 27, 2024 | 3 (row 2)
Apr 7, 2024  | Apr 27, 2024 | 3 (row 3)

> DESIRED:

Week (Start)  |  Week (End)  | Weeks

Apr 7, 2024   | Apr 13, 2024 | 3 (row 1)
Apr 14, 2024  | Apr 20, 2024 | 3 (row 2)
Apr 21, 2024  | Apr 27, 2024 | 3 (row 3)

Solution

  • =LET(rng,'Input Sheet'!A2:I, 
       arr,FILTER(rng,INDEX(rng,,1)<>""),
       WRAPROWS(TOROW(BYROW(arr, LAMBDA(r, TOROW(
         MAP(SEQUENCE(INDEX(r,,8)), LAMBDA(wk, 
           {CHOOSECOLS(r, SEQUENCE(4)), INDEX(r,,6)+7*(wk-1),
            INDEX(r,,6)+7*wk-1, INDEX(r,,9)}))))),1),7))
    
    1. First BYROW passes the array arr row by row into a LAMBDA that stores the current row in r.
    2. That LAMBDA's formula is a MAP function that passes a SEQUENCE derived from the number of weeks into a LAMBDA that stores the current week in wk.
    3. That LAMBDA's formula returns an array for each wk where:
      1. The first 4 columns are the first 4 columns from r,
      2. The fifth column is the week (start) value from r with wk-1 weeks added
      3. The sixth column is the week (start) value with wk weeks added.
      4. The seventh and last column is the hours from column 9 of r
    4. Since the BYROW function requires the LAMBDA to return a single row for each row passed in, the MAP function is wrapped in TOROW.
    5. The result of the BYROW is an array where each row represents a single row from arr, but the number of columns for each row can vary by multiples of 7.
    6. To get an array of only 7 columns, TOROW is applied to the result with the skip_blanks argument included, then WRAPROWS is used to wrap that row every 7 columns.