How to specify a range with a relative cell reference? - google-sheets

I frequently have a "total" cell that should simply sum up all cells above it in the same column. Easy enough, but I also frequently add rows above that total, then have to manually update the total range to include those new cells.
A
1 10
2 20
3 30
=SUM(A1:A3)
When I add 3 rows above the "total" cell, I also have to remember to update the SUM formula to =SUM(A1:A6).
A
1 10
2 20
3 30
4 40
5 50
6 60
=SUM(A1:A3) *X INCORRECT. Does not include A4:A6*
Is there a way to define the formula as =SUM(A1:"The cell directly above this one") ? I have looked into INDIRECT(R[-1]C[0]), but mixing that notation with A1 notation, of course, results in an error: =SUM(A1:INDIRECT(R[-1]C[0])). Is there a better approach, or am I simply mis-using the notation?

try like this:
=SUM(INDIRECT("A1:A"&ROW()-1))

Related

Google Sheet - It's possible to array sum function in the following condition?

Would it be possible to use arrayformular for this condition?
Sum all the rows that PID are the same, the result should be as in the image.
I tried this code, but I think it's too long, and if the PID exceed over 20 rows, it would not work.
=IF(A3<>A2,BJ3+IF(A3=A4,BJ4,0)+IF(A3=A5,BJ5,0)+IF(A3=A6,BJ6,0)+IF(A3=A7,BJ7,0)+IF(A3=A8,BJ8,0)+IF(A3=A9,BJ9,0)+IF(A3=A10,BJ10,0)+IF(A3=A11,BJ11,0)+IF(A3=A12,BJ12,0)+IF(A3=A13,BJ13,0)+IF(A3=A14,BJ14,0)+IF(A3=A15,BJ15,0)+IF(A3=A16,BJ16,0)+IF(A3=A17,BJ17,0)+IF(A3=A18,BJ18,0)+IF(A3=A19,BJ19,0)+IF(A3=A20,BJ20,0)+IF(A3=A21,BJ21,0)+IF(A3=A22,BJ22,0),0)
With a table like this :
ID
Value
1
5
1
10
2
5
2
10
2
15
You have an expected output of :
ID
Value
Sum
1
5
15
1
10
blank
2
5
30
2
10
blank
2
15
blank
It is achievable with this formula (just drag it in your sum column) :
=IF(A2=A1,"",SUMIFS(B$2:B$12,A$2:A$12,A2))
It check if the ids are the same and then sum them, but only show them on the row where the id first appears
Found it on google by searching google sheets sum group by
The following in C2 will generate the required answer without any copying-down required:
=arrayformula(if(len(A2:A),ifna(vlookup(row(A2:A),query({row(A2:B),A2:B},"select min(Col1),sum(Col3) where Col2 is not null group by Col2"),2,false)),))
We are making a lookup table of grouped sums against the first row of each 'P#' group using QUERY, then using VLOOKUP to distribute the group sums to the first row in each group. Probably also doable using a SCAN/OFFSET combination as well, I think.

Spreadsheet: Sum of dynamic number of rows

I have a table in my Google Spreadsheet that looks like this :
Row
(A) Some day
(B) Some data
1
day 1
5
2
day 2
10
3
total
4
day 1
8
5
day 2
7
6
day 3
9
7
total
Where I can have multiple "day rows", but I don't know how many. It can be only 1 like it can be 20 "day rows". And I want the "total row" to automatically do a SUM of the "day rows" above.
Result expected :
Row
(A) Some day
(B) Some data
1
day 1
5
2
day 2
10
3
total
15
4
day 1
8
5
day 2
7
6
day 3
9
7
total
24
Where B3 is equal to SUM(B1:B2) and B7 is equal to SUM(B4:B6)
I am trying to do that without the App Script, just using Spreadsheet native functions.
I think I should be using the SUM function or the Query function, but I don't know how to dynamically get the right range. Do you have any idea how to do that ?
Thank you
In your example, column B would be a mixture of constants and formulas. That would require a script to deposit the formulas. However with an extra column, you can avoid scripts. In C2 enter:
=if(A2<>"Total","",sum($B$1:$B1)-sum($C$1:C1))
and copy downwards:
Basically we add column B and subtract any previous Totals in column C.
Another approach is to place the following single array formula in C1:
=ArrayFormula(IF(A:A="",, SUMIF(IF(ROW(A:A),ROW(A:A)), "<="&ROW(A:A),B:B) - SUMIF(IF(ROW(A:A), ROW(A:A)),"<="& VLOOKUP(ROW(A:A)-1, FILTER(ROW(A:A), A:A="total"), 1, TRUE), B:B)))
If you only want to see the values for the "total" rows, change the opening
IF(A:A=""
to
IF(A:A<>"total"
The short version of how it works is that a sum is made of all values up to the current row in B:B, and from that is subtracted any values up to the last listing of the word "total" in A:A.
paste in each cell in B column where A column = total
=INDEX(SUM(IFERROR(1*INDIRECT(ADDRESS(MATCH(INDEX(
COUNTIFS({"total";A:A}, {"total";A:A}, {"total";A:A}, "=total",
{ROW(A:A);ROW()}, "<="&{ROW(A:A);ROW()})-1, ROW()+1, 1),
COUNTIFS({"total";A:A}, {"total";A:A}, {"total";A:A}, "=total",
{ROW(A:A);ROW()}, "<="&{ROW(A:A);ROW()}), 0), 2)&":"&
ADDRESS(ROW()-1, 2)), 0)))

ARRAYFORMULA with repetition

I have two columns of data, and would like to distribute the elements of one of these columns over several rows. I can easily calculate the index of the element I need, but cannot figure out how to access the element.
A B Desired output Formula for index: =ARRAYFORMULA(IF(A:A,CEILING(ROW(A:A)/3+1),""))
1 11 22 2
2 22 22 2
3 33 22 2
4 44 33 3
5 33 3
6 33 3
7 44 4
How can I modify my formula for the index so that it yields the item of column B at the calculated index?
I tried =ARRAYFORMULA(IF(A:A, INDEX(B:B, CEILING(ROW(A:A)/3+1), 1), "")) but that only repeats the first element (22) 7 times.
Use Vlookup instead of Index:
=ARRAYFORMULA(IF(A:A,vlookup(CEILING(ROW(A:A)/3+1),A:B,2),""))
EDIT
It isn't necessary to use a key column, you could use something like this:
=ARRAYFORMULA(vlookup(CEILING(sequence(counta(B:B)*3)/3+1),{row(B:B),B:B},2))
assuming you wanted to generate three rows for each non-blank row in column B not counting the first one.
Or if you want to be different, use a concatenate/split approach:
=ArrayFormula(flatten(split(rept(filter(B:B,B:B<>"",row(B:B)>1)&"|",3),"|")))
(all the above assume you want to ignore the first row in col B and start with 22).

Negative References or reversing order of column for DATEDIF

I have a ascending sorted list of irregular dates in Column A:A:
A B C D (A:A,A2:A) E (A:A,A3:A)
2017-11-09 10 10 NA NA
2017-11-10 11 21 1 NA
2017-11-14 15 36 4 5
2017-11-15 22 58 1 5
Column C:C is a rolling sum of B:B. I'm trying to get arrayformula in D:D/E:E to find the datedif between current row (starting date) and X rows above (end date):
=ArrayFormula(DATEDIF(B:B-(X Rows),B:B,"D"))
The goal is to find range of change in D:D over X amount of days:
D:D - D:D-rowX / datedif (A:A-rowX, A:A)
i.e for 2 days on row C4:
(C4-C2) / datedif(C4-2,C4,"D")
(58-21) / datedif(C2,C4,"D")
37 / 5 = 7.4
for 5 days on row C10:
(C10-C5) / datedif(C10-5,C10,"D")
for 15 days on row C20:
(C20-C5) / datedif(C20-15,C20,"D")
I'm trying to calculate X for 1,2,3,4,7,28 rows up which means the array has to start that 1,2,3,4,7,28 rows down.
Right now, the array bugs out to bad reference because the first starting date is DATEDIF(B-X,B1,"D") where B-X is a invalid negative reference. Arrayformulas with bad values instead of bad references seems to just skip past errors and starts working once input are valid. But I can't figure out how to skip bad references. I've tried forcing start date with INDIRECT but can't get it to recognize value as a date. I also tried DATEDIF(B:B, B:B+X,"D"), which spits out the correct numbers but results are offset by X rows. I've tried reverse sorting A:A, =ArrayFormula(if(len(A:A),DATEDIF(SORT(A2:A,1,0),SORT(A:A,1,0),"D"),"")) it produces a reverse orders list of correct answers that I can't figure out how to flip back.
Seems like I'm missing something obvious?
EDIT: tried to clarify original post
Is there a easy way to displace an entire column?
Alternative Solution?
The formula roughly works but is not aligned to the correct row:
C D E
1 2 3
1 2 3
1 2 3
1 2
1
I just need it to display
C D E
1
1 2
1 2 3
1 2 3
1 2 3
To get things aligned, I can put in cell on row2 of Column F:
=array_constrain(ARRAYFORMULA(D:D),COUNT(A:A)-2,1)
Or cell in row3 of Column G:
=array_constrain(ARRAYFORMULA(E:E),COUNT(A:A)-3,1)
But if I try trigger teh formula from row1 via:
=arrayformula(if(row(A:A)>=2,array_constrain(D:D,COUNT(A:A)-2,1)))
It label everythign >=2 row false and still render D:D without displacing the cells the proper number of rows:
C D
1 false
1 2
1 2
1 2
1
EDIT: I'm closing the request, ended up just using vlookup(B:B-X) which provided an approximate enough result to work for my needs.
Short answer
Add the following formula to D1
=ArrayFormula({"N/A";ARRAY_CONSTRAIN(DATEDIF(A:A,A2:A,"D"),COUNT(A:A)-1,1)})
And the following formula to E1
=ArrayFormula({"N/A";"N/A";ARRAY_CONSTRAIN(DATEDIF(A:A,A3:A,"D"),COUNT(A:A)-2,1)})
Explanation
The solution use ARRAY_CONSTRAIN to return just the required result values and use a the array notation to add the required N/A values for the rows that as it don't have a pair to calculate the date difference.
REMARK:
Please note that the DATEDIF functions use the column A for the references as this column is the one that holds the date values.

Excluding the last value in a range from an aggregate calculation in Google Sheets

I have a Google Sheet with two columns of data. A is monotonically increasing with many duplicates (based on a coarse timestamp), while B is essentially random. There are many empty rows at the bottom waiting for future data. It resembles the following:
A B
1 5 43
2 5 77
3 13 8
4 21 34
5 27 68
6 27 90
7
8
9
10
I'm trying to write a few formulae which examine all of the (non-empty) values in a column except for the last one. For example, I would like to find the maximum value of B excluding the latest value, so the result should be 77 from B2 instead of 90 from B6.
If the values in the range were strictly increasing and unique, I could filter the values of A into C, excluding any values equal to the maximum value (only the last entry), and then take the MAX(..) of that range. However, my data does not have that property; the final value could be duplicated and the duplicates would be inappropriately ignored.
C D E
1 =FILTER(A:A, A:A < MAX(A:A)) =MAX(C:C) This produces A4's 21 instead of A5's 27.
A similar approach would work if we had a third column of incrementing indices to use:
A B C D E
1 5 43 9 =MAX(FILTER(C:C, A:A <> "")) Value of index in last populated row.
2 5 77 10 =MAX(FILTER(A:A, C:C < D1)) Maximum value from a row with lower index.
3 13 8 11
4 21 34 12
5 27 68 13
6 27 90 14
7 15
8 16
9 17
10 18
But I'm looking for a solution that doesn't require modifying the original spreadsheet, because that's not always possible. I can't just create a new IndexSheet with nothing but an an index column and join it in like this instead...
A B C
1 5 43 =MAX(FILTER(IndexSheet!A:A, A:A <> ""))
2 5 77 =MAX(FILTER(A:A, IndexSheet!A:A < C1))
...
...because that requires that the IndexSheet have the same number of rows as the data sheet, and would break as more data is added.
Without modifying the original data sheet, or relying on properties of the data (beyond values being numeric and rows being empty or full), is there any way to perform an aggregate calculation on a range while excluding the last value?
You can use indirect and address formulas to create dynamic range excluding the last row
=max(indirect("A1:"&Address(count(A:A)-1,1)))
The count function gives the number of non empty cells in the column A. You subtract 1 to exclude the last row.
You use that number to build an address using "A1:"&address(row no, Col no) which in your example case should be A1:$A$5
Use this string to reference your cells using the indirect method indirect(A1:$A$5) and pass the reference to the max function to determine the max in that range.
From another sheet try:
=MAX(Sheet1!B1:indirect("Sheet1!B"&count(Sheet1!B:B)-1))
We can use the FILTER() and ROW() functions to accomplish this:
D
1 =MAX(FILTER(Data!A:A,
ROW(Data!A:A) < MAX(FILTER(ROW(Data!A:A),
Data!A:A <> ""))))
We use FILTER(ROW(DATA!A:A), Data!A:A <> "")) to get an array of row numbers of non-empty rows, and use MAX(...) to take the last row number. We use this to exclude the last row by filtering out values from lower row numbers with FILTER(Data!A:A, ROW(Data!A:A) < ...). We apply MAX(...) to this filtered array and get the result we were looking for.

Resources