How to Calculate the difference between numbers in a Google sheet row? - google-sheets

I want to calculate the difference of numbers that are inputted in a Google sheet row like this:
Row 1: 35 | 37 | 39 | 38 and So on...
I found a formula that can get the last entered number in Row 1:
=IFNA(LOOKUP(1, INDEX(1/(Row 1:1<>"")), Row 1:1),"")
However, I need to sum these values like this:
Row 1 Output: (37-35)+(39-37)+(38-39) = (2+2-1)=3
Any idea how to solve this?
Thanks!

Use INDEX/COUNTA to get the last column and OFFSET to shift the range by -1:
=LAMBDA(rg,SUMPRODUCT(rg-OFFSET(rg,0,-1)))(INDEX(1:1,2):INDEX(1:1,COUNTA(1:1)))

Give a try to following formula-
=SUM(INDEX(B1:INDEX(1:1,COUNTA(1:1))-A1:INDEX(1:1,COUNTA(1:1)-1)))

Row 1 Output: (37-35)+(39-37)+(38-39) = (2+2-1)=3
Notice that the middle numbers (37 and 39) cancel out so you just need to subtract the first number from the last one
38-35 = 3
So this should be enough
=index(1:1,count(1:1))-A1

Related

Calculate Days between series of dates in a range

I wanted to calculate the difference of numbers that are inputted in a Google sheet row like this:
Row 1: 35 | 37 | 39 | 38 and So on (until last non-empty cell)...
And the output would be:
Row 1 Output: (37-35)+(39-37)+(38-39) = (2+2-1)=3
the suggested formula for this calculation is:
=LAMBDA(rg,SUMPRODUCT(rg-OFFSET(rg,0,-1)))(INDEX(1:1,2):INDEX(1:1,COUNTA(1:1)))
Taking forward to this concept, now I want to calculate days between dates instead of numbers.
something like this (date format:yyy-mm-dd):
Row 1: (blank cell) | 2022-07-06 | (blank cell) | 2022-07-07 | 2022-07-08 and So on (until last non-empty cell)...
Expected Row 1 Output:(2022-07-07-2022-07-06)+(2022-07-08-2022-07-07) = (1+1)=2
I used the above formula but I reckon formula is producing incorrect output because it does not exclude blank cells in between these dates, that's why it gives 44749 an an output,image is also attached:
Here is the how the table looks like in sample sheet in case you want to test it.
Desired Output (No. of Days)
Date1
Date2
Date3
Date4
Date5
Date6
3
2022-07-06
2022-07-07
2022-07-08
2022-07-09
(G2-E2)+(E2-D2)+(D2-B2)
1
2022-07-06
2022-07-07
(G4-C4)
Table is starting from column A to Column G. Any guidance would be much appreciated, thank you.
There are many ways of finding the first and last non-blank cell in a row, but let's use xlookup for the sake of argument:
=ArrayFormula(xlookup(true,(B2:2<>""),B2:2,,0,-1)-xlookup(true,(B2:2<>""),B2:2,,0,+1))
But people just don't believe me when I tell them that (G2-E2)+(E2-D2)+(D2-B2) simplifies to G2-B2. It's basic algebra folks!
Or this is equivalent to my original formula but doesn't lend itself to being written as an array formula:
=index(filter(B2:2,B2:2<>""),count(B2:2))-index(filter(B2:2,B2:2<>""),1)
EDIT
Possible array formula (but could be a bit inefficient):
=iferror(byrow(B2:Z,lambda(r,index(filter(r,r<>""),count(r))-index(filter(r,r<>""),1))))
Also if dates are in ascending order left to right (or more exactly, if the first date is the smallest and the last date the largest), and because min and max ignore blanks, you can simplify the original formula to:
=max(B2:2)-min(B2:2)
or with byrow:
=ArrayFormula(byrow(B2:Z,lambda(r,if(min(r)=0,,max(r)-min(r)))))

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.

Google Sheets: Is there a way to make an Array for a sequence that grows to a repeat given number of times

I have managed to make this formula:
=IF(COUNTIF($A$1:A1,A1)=$B$1,A1+1,A1)
Is making an incremental list in the same column, starting with 01, and on cell B1 I am instructing how many times this number should repeat (up to 5 times per number). So, if I say repeat 2 times it looks like this:
Column A
1
1
2
2
How can I make it an Array Formula? I'm barely starting on Arrays so I'm just trying to learn as I go.
you can try to solve this with the following formula =ArrayFormula(flatten(if(SEQUENCE(1,C1),SEQUENCE(C2)))) but you should specify the value limit in cell C2 to which the numbers must increase, otherwise the formula will loop
Try this
=ARRAY_CONSTRAIN(arrayformula(query(flatten(split(rept("|"&A2:A,B2:B),"|")),"select * where Col1 is not null")),SUM(B2:B),1)
https://docs.google.com/spreadsheets/d/1qXy-hzWUnsUmAa-8aV5IHOyZABGD_h8ajICLwdyNge8/edit?usp=sharing
Try Sequence() with ROUNDUP() function.
=ArrayFormula(ROUNDUP(SEQUENCE(B1*B1)/B1,0))
When enter 3:

Google Sheets - dynamic lookup sum

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))

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.

Resources