Google Sheets - dynamic lookup sum - google-sheets

I have a vertical list of points and several horizontal sheets of data. I want to get a sum of the data from each of those sheets, multiply them by value of their header key, and get a sum of each row of data.
For row 2 I want the total to be 1,011 (3 x 123 = 369, 2 x 321 = 642, 369 + 642 == 1,011) where it looks up the header of aaa and bbb, gets their point value, and multiplies it by their row value. I'm sure I can hack it and get the result but I'd like to get an efficient way to do it.

This seems to be a straightforward SUMPRODUCT formula.
=sumproduct((A3:A4=D1:E1)*(B3:B4)*(D2:E2))
Sample Google-Sheet

=SUMPRODUCT(B:B*iferror(hlookup(A:A,D1:E2,2,false),0))
or
=SUMPRODUCT(B:B*iferror(hlookup(A:A,1:2,2,false),0))

Related

Conditional Sum to a Specific Total in a Single Column

I am trying to weighted average of available stock ie 888 Items. We operate FIFO so that means I need to start sum from recent date backwards. How do i only select those cells that sum up to available stock balance (888) then sumproduct with the price?
Date Items Recieved Price
9/1/2022 254 $25.00
8/25/2022 242 $25.00
8/18/2022 230 $65.00
8/11/2022 218 $77.00
8/4/2022 206 $45.00
7/28/2022 194 $77.00
7/21/2022 182 $89.00
7/14/2022 737 $74.00
7/7/2022 1292 $86.00
6/30/2022 1847 $87.00
Query, Arrayformula & SUMproduct
You tagged both Excel and Google sheets. They're different. In Excel (Office 365) you can do this using:
=LET(stock,888,
data,B2:C11,
items,INDEX(data,,1),
price,INDEX(data,,2),
cumulative,SCAN(0,items,LAMBDA(a,b,a+b)),
r,XMATCH(stock,cumulative,1),
correction,INDEX(items,r)+stock-INDEX(cumulative,r),
SUMPRODUCT(
IFERROR(
VSTACK(
TAKE(items,r-1),
correction),
correction),
TAKE(price,r)))
stock is the number to sum up to.
data is the range containing both the items and prices.
SCAN is used to get the cumulative sum of all items row-by-row.
XMATCH is used to find the row (r) in the cumulative sum where the value is greater than or equal to the stock value.
r is used to correct the items in that row to the value required to get the cumulative sum up to row r equal to the stock value. (Item in row r + stock - cumulative sum in row r).
I than take the rows before r of the items and add (stack) the correction items value calculated and use that in a SUMPRODUCT with the prices up to r.
If r is the first row it'll throw an error at the TAKE(items,r-1)-part, if so IFERROR makes sure the corrected value is used without stacking it on previous items values.
Edit: since you mentioned FIFO you'd probably be interested to calculate from the bottom up. In this case you could use:
=LET(stock,888,
data,SORT(A2:C11,1,1),
items,INDEX(data,,2),
price,INDEX(data,,3),
cumulative,SCAN(0,items,LAMBDA(a,b,a+b)),
r,XMATCH(stock,cumulative,1),
correction,INDEX(items,r)+stock-INDEX(cumulative,r),
SUMPRODUCT(
IFERROR(
VSTACK(
TAKE(items,r-1),
correction),
correction),
TAKE(price,r)))
It works the same, it just uses an extra column for the data, so it could sort from old (first in) to new.
And it's unclear if you wanted this SUMPRODUCT or the average of it, but that's simply adding /stock to the last argument of LET

How to split and sum in google sheets

I am trying to figure out how to split the range in google sheets by
"-" delimiter and add the result. Basically from the image below, I am trying to split on "-" and add up the ones (i.e 1 + 1 +1 +1 = 4). However, using the formula below it adds up all of the numbers (i.e 1 + 5 +1 + 1+1 + 0 +1 +3 = 13) which is not what I want
You are correctly splitting the values into to two columns of data, but then summing the entire dataset. You need to specify your summation to just the column you want (which appears to be the first column). Index function will do this probably the best as the second parameter can specify which column in a data set.
Summing the first column:
=sum(index(split(B11:B14,"-"),,1))
Summing the second column:
=sum(index(split(B11:B14,"-"),,2))
Showing all (same as your your arraysformula split)
=index(split(B11:B14,"-"))
See sample sheet here.

