Search code examples
arraysgoogle-sheetsgoogle-sheets-formula

Dynamic Spill Formula For Reduce Function Mixed With Split


My sample sheet is probably easier to understand than my writing but here's the issue: I have a sheet that I'm trying to create a spill formula that sums an array of numbers up in each line.

  • Columns B:D is my existing data that's being evaluated
  • If values exist in Column D (which is not always the case), split values (defined by , ) and lookup each one's most recent entry (column B) and sum its value from column C with other members in same cell.

I can accomplish this using the Reduce formula shown in my sample data in blue column F, and dragging the formula to the latest entry, however it will not spill down dynamically.

=iferror(REDUCE(0,SPLIT(D2,",",false),lambda(total,value,xlookup(value,B:B,C:C,"",0,-1)+total)),0)

I can get the C values to spill down dynamically (as shown in green columns in sample) as numeric values, but I can't figure out how to sum them.

=Filter(iferror(XLOOKUP(SPLIT(D2:D,", ",false),B:B,C:C,"",0,-1),0),A2:A>0)

I would have expected something like either of these to work, but both generate a #N/A

=Filter(iferror(REDUCE(0,SPLIT(D2:D,", ",false),
 lambda(total,value,xlookup(value,B:B,C:C,"",0,-1)+total)),0),A2:A>0)

=Filter(sum(iferror(XLOOKUP(SPLIT(D2:D,", ",false),B:B,C:C,"",0,-1),0)),A2:A>0)

I've also tried these as named functions with only the spilled variables as input, but same result.

I know the reduce function can perform a spilled range, as shown here on Ben Collins' site, however I can't figure out how to get it to do so with my dataset. It's occurred to me that because I'm generating a horizontal array, a verticle array may not be possible?

Any helpful answers will be upvoted if not accepted. Thanks.


Solution

  • Here's one approach:

    =byrow(index(map(iferror(split(D2:index(D:D,match(2,1/(D:D<>""))),", ",0,1)),lambda(z,xlookup(z,B:B,C:C,)))),lambda(y,sum(y)))
    

    enter image description here