Search code examples
google-sheetsgoogle-sheets-formulafillnaforward-fill

How do I forward fill line items data in Google Sheets?


I have an exported sheet of order line items and that has a master order number in column A but the order data is only completely filled on the first line item.

The blank cells are creating an issue when I try to work with the data.

What is the best way to fill out the blank cells with the data from the first line item?

I've tried to merge rows but I am only trying to merge if the cells are empty and of the same order number.

I came up with an idea:

  1. =if($A3=$A2,if(ISBLANK(C2),"",C2),"FALSE")

  2. Then filter by blank cells and copy and paste.

Is this the best way?

Input:

Name Email Shipping Method Lineitem quantity Lineitem price Lineitem sku Shipping Address1 Shipping Address2 Shipping City Shipping Zip Shipping Province
985292 [email protected] Expedited Plus Shipping (3-4 days) 1 0 XXXXSKU XXXXXX Avenue Apt #220 Venice 90291 CA
985292 [email protected] 1 24.95 XXXXLO01
985292 [email protected] 2 44.95 PRO20LL
985285 [email protected] Expedited Plus Shipping (3-4 days) 1 85.4 PRO20LL02 XXXXXX Ave Norwell 2889 RI
985288 [email protected] Priority Shipping (2-3 days) 1 44.95 PRO20LL XXXXXX Parkway Minneapolis 55408 MA
985287 [email protected] Priority Shipping (2-3 days) 1 44.95 PRO20LL XXX Street Apt #4 Minneapolis 55102 MN
985250 [email protected] Expedited Plus Shipping (3-4 days) 1 35.96 XXXXX-01 XXXXX Road Norwell 2061 MA
985250 [email protected] 1 40.46 PRO20LL
985250 [email protected] 1 35.96 XXXX_SK01

Ideal output:

Name Email Shipping Method Lineitem quantity Lineitem price Lineitem sku Shipping Address1 Shipping Address2 Shipping City Shipping Zip Shipping Province
985292 [email protected] Expedited Plus Shipping (3-4 days) 1 0 XXXXSKU XXXXXX Avenue Apt #220 Venice 90291 CA
985292 [email protected] Expedited Plus Shipping (3-4 days) 1 24.95 XXXXLO01 XXXXXX Avenue Apt #220 Venice 90291 CA
985292 [email protected] Expedited Plus Shipping (3-4 days) 2 44.95 PRO20LL XXXXXX Avenue Apt #220 Venice 90291 CA
985285 [email protected] Expedited Plus Shipping (3-4 days) 1 85.4 PRO20LL02 XXXXXX Ave Norwell 2889 RI
985288 [email protected] Priority Shipping (2-3 days) 1 44.95 PRO20LL XXXXXX Parkway Minneapolis 55408 MA
985287 [email protected] Priority Shipping (2-3 days) 1 44.95 PRO20LL XXX Street Apt #4 Minneapolis 55102 MN
985250 [email protected] Expedited Plus Shipping (3-4 days) 1 35.96 XXXXX-01 XXXXX Road Norwell 2061 MA
985250 [email protected] Expedited Plus Shipping (3-4 days) 1 40.46 PRO20LL XXXXX Road Norwell 2061 MA
985250 [email protected] Expedited Plus Shipping (3-4 days) 1 35.96 XXXX_SK01 XXXXX Road Norwell 2061 MA

Solution

    • This is called forward fill and can be accomplished using SCAN. Using SCAN, iterate through each Name(col A) and to get a comparator function to compare each item(c) to the previous accumulated one(a)
    • Extend each col A inside SCAN using OFFSET to the entire 10 columns(ce)
    • Compare current Col A Name with the accumulator's Name and if current value is empty, put the accumulator, else the extended current value ce

    Assuming the table is in A1:K10, in A15,

    =SCAN(
      A1,A2:A10,
      LAMBDA(a,c,
        LET(
          cols,10,
          ce,OFFSET(c,0,0,1,cols),
          ARRAYFORMULA(
            IF(
              (ce="")*((c=INDEX(a,1,1))),a,ce
            )
          )
        )
      )
    )