Google Sheets - Using ArrayFormula with Conditional and Embedded Formula

This may be far more simple than I think. What I'm trying to do is use the ArrayFormula to copy an existing formula to all the rows in a particular column.
To begin with, I have several columns along the lines of:
What I'm doing is adding the first three numbers and multiplying them by the fourth. I also have an IF condition in which, if the fourth column is blank, I leave the value in column 5 blank.
Now, I'm trying to convert this to an ArrayFormula to repeat this for all the rows in column 5.
So I went from:
=IF(ISBLANK(E2)=TRUE,,SUM((B2+C2+D2)*E2))
to this:
=ArrayFormula(IF(ISBLANK(E2:E)=TRUE,,SUM((B2+C2+D2)*E2)))
But what this does is, when I add a new row, ALL of the values in column 5 are set to the same value. Here is an example:
So, my first thought was to set the range on the SUM formula and change it to:
=ArrayFormula(IF(ISBLANK(E2:E)=TRUE,,SUM((B2:B+C2:C+D2:C)*E2:E)))
But that just makes all the values to sum of all of what the individual values should be...so, in my example, it works out to 435 (60 + 135 + 240).
What am I doing wrong here? The values in column 5 should be different in each row (e.g., row 2 should be 135 and row 3 should be 240).
Thanks!
use:
=ARRAYFORMULA(IF(ISBLANK(E2:E)=TRUE,, (B2:B+C2:C+D2:D)*E2:E))
SUM is not supported under AF

ARRAYFORMULA with SUM OR SUMIF?

I'm trying my to use ARRAYFORMULA with SUM (or SUMIF?). I basically want to lock C1 and always SUM from C1 down
=ARRAYFORMULA((SUM(C1:C2) + 1)&":"&(SUM(C1:C3))) IN D3 is this
=ARRAYFORMULA((SUM(C1:C3) + 1)&":"&(SUM(C1:C4))) IN D4 is this
Here is sample sheet and below is visual.
Col C is 50, 20, 16, etc.
Col D is 2:50, 51:70, 71:86, etc.
https://docs.google.com/spreadsheets/d/1DANMNEahYAoYBCQO1BsfXfUrgPj2mVWNKjn7VuYIIyI/edit#gid=0
units desired_result
50 2:50
20 51:70
16 71:86
8 87:94
2 95:96
If you could give a brief explanation on logic that'd be great. Google's is confusing (as always) and Youtube is limited.
This gives a result close to the one you want, but will need a bit of tweaking if you want to get 2:50 in F2 and 163:163 further down
=arrayformula(if(C2:C="","",sumif(row(C2:C),"<"&row(C2:C),C2:C)+1&":"&sumif(row(C2:C),"<="&row(C2:C),C2:C)))
I think it should be fairly self explanatory - the first part of the formula gives the sum for all rows where row number is less than row number of the current row and the second part of the formula gives the sum for all rows less than or equal to the current row. The slightly tricky thing is to realise that when the criteria part "<"&row(C2:C) of the SUMIF is itself an array, the SUMIF is evaluated separately for each array element and gives a new row in the resulting output array.
To lock a range, use $
=(SUM($C$1:C2) + 1)&":"&(SUM($C$1:C3))
Drag fill down.

Find the sum of each row in a spreadsheet

I'm new to Sheets and I don't know any terminology yet so I wasn't sure how to look this up.
If I have:
A1[=SUM(B1:1)]
How do I automatically copy that to A2 so that:
A2[=SUM(B2:2)]
And the same thing continues either indefinitely or until I declare a stopping point?
First of all, if you simply copy-paste the formula from A1 to A2 (or several cells below), it will automatically change as you want. This is how relative references work.
But it's also possible to get all the sums with one formula.
The following formula, entered in A1, will create sums of the first seven row in column A. To change the number of rows summed, replace 7 in B1:7 with another number.
=arrayformula(mmult(B1:7 + 0, transpose(B1:1 * 0 + 1)))
Explanation:
B1:7 + 0 coerces the entries to numbers (so that blank cells become 0).
transpose(B1:1 * 0 + 1) creates a column vector of 1s of suitable size.
matrix multiplication mmult by a column of 1s amounts to summing each row.
the wrapper arrayformula indicates that the operations are to be done on arrays.

Resources