Forum Discussion
ctone00
Feb 20, 2024Copper Contributor
sumif horiztonally and vertically
how can i update my formula in the summary tab such that when i drag my formula down it finds the month and pulls in the correct column from the raw data tab.
7 Replies
Sort By
- djclementsBronze Contributor
ctone00 One option with SUMIF is to use XLOOKUP (or INDEX/MATCH) in the [sum_range] argument:
=SUMIF(Raw!$A:$A,B$1,XLOOKUP($A2,Raw!$B$1:$M$1,Raw!$B:$M))
Then drag down and across as needed. Personally, I would not use entire column references here, but such is your example...
- Patrick2788Silver Contributor
A 365 solution for the future with an insider function (hopefully, widely available very soon!):
=LET( aggregated, GROUPBY(Division, Data, SUM, , 0), TRANSPOSE(aggregated) )
- PeterBartholomew1Silver Contributor
That takes the prize for elegance!
I had wondered about normalising the data and then using PIVOTBY but this is far better.
- PeterBartholomew1Silver Contributor
I have added a couple more solutions to Maciej_Kopczynski 's workbook. I program solely for 365 dynamic arrays so they look somewhat different. The main challenge is that your required layout is an array of arrays (month x division). That is a characteristic of most spreadsheet solutions but, regrettably, Microsoft decided that nested arrays should be treated as an error rather than the desired result.
My first solution uses array reshaping to lay the raw data out in a form that the basic summation by columns is a straightforward SUM rather than SUMIFS.
= LET( reshaped, WRAPROWS(TOCOL(data), 24), monthlyTotal, BYCOL(reshaped, LAMBDA(x, SUM(x))), WRAPCOLS(monthlyTotal, 12) )
The second approach uses SUMIFS, as suggested by the OP, but treats the divisions as two separate solutions which are then stacked to give the result. This would be very tedious if there were 100s of divisions or if the count of divisions needed to be dynamic.
= LET( div1Totals, TOCOL(BYCOL(data, LAMBDA(d, SUMIFS(d, Division, "Division1")))), div2Totals, TOCOL(BYCOL(data, LAMBDA(d, SUMIFS(d, Division, "Division2")))), HSTACK(div1Totals, div2Totals) )
Something that does work well is that the BYCOL helper function returns each column as a range reference rather than an array, so permitting the use of SUMIFS etc.
p.s. I appreciate that the OP ctone00 wanted to get results by 'dragging a formula down' but, since I haven't used that as a technique since 2015, I am definitely not the best person to address that requirement.
- Patrick2788Silver Contributor
I offer a 365 solution with a dynamic array:
=LET( GetTotals, LAMBDA(r, c, LET( month, INDEX(totals, , r), div, RIGHT(division) * 1, filtered, FILTER(month, div = c, 0), SUM(filtered) ) ), MAKEARRAY(12, 2, GetTotals) )
- Maciej_KopczynskiCopper Contributor
ctone00 ,
see attached. All methods are pretty much using the same matrix multiplication logic. There are probably plenty more solutions to this.
- OliverScheurichGold Contributor
=SUMPRODUCT((B$1=Raw!$A$2:$A$9)*(Summary!$A2=Raw!$B$1:$M$1)*Raw!$B$2:$M$9)
You can use this SUMPRODUCT formula. See your attached file.