Adding or subtracting values based on a vector of check boxes in Google Sheets - google-sheets

I hope you can help me with this:
I'm trying to create a savings-control sheet where I list my monthly payment and I'm trying to use the SUMIF formula to subtract my expenses by selecting what I have currently payed but I don't know if this may work with a vector of check boxes Sheets sample
the current formula as you can see in the image works fine but only for column D however if I check the rest of the boxes nothing is subtracted
This is how the formula looks like now: =A31+A32-SUMIF(D3:J14,TRUE,C3:C14) however only works from D3 to D14 and I need it to work from D3 to J14
Any help will be highly appreciate

I think the simplest solution is:
=A31+A32-SUM(ARRAYFORMULA(N(D3:J14)*C3:C14))
Formula rundown
This formula is based on the function N that converts a boolean to an integer (1 for true, 0 for false). We can then multiply by the expense value. Here an example:
=N(D3)*C3
This will equal C3 iff D3 is checked.
Having that we can make the entire table with ARRAYFORMULA:
=ARRAYFORMULA(N(D3:J14)*C3:C14)
Now we can sum all the values to have the total expenses:
=SUM(ARRAYFORMULA(N(D3:J14)*C3:C14))
Add the other cells and you get your result.
References
N (Docs Editors Help)
ARRAYFORMULA (Docs Editors Help)
SUM (Docs Editors Help)

Try
=A31+A32-sumproduct((countif(if(D3:J14, row(D3:D14)), row(D3:D14))>0),C3:C14)
and see if that helps?

Related

Sum every n columns starting at the 1st column in the range

In Excel/Google Sheets I have found how to sum every N columns on websites such as https://exceljet.net/formula/fixed-value-every-n-columns, but the problem is, from what I can see is that it starts at N column each time. I need something that starts from column 1 and then counts every N columns. like the following:
I need to do this with a formula and not a script.
With Google-Sheets, try:
Formula in M2:
=SUM(QUERY(TRANSPOSE(A2:J2),"Skipping "&L2))
Or, a single dynamic array formula (without dragging):
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(10,1,1,0)))
Or, more dynamic:
=INDEX(MMULT(A2:J4*(MOD(COLUMN(A2:J4),L2:L4)=1),SEQUENCE(COLUMNS(A2:J4),1,1,0)))
Note: The latter would also work in Excel with slight modifications.
Google sheets formula:
=SUM(FILTER(A2:J2, MOD(A2:J2, L2)=1))
then drag to other cells
or use this array version:
Array version:
=INDEX(TRANSPOSE(MMULT(A2:J4,TRANSPOSE(COLUMN(A2:J4)^0 *
N(MOD(COLUMN(A2:J4), L2:L4)=1)))), ,1)
If you want the cells that were added to be automatically highlighted.
Conditional formatting used on A2:J:
=MOD(COLUMN(), $L2)=1
In M2:
=SUMPRODUCT(A2:J2,N(MOD(SEQUENCE(,COLUMNS(A2:J2),0),L2)=0))
and copied down.
Try this formula on column M:
=SUM((sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2))+A2)
Here's the result on Column M.
Just to break down the code sumif(ArrayFormula(mod((COLUMN(B2:J2)-COLUMN(B2)+1),L2)),0,B2:J2) does the actual calculation with the number of intervals set on Column L but take note that I started at the 2nd column so the range here does not include the first column. The result from this is at the Column O highlighted red as you can see in the screenshot.
At the Column M is the actual solution where I only added the first column by using SUM on top of the previous formula.
I hope my explanation is clear.
Just copy/drag the formula down to each row and it should work.
Reference: How to Sum Every Nth Row or Column in Google Sheets Using SUMIF

Google Sheet SUMIF not summing range

I am trying to SUM all rows based on the condition that a row's value is greater than 0. However, the formula is only calculating the first row and ignoring the rest of the range. I encountered this issue on a different spreadsheet but I isolated the issue on a new spreadsheet to show you and ask questions.
The answers should be B1 ($5), B2 ($15), B3 ($30).
I followed the formula's usage as explained in the Google Docs https://support.google.com/docs/answer/3093583?hl=en
The formula with respective row number =SUMIF(A1, ">0", A$1:A1)
Update - The below is my intended purpose. I have a spreadsheet that calculates money In and Out. For each of these transactions, I simply want the balance as a result of such transactions. This only works for the first row. (I hardcoded the values for the rest of the rows just to show my goal.)
try:
=ARRAYFORMULA(IF(A:A="",,SUMIF(ROW(A:A), "<="&ROW(A:A), A:A)))
if you prefer dragging do:
=SUMIF(INDIRECT("A1:A"&ROW()), ">0")
UPDATE:
=ARRAYFORMULA(IF(D2:D="",,
SUMIF(ROW(D2:D), "<="&ROW(D2:D), D2:D)-
SUMIF(ROW(E2:E), "<="&ROW(E2:E), E2:E)))

Is there a function that can create an expanding array formula that conditionally sums a column based on multiple criteria?

To start, this is my first time posting and so please let me know if I can fix my post in any way to make it easier to answer.
I am trying to create an auto-expanding array formula
I have a sheet with my investment asset mix that including amounts of shares owned of each particular stock, and a sheet that tracks when I receive dividends. My goal is to write an automatically expanding array formula that will sum up the amount of shares that own of a stock on the date a dividend is received and return that value. I have written three different formulas that all accomplish this but none of them will auto-expand as an array.
I'm sure there are a lot of solutions I've overlooked. To boil it down, I need an expanding array formula that will sum the "Shares" column of my asset mix sheet ('Asset Mix'!D2:D, or 'AssetMixShares') conditionally. The name of the stock entered in 'Dividends'!C2:C needs to match the name of the stock in 'Asset Mix'!A2:A (or the named range 'AssetMixStocks'). It then needs to check the dates in 'Asset Mix'!C2:C (or 'AssetMixDates') against the dates in 'Dividends'!A2:A and sum all share amounts where the purchase date is less than (earlier than) the Ex-Dividend Date.
I could probably write some sort of vlookup array on the "Running Total" column -- 'Asset Mix'!E:E -- that would solve the issue, but I'm hoping to eliminate that column. I feel very strongly that what I'm trying to do should be possible without the help of a running total column -- I just don't have the knowledge.
I have tried countless functions and formulas, but the four that I currently have in my example worksheet are SUM, SUMPRODUCT, DSUM, and QUERY.
Attempt 1
SUM and IF
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Attempt 2
SUMPRODUCT
=({arrayformula(SUMPRODUCT(--((AssetMixStock=(indirect("C"&ROW())))*(AssetMixDate<(indirect("A"&ROW())))),AssetMixShares))})
Attempt 3
DSUM
=DSUM('Asset Mix'!A:E,"Shares",{"Date","Stock";"<"&A2,C2})
Attempt 4
QUERY
=arrayformula(query(AssetMix,"Select sum(D) where A = '"&C2:C&"' and C < date'"&(text(year(A2:A),"0000") & "-" & text(month(A2:A),"00") & "-" & text(day(A2:A),"00"))&"' label sum(D) ''",0))
These will all work, as long as I manually drag the formula down, but I want to write some sort of formula that will auto-expand to the bottom of the Dividends sheet.
I have tried to create a Dummy sheet that has both of the relevant sheets. Please let me know if you can access it -- the link is below.
https://docs.google.com/spreadsheets/d/1wlKffma0NJ0KrlWxyX_N20y62azsGpFp3enhmjzJK1Q/edit?usp=sharing
Thanks so much for getting this far and any help you can provide!
We can focus in the first formula to understand a way to make it "self-expandable". As we see it contains references to the cells A2 and C2 in "Dividends" sheet:
=ArrayFormula(SUM(IF('Asset Mix'!A:A=C2,IF('Asset Mix'!C:C<A2,'Asset Mix'!D:D))))
Every time some data appears in these columns (A and C), the formula should work. We can control the presence of the formula by onEdit trigger, if editing is manual. Consider the code:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.getName() == 'Dividends') {
var row = e.range.getRow();
for (var offset = 0; offset < e.range.getHeight(); offset++) {
sheet.getRange(3, 10).copyTo(sheet.getRange(row + offset, 10));
}
}
}
It checks any modification on the sheet "Dividends" and copies required formula to the modified row(s). This way the formula is expanded for other rows in use.
Well, it's solved! I'll leave this up in case anyone else has the same question.
A kind soul explained the magic of MMULT() to me, and wrote this solution.
=ARRAYFORMULA(MMULT((C2:C=TRANSPOSE('Asset Mix'!A2:A))*(A2:A>TRANSPOSE('Asset Mix'!C2:C)),N('Asset Mix'!D2:D))

google sheets, use formula output for next formula

I'm trying to CONCATENATE two cells in order to compare the results so that I can search by them, however the values of the two CONCATENATE outputs are different as one inputs is coming from the another formula.
Screen shots attached
I'm basically trying to compare the start time and channel number from A and B, with the data from G and H, so that I can update D with the relevant information in F (in the same format as A).
I first convert the EPOC time to human time readable, but when i try and CONCATENATE with the channel number, I get a different value to when i do that with A and B.
formula for c2 =CONCATENATE(A2,B2)
formula for i2 =G2/86400000+date(1970,1,1)
formula for k2 =CONCATENATE(G2,H2)
As you can see, the values for c2 and k2 are different event though a2 and i2 are the same (looking).
I've tried using CELL, INDEX, and INDIRECT but just can't seem to get it right, and I've tried various formatting options
Hopefully i've explained this right. Any solution welcome
raw data csv here START ,CHANNEL,concat,end?,,EndDateTime epoc,startDateTime epoc,channel,converted start,converted end,concat
12:58:00 AM,10,,,,1520391600000,1520382480000,7,,,
12:28:00 AM,7,,,,1520395200000,1520384280000,10,,,
So you have a couple of issues here.
CONCATENATE(A2,B2) will never equal CONCATENATE(I2,H2) because the values in A2 (12:58) and B2 (10) do not equal the values in I2 (12:28) and H2 (7). I think you meant to compare A2,B2 to I3,H3
A2 (12:58) does not equal I3 (12:58). You'll see this for yourself if you convert both to the date or number formats. The date value of A2 is 12/30/1899, the default when you enter only a time in the cell. The date value of I3 is 3/7/2018, because you converted the exact date and time from the EPOCH value.
For the two concatenations to equal each other, you need to resolve the issues above. You can do this by adding a date to column A's values.
On another note, I think there are better ways of populating column D based on the data in column F. A simple Vlookup should do the trick, once you resolve issue #2 above.

Google Spreadsheet sum which always ends on the cell above

How to create a Google Spreadsheet sum() which always ends on the cell above, even when new cells are added? I have several such calculations to make on each single column so solutions like this won't help.
Example:
On column B, I have several dynamic ranges which has to be summed. B1..B9 should be summed on B10, and B11..B19 should be summed on B20. I have tens such calculations to make. Every now and then, I add rows below the last summed row , and I want them to be added to the sum. I add a new row (call it 9.1) before row 10, and a new raw (let's call it 19.1) before row 20. I want B10 to contain the sum of B1 through B9.1 and B20 to contain the sum of B11:B19.1.
On excel, I have the offset function which does it like charm. But how to do it with google spreadsheet? I tried to use formulas like this:
=SUM(B1:INDIRECT(address(row()-1,column(),false))) # Formula on B10
=SUM(B11:INDIRECT(address(row()-1,column(),false))) # Formula on B20
But on Google Spreadsheet, all it gives is a #name error.
I wasted hours trying to find a solution, maybe someone can calp?
Please advise
Amnon
You are probably looking for formula like:
=SUM(INDIRECT("B1:"&ADDRESS(ROW()-1,COLUMN(),4)))
Google Spreadsheet INDIRECT returns reference to a cell or area, while - from what I recall - Excel INDIRECT returns always reference to a cell.
Given Google's INDIRECT indeed has some hard time when you try to use it inside SUM as cell reference, what you want is to feed SUM with whole range to be summed up in e.g. a1 notation: "B1:BX".
You get the address you want in the same way as in EXCEL (note "4" here for row/column relative, by default Google INDIRECT returns absolute):
ADDRESS(ROW()-1,COLUMN(),4)
and than use it to prepare range string for SUM function by concatenating with starting cell.
"B1:"&
and wrap it up with INDIRECT, which will return area to be sum up.
REFERRING TO BELOW ANSWER from Druvision (I cant comment yet, I didn't want to multiply answers)
Instead of time consuming formulas corrections each time row is inserted/deleted to make all look like:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
You can spare one column in separate sheet for holding variables (let's name it "def"), let's say Z, to define starting points e.g.
in Z1 write "B1"
in Z2 write "B11"
etc.
and than use it as variable in your sum by using INDEX:
SUM(INDIRECT(INDEX(def!Z:Z,1,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B1 to calculated row, since in Z1 we have "B1" ( the 1,1 in INDEX(...,1,1) )
SUM(INDIRECT(INDEX(def!Z:Z,2,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B11 to calculated row, since in Z2 we have "B11" ( the 2,1 in INDEX(...,2,1) )
please note:
Separate sheet named 'def' - you don't want row insert/delete influence that data, thus keep it on side. Useful for adding some validation lists, other stuff you need in your formulas.
"Z:Z" notation - whole column. You said you had a lot of such formulas ;)
Thus you preserve flexibility of defining starting cell for each of your formulas, which is not influenced by calculation sheet changes.
By the way, wouldn't it be easier to write custom function/script summing up all rows above cell? If you feel like javascripting, from what I recall, google spreadsheet has now nice script editor. You can make a function called e.g. sumRowsAboveMe() and than just use it in your sheet like =sumRowsAboveMe() in sheet cell.
Note: you might have to replace commas by semicolons
NOTE
After testing this answer, it will only work if the sum is in a different column due to a circular dependency error. Otherwise, the solution is valid.
It's a bit of algebra, but we can take advantage of Spreadsheets' lower right corner drag.
=SUM(X:X) - SUM(X2:X)
Where X is the column you are working with and X2 is your ending point. Drag the formula down and Sheets will increment the X2, thus changing the ending point.
*You mentioned that you had tens of such calculations to make. So in order to fit your exact need, we would subtract your last summation to get that "middle" range that we wanted.
e.g.
B1..B9 should be summed on B10, and B11..B19 should be summed on B20
Because of the circular dependency error mentioned earlier, I can't solve it exactly and put the sum on the same line, but this could work in other cases where the sum needs to be stored in a different column.
=SUM(B:B) - SUM(B9:B) //Formula on C10 (Sum of B1..B9)
=SUM(B:B) - SUM(B19:B) - B10 // Formula on C20 (Sum of B11..B19)
This is based on #PsychoFish, here is the solution:
=SUM(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"3:"&ADDRESS(ROW()-1,COLUMN(),4)))
Simply replace the "3:" for the row to start sum.
#PsychoFish is correct but cannot be dragged and copied since the column is literal and hard coded, and #Druvision was in the right direction but was wrong... basically ended up with the same issue of having to re-enter the ranges and then sliding the formulas over and over.
You guys are making this harder than you have to. I just leave a couple of empty rows above by "sum" row (you can format them to be filled with color or something to keep them from being inadvertently used), then just add your new rows just above those special rows.
Agree with what user7255446 said that everyone is overcomplicating. Keep one row blank before your sum row. And then whenever you want to insert a new row, click on your blank row and use "Insert row ABOVE" instead of "insert row below". Your sum formula will automatically adjust.
Example: I want to sum from B1 to B19. I leave row 20 blank. In cell B21, put =SUM(B1:B20). Then if you ever need to insert a new row, click on row 20 and choose "Insert row above". The sum formula automatically changes to =SUM(B1:B21) for you. And of course your sum cell is now B22.
General syntax:
=SUM(INDIRECT(cell_reference_as_string1 &":"& cell_reference_as_string2)
with for example:
cell_reference_as_string1 = ADDRESS(ROW(),COLUMN(),4)
cell_reference_as_string2 = ADDRESS(ROW()-1,COLUMN(),4)
I like how #abernier describes the general solution. So far only alphabet-based A1 notation (A being first column, 1 being first row) are being used. It keeps confusing me, especially when thinking of number of columns left of another column. I like the number-based R1C1 notation much better. To use R1C1 notation for INDIRECT, you need to pass FALSE like so:
=SUM(INDIRECT("R1C"&COLUMN()&":R"&(ROW()-1)&"C"&COLUMN(), FALSE))
I hope you find that helpful, too.
OFFSET() can be used/abused for this purpose. Give it the absolute address of the top left of the range, 0 and 0 for the row/column offsets, and the height/width of the range. Let OFFSET() be the argument to SUM(), SUMIF(), etc.
ROW() and COLUMN() are handy when computing the desired height/width. Be sure to remember to subtract one to exclude the current row/column, or else you're liable to end up with a circular reference. If you have header rows/columns, subtract for them too.
For example, to sum everything from A2 down, excluding the current row, try:
=SUM(OFFSET($A$2,0,0,ROW()-2,1))
To sum everything to the left of the current cell, wherever it may be, try:
=SUM(OFFSET(INDIRECT("RC1",FALSE),0,0,1,COLUMN()-1))
Now let's flip things upside down, to show that this works in the other direction. Suppose you want to sum the B column, starting below the current row, until (and including) row #10. Try this:
=SUM(OFFSET($B$10,ROW()-9,0,10-ROW(),1))
You can avoid negative offsets, while still summing column B:
=SUM(OFFSET(INDIRECT("RC2",FALSE),1,0,10-ROW(),1))
Remove the "2" to instead sum the current column:
=SUM(OFFSET(INDIRECT("RC",FALSE),1,0,10-ROW(),1))
(Credit to Tom Sharpe, who commented above.) INDEX() can be used in a range expression. You might prefer this over OFFSET(), so I'm putting it here. The following sums everything from G1 down to the row above the current:
=SUM(G1:INDEX(G:G,ROW()-1))
Here's how I do it.
This formula does not require you to edit or enter anything about the particular column you would like to sum
=SUM(INDIRECT(CONCATENATE(address(1,column(),4),":",LEFT(address(1,column(),4),1))&ROW()-1))
The answer by #PsychoFish led me in the correct way.
The only issue that I had to rewrite the formula again from each column and each sum. So here is the improved formula, which sums the previous 9 cells on the same column, without hardcoding the column or row numbers:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
The only issue is that I had to rewrite the formulas if someone adds or deletes a row. In this case I should change 9 to 10 or 8 corrspondingly.

Resources