Forum Discussion
Two way sum lookup (by month & name)
- Feb 11, 2025
Another option for modern Excel:
=LET( rϑws, MAP(A12:A15, LAMBDA(v, LET(x, XLOOKUP(v, A4:A7, B4:W7, B2:W2), LAMBDA(x)))), cϑls, MAP(B11:G11, EOMONTH(+B11:G11, 0), LAMBDA(s,e, LAMBDA(n, CHOOSE(n, s, e)))), MAP(IFNA(rϑws, cϑls), IFNA(cϑls, rϑws), LAMBDA(r,c, SUMIFS(r(), B3:W3, ">=" & c(1), B3:W3, "<=" & c(2)))) )This method uses the concept of "thunks" to store the applicable row of data for each name, as well as the applicable start and end dates for each period, inside a separate LAMBDA function. The two resulting vectors of "thunks" are then broadcast both across and down to fill an array for the entire output grid, and MAP loops through both arrays together, applying SUMIFS to each applicable row for each applicable period.
Alternatively, GROUPBY could also be used, but the order of names will not be exactly the same as the desired output table shown in your sample file:
=LET( tbl, TRANSPOSE(A3:W7), mth, IFERROR(EOMONTH(TAKE(tbl,, 1), 0), "Name"), arr, TRANSPOSE(GROUPBY(mth, DROP(tbl,, 1), SUM, 3, 0)), VSTACK(TEXT(TAKE(arr, 1), "mmm-yy"), DROP(arr, 1)) )Cheers!
Thanks very much for your detailled and quick reply.
This may be a stupid question but what is "pivoted" data that needs "flattening out"?
Flattend data:
"Pivoted" data:
Flattened data is much easier to summarize because we know the dates are in column 2 and the values are in column 3. PIVOTBY and a PivotTable can make quick work of vertically arranged data. The screen cap of "pivoted" data shows an actual pivot table with ungrouped dates.