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)))))
Related
Spreadsheet
So Basically i'm building a sheet to monitor my NW, my problem is simple, i need the 2023 column to adapt to whatever last number on that row there is, for income i just sum values, but for example the total amount in my bank account is different every month, i just need 2023 column to track that, i update the 31 of each month.
With the formula you can see in the screenshot i have some problem, if i input the number alright, if the value is defined by other formulas it won't show up. Any solutions?
THANKS
Only formula present in the row
Another formula from the comments that seems to not work
I tried =INDEX(X:Y;1;COUNTA(X:Y)) but it won't update if the last number in the row is generated by other formulas.
You can use this formula to find the last value of each row in the range D2:O26. Adapt it as needed!
=BYROW(D2:O26,LAMBDA(e,XLOOKUP(1,INDEX(1/(e<>"")),e,,0,-1)))
Explanation:
BYROW creates an array formula in each row of the range. To each row, here denoted as e, the specified lambda function is applied.
INDEX(1/(e<>"")) returns an array with 1 in places where cell is not empty, and #N/A for empty cells.
XLOOKUP finds the index of the last occurrence (parameter search_mode set to -1) of 1 (first parameter) in the array returned by INDEX and returns corresponding value in the row.
You haven't show what formula in columns Jan-Dec causes you problems. If formula produce values 0 in case you don't what them to count, you can use countif to filter them out:
=INDEX(D13:O13,1,COUNTIF(D13:O13,">0"))
Same as your solution, this only works if columns are filled sequentially - if there are no gaps in each row, e.g. Jan and Mar are filled while Feb is blank.
If you need a more general solution, you may go with series of nested if(isblank(). Here is an example for the first three months, to get an idea:
=INDEX(D13:O13,1,IF(ISBLANK(F13),if(ISBLANK(E13),1,2),3))
I am wondering if there is a way to use column comparisons in a SUMIF or SUMIFS function or the DSUM function even. Specifically, I want to compare one value in a row with another value in the same row of data.
For example, let's say I have the following table of data:
Date 1 | Date 2 | Money Earned
1/2/22 | 1/5/22 | $23.00
1/5/22 | 1/3/22 | $11.00
I want to write a formula that sums the values in the 3rd column if Date 1 is last week and if Date 2 is later than or equal to Date 1.
I have completed a workaround solution with 2 hidden columns as follows:
Last week --> =IF(EQ(WEEKNUM(TODAY()) - WEEKNUM(A2), 1), "YES", "NO")
Date 2 After Date 1 --> =IF(B2>=A2, "YES", "NO")
Then I use:
=SUMIFS(C2:C3,D2:D3,"YES",E2:E3,"YES")
It all works, but I am trying to learn to do this a cleaner way without hidden columns that someone might move or delete.
All the examples I can find of SUM or SUMIF rely on the criteria being compared to a singular value that is either hardcoded in the formula or the value of a single cell. I want to compare data from column 1 to column 2 directly for each row. Is this possible with these formulas or any formula in Google Sheets?
In order to check if a date was "last week", you need to consider that the current week might be the first one of the year. So, instead of comparing the WEEKNUM of today with the WEEKNUM of the other date, just compare it to the WEEKNUM of 7 days ago.
Secondly, instead of using "YES" or "NO", you could have used TRUE/FALSE values and remove the need to write IF statements, like this:
last week: =(WEEKNUM(TODAY()-7)=WEEKNUM(A2))
date 2 after date 1: B2>=A2
From there, while it is possible to build an arrayformula for SUMIFS or QUERY, it is easier to just filter the table and sum the results from column C:
=SUM(FILTER(C2:C,WEEKNUM(TODAY()-7)=WEEKNUM(A2:A),B2:B>A2:A))
I have the below formula which in theory should work but it adds in additional numbers giving an incorrect figure.
=SUMIFS(Sheet1!F3:F, Sheet1!B3:B, "<="&TODAY()-3, Sheet1!B3:B, ">="&TODAY()-10)
My dates (yyyy-mm-dd) are in column B and my figures are in column F.
I want this to provide the total sales figure for a rolling 7 days but to have a 3 day delay.
I have attached my example workbook: https://docs.google.com/spreadsheets/d/1koU28NWl9T0D2Pip0caa1tWXrAeQFHtjgEz1a1kngvI/edit?usp=sharing
your formula should be:
=SUMIFS(Sheet1!F2:F, Sheet1!B2:B, "<="&TODAY()-3, Sheet1!B2:B, ">="&TODAY()-9)
&
=AVERAGEIFS(Sheet1!F6:F, Sheet1!B6:B, "<="&TODAY()-3, Sheet1!B6:B, ">="&TODAY()-9)
you can test the output with filter like:
=FILTER(Sheet1!B2:F, Sheet1!B2:B<=TODAY()-3, Sheet1!B2:B>=TODAY()-9)
I'm sorry to ask this, I have not any code skills and I've trying to figure that out for a few hours now.. I think an image will be better for you to understand what I want:
I want A2 to show the sum of the products in G:G that fit certain conditions (2020,jan,buy). I haved tried several formulas but I came up with this one as the closest, I think, but still won't work:
=arrayformula(SUMIFS(E:E=B1,F:F="jan",G:G="buy",H:H))
Can anyone explain me how to achieve that?
Thanks very much :)
Please use this formula in A2 it will work
=sumifs(G2:G100,D2:D100,2020,E2:E100,"jan",F2:F100,"buy")
So basically, sumifs formula is right one as you want to check for multiple conditions.
so this is how this formula work
=sumifs(sum_range,criteria_range1,criteria1,criteria_range2,criteria2,...)
In your case your same range is column 'G' so if you have a finite range like only 25 rows you have in your table then instead of G2:G100 you can use G2:G25 as G1 is containing label and make sure that all other ranges also similar to the range of column G. for example if you take range of G2:G100 means 99 rows then you should take E2:E100 or E3:E101(range of 99 rows, that rows must be 99 and series start and end number is as per your requirement, similar case for other columns in this formula)
you have to check 2020 in column D, so you criteria_range1 is of D column I took it D2:D100 and criteria 1 is 2020 as it's a number it doesn't need double quotes
criteria 2 is you need to check Jan in column E so criteria_range2 is column E I took it E2:E100 and criteria 2 is "jan" as it's not a number so I took it in double quotes.
criteria 3 is you need to check 'buy' in column F so Criteria_range3 is column F. I took it as F2:F100 and criteria 3 is "buy" again it's not a number so took it under double quotes.
I have a sheet with a timeline that shows a month per row in column A and an amount in USD next to that month in column B.
I want to be able to specify amounts in column G with a start and end date for that amount in columns E and F.
What I am trying to achieve is that the values in column B are automatically calculated by looking at the start and end dates specified in columns E and F and then taking the corresponding value from column G if the date in column A falls in between the date range specified in E and F.
I have found many suggestions for similar problems online but wasn't able to get any of them to work for my specific case. Any help is very welcome
You could do it as an array formula like this:
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("`E3:E"&count(E3:E)+2)),"YYMM"))*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))*transpose(indirect("G3:G"&count(G3:G)+2)),(INDIRECT("G3:G"&count(G3:G)+2)+2)^0))
The idea is to develop a 2D array where the rows are the months and the columns are the amounts for matching time periods. Then use the standard Mmult method to get the row totals of the array.
Using indirect for the ranges makes the formula longer but using full-column references would be slow as it would result in a nearly 1000 X 1000 array for a default-sized sheet.
EDIT 1
Or shorter
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("E3:E"&count(E3:E)+2)),"YYMM"))
*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))
,INDIRECT("G3:G"&count(G3:G)+2)))
because you can combine the row totals step with multiplication by column G.
EDIT 2
Alternatively you could just employ a much simpler pull-down formula using SUMIFS:
=ArrayFormula(sumifs(G$3:G,eomonth(E$3:E,-1)+1,"<="&A2,F$3:F,">="&A2))
This uses Eomonth to change all the start dates to the first of the month so they can be compared to the dates in column A correctly. The formula still has to be entered as an array formula because of the Eomonth calculation.
Note
The equivalent pull-down formula to the original array formulas above would be
=ArrayFormula(sumifs(G$3:G,text(E$3:E,"YYMM"),"<="&text(A2,"YYMM"),text(F$3:F,"YYMM"),">="&text(A2,"YYMM")))
but this gives zero for all rows - the reason for this is not obvious to me at time of writing.