In Google Sheets add all the rows above the current one - google-sheets

I want to add all the numbers in a given column of all the rows before the total row. Let's assume my column H contains number. If I have 15 rows, I'd do sum(H1:H15) in my sixteenth (total) row.
But when I add rows in between, my formula doesn't get updated, so I have to think of updating it to include all the rows.
I thought of using indirect to determine the index of the total row and doing the sum up to that row minus 1. That gave me:
=round(sum(indirect(concatenate("H2:H",row()-1))))
We can ignore round() here - it's just to given the nearest integer for my sum
sum adds all the rows
indirect lets me convert a string into an address so it can be used in my sum function
concatenate adds all the strings together
"H2:H" is the beginning part of my range
row() returns my current row and so row()-1 represents the last row I want to sum
Is there a more elegant way?
Thanks

You can use OFFSET for that like this:
=SUM(OFFSET(B6, 0, 0, ROW() - ROW(B6), 1))

Related

Can you use ARRAYFORMULA to SUM multiple columns changing dynamically

edit: must work with blank rows
I have a list of users in Column F and in Row 1 a list of dates.
I want to use ARRAYFORMULA to sum the values from relevant columns per each user. As an example, this sums 4 columns (F,G,H,I) per user:
=ARRAYFORMULA(IF(LEN(F1:F),G1:G+H1:H+I1:I+J1:J,""))
My question is, if it possible to sum for a dynamic number of columns. For example, I'll choose a number (e.g 7, 30...) and it will sum the relevant number of columns.
Can this be done?
Here's a spreadsheet with the above data:
https://docs.google.com/spreadsheets/d/17hyBEF1va4GMYZUFkDxxjJ0pXH2oCccgIaBT79GIsGc/edit#gid=0
In A2 I choose how many columns, and it will sum the relevant number of columns. In C1 I use such a formula to sum 4 columns using ARRAYFORMULA as an example (which is static, not dynamic).
Note that there was a nice solution, but because there is a blank row (#3) it causes the suggested solution to leave the sum for the final row (#7) empty. I'm looking for a solution that will work with blank rows.
There are 3 parameters:
A2: no of cols
G2: top left cell of values
F:F: col of row field (to count number of rows)
=ArrayFormula(MMULT(N(INDIRECT(CELL("address",G2)&":"&ADDRESS(COUNTA(F:F),COLUMN(G2)+A2-1,4))),N(TRANSPOSE(COLUMN(INDIRECT(CELL("address",G2)&":"&ADDRESS(COUNTA(F:F),COLUMN(G2)+A2-1,4)))^0))))

Summing over values in Google Sheets after applying a formula

I have a column of strings of the form
n|m
where n<m are natural numbers and I want to calculate the sum of this column where each cell has value m-n (m minus n). I can calculate the respective values into a separate cell via:
=INDEX(SPLIT(A1,"|"), 0,2)-INDEX(SPLIT(A1,"|"), 0,1)
and sum over those but I would like to do it in one step just as one formula below the specified column in one cell. Is that possible?
Thanks
Try in B1
=ARRAYFORMULA(SUM(IFERROR(INDEX(SPLIT(A1:A,"|"), 0,2)-INDEX(SPLIT(A1:A,"|"), 0,1))))
if you want to get the sum below the last value, i.e. row#8
=ARRAYFORMULA(SUM(IFERROR(INDEX(SPLIT(A1:A8,"|"), 0,2)-INDEX(SPLIT(A1:A8,"|"), 0,1))))

Is there a way to use an array formula to keep my formula (to add specific columns) when rows are inserted?

I'm setting up a spreadsheet that has specific columns summed in each row, but I need the formula to be included when a row is inserted.
The current formula also includes a statement to make a 0 value, if a check box is checked in the last column:
=IF(T2=FALSE, SUM(I2,K2,L2,M2,N2,O2), 0)
Is there a way I can do this using an array formula?
Here is a formula which will give a sum for columns I to O in each row, ignoring column J:
=ArrayFormula(if(I2:I="","",if(T2:T<>FALSE,0,I2:I+sumif(row(K2:O)+0*column(K2:O),row(K2:O),K2:O))))
but this assumes all rows that have data will have a number in column I.
If this isn't the case, you could go on to test columns individually like this:
=ArrayFormula(if((I2:I="")*(K2:K=""),"",if(T2:T<>FALSE,0,I2:I+sumif(row(K2:O)+0*column(K2:O),row(K2:O),K2:O))))
and so on up to column O if necessary, or maybe column T is always completed and you could test that - it depends how your data actually looks.
Note 1
row(K2:O)+0*column(K2:O)
is necessary to generate an array which is has the same dimensions as K2:O as required by SUMIF.
Note 2
There's also the MMULT approach to getting the row sums as demonstrated here
={"AAA"; ARRAYFORMULA(IF(LEN(T2:T), IF(T2:T=FALSE, I2:I+K2:K+L2:L+M2:M+N2:N+O2:O, 0), ))}

Insert duplicate rows (based on one cell-value) in google sheets

So I have this formula that copies rows (with data in col A) into a new range. Column A contains number indicating how many duplicates the row should yield in the result. Also the output rows gets sorted based on the value in column A.
=sort(arrayformula(vlookup(
transpose(split(query(rept(row(D2:D)&" ",A2:A),,9^9)," ")),
arrayformula({row(D2:D),{A2:A,D2:F}}),
{2,3,4,5},
0)),
1,
TRUE)
This is not exectly what I need thou. Instead of having a single value in the cells in column A that indicates how many times the row should be duplicated I need to have a text string like “2,3,5” in every cell in that column, where the individual numbers in the string indicates the position of the row in output (rather than the number of times the row should be copied).
For example, in the output I want the row with the string “2,3,5” to be copied three times. The output should have one of the rows be 2:nd from top, the other 3:rd from top, and the last one the 5:th from top.
If I could have the A2:A part of this range {A2:A,J2:N} instead contain the matching values for split(A2:A) I think it would do what I want.
This is a copy of my google sheet. Hopefully it's possible to understand what it is I'm trying to achieve.
https://docs.google.com/spreadsheets/d/1sp5DRBwFP0-aG-FvjUPKBmyylz0WoPB63ASOOdUGdnI/edit?usp=sharing

How do I subtract the sum of the values of a Google spreadsheet column from a fixed number?

This question pertains to Google Sheets only.
How do I subtract the sum of the values of a Google spreadsheet column from a fixed number?
I want to subtract the sum of the numbers in column D a sheet from 26 000. Column D will be dynamic; new numbers will be added every day to it.
If the values are in Column D, starting with row 5 (D5:D), then simply make a cell equal to your fixed number (or a reference to another cell) minus the sum of the values in your desired column. Here's what I wrote:
=26000-sum(D5:D)
Here's a link to a sample sheet so you can see it in action:
https://docs.google.com/spreadsheets/d/18TZPbvp4kqyYSe-fNH-6ByWh64Dl9QbECIGiCrSwLwM/edit#gid=0

Resources