Reputation: 351
Hi friends, In the above example grid, I am looking for a formula or function that can find the sum of all Data elements with their weighted products. Below is an example pattern in terms of calculation -
Data Range: D5 : N7
Formula I am looking for is : Data (Range) * Weight A (In the same col) * Weight B (In the same row)
D5 * D4 * C5 + E5 * E4 * C5 ... + N5 * N4 * C5 +
D6 * D4 * C6 + .................+ N6 * N4 * C6 +
................................. N7 * N4 * C7
One way I know I could solve this is by calculating SUM at column and row levels and then adding them up to get a 'Grand Total'. However, I have a feeling there is a better and efficient way of solving this.
If anyone has any insights/suggestions, please help!
Upvotes: 1
Views: 77
Reputation: 2614
DROPping in to take Tom's for fun (using broadcasting):
=LET(
tb, C4:N7,
w_a, DROP(TAKE(tb, 1), , 1),
w_b, DROP(TAKE(tb, , 1), 1),
data, DROP(tb, 1, 1),
SUM(w_a * w_b * data)
)
Upvotes: 0
Reputation: 11628
=MMULT(D5:N7*D4:N4*C5:C7,SEQUENCE(COUNTA(D4:N4))^0)
Or =BYROW(D5:N7*D4:N4*C5:C7,LAMBDA(x,SUM(x)))
Or I believe ETA was rolled out to production (can't test this on my mobile):
=BYROW(D5:N7*D4:N4*C5:C7,SUM)
Edit: Oops, I thought you wanted a row wise total. Than a SUM(product) would do, which is already posted.
Upvotes: 0
Reputation: 875
I did it the hard way:
=LET(_Data,A1:D3,_Rows,ROWS(_Data),_Cols,COLUMNS(_Data),
_HeadRow,TAKE(_Data,1,_Cols*(-1)+1),
_HeadCol,TAKE(_Data,_Rows*(-1)+1,1),
_Body,DROP(_Data,1,1),
_Calc,MAKEARRAY(_Rows-1,_Cols-1,LAMBDA(a,b,INDEX(_Body,a,b)*INDEX(_HeadRow,1,b)*(INDEX(_HeadCol,a,1)))),
_Sum,SUM(_Calc),
_Sum)
Cross referenced with Tom's answer, I'm getting the same.
Advantage of my overcomplicated approach: if your data set changes, you only have one selection of data to make.
Upvotes: 1
Reputation: 34370
I think it's just
=SUM(D5:N7*D4:N4*C5:C7)
I checked the total using
=D$4*D5*$C5
and copying down and across.
Upvotes: 4