In Google Sheets, how to check if Cell A (Date) is within the Date range of Cell B and C - google-sheets

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.

Related

Is there a rolling 7 day formula for Google sheets that will sum up the figures in one column based on dates in another column?

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)

Why can INDEX(...,COUNTA(...),..) find the last value of one array, but not another?

I'm learning basic =ARRAYFORMULA usage for a finance spreadsheet:
https://docs.google.com/spreadsheets/d/12cAGuUBzIo0LPbmtqWJZNFgjt94f1ybGoj6x2g0c2Y0/edit?usp=sharing
First, I used =GOOGLEFINANCE at B1 to pull up stock prices for a given date range in Column C
=GOOGLEFINANCE(A1,"price",DATE(2020,1,1),DATE(2020,5,30),"DAILY")
Then, I used simple arithmetic to multiply by number of shares at D2 and dragged the formulas down to get a nice column of values
=C2*20
Then, I used INDEX and COUNTA to pull out the last value of Column D at F3 === Great!
=INDEX(AAPL!D2:D,COUNTA(AAPL!D2:D),1)
Next, I turned my arithmetic formula into an ARRAYFORMULA at G2 === Cool!
=ARRAYFORMULA(C2:C103*20)
Of course, the problem with that ARRAYFORMULA is that I would have to manually change the array name in G2 every time the date range updated.
That is, instead of C2:C103, I would need to change the reference to C2:104 to get the columns to match === Rookie mistake!!!!!
So, I got smart with an ARRAYFORMULA containing a IF(ISBLANK(),...,...) at J2
=ARRAYFORMULA(IF(ISBLANK(B2:B),"",C2:C*20))
Column J stays fully populated with the correct values for any date range === !!!!!!!
But now the =INDEX(AAPL!J2:J,COUNTA(AAPL!J2:J),1) at L3 can't find the last value in Column J
Whaaat???
I've tried everything I can think of
It works if I use =INDEX(AAPL!J2:J104,COUNTA(AAPL!J2:J104),1) but that would defeat the purpose, since the reference J:J104 is going to change as the dates change
WHY???
I get the same results in both cells "L3" and "L5" when using:
=ARRAYFORMULA(IF(ISBLANK(B2:B),,C2:C*20))
Check that you are not returning a blank string ("") in your IF like: "=ARRAYFORMULA(IF(ISBLANK(B2:B),"",C2:C*20))" doing so will fill the cells up to the last row in the sheet with empty strings, thus when you use COUNTA(AAPL!J2:J),1) you get a lot more cells than you would expect, these extra cells are the ones you filled with blank strings in the array formula.
On the contrary when you limit the =INDEX(AAPL!J2:J104,COUNTA(AAPL!J2:J104),1) to cells with numbers only it doesn't mix strings and numbers in the calculation and you are naturally get the expected results.

Formula to Test for Blanks in two Columns Against a Third Column

So I am using a conditional formatting custom formula to highlight a cell if the column it is summing from another spreadsheet has blanks, but I don't know how to do it with changing ranges. Basically what I want to do is use a third column, say column A, to determine the length of the range (of rows) I want to scan with CountBlank, and if it picks up a blank in there to return a "True".
So basically:
Column A Column B Column C Column D
Person 1 5:30AM 3:00PM 9.5
Person 2 5:00AM 8
Person 3 4:30AM 4:00PM 10.5
So ideally, the cell sums the fourth column with a different function (already have that), and it conditionally formats itself if a blank is picked up in Columns B or C, going all the way down to the last row of column A that has a value. Any help here would be appreciated, thank you.
If I understand you correctly, the custom formula for conditional formatting should be:
=or(countifs(A2:A,"<>",B2:B,""),countifs(A2:A,"<>",C2:C,""))
Suppose you had a list of people's names starting in F2 and their total hours starting in G2. Then you could alter the formula to:
=or(countifs(A$2:A,F2,B$2:B,""),countifs(A$2:A,F2,C$2:C,""))

Google sheets Average function is not calculating correctly

I'm using Google Sheets to create a financial record.
What i'm trying to do is create a formula that takes 3 columns in my data range in to consideration. The three columns are a date, a word and a number.
The first part of the formula will check that the date is the current month (not within 30 days, but the current month). The second part will check whether the word "Yes" is present in the second column, and if those two are true, then it will take the average of column 3 for all other rows that are also completely true.
Column C is Date
Column W is Word
Column Y is Number
I've tried a number of methods, the first one was to use a average IF function, where i used a filter to check the dates, and then the word Yes in the criterion. This resulted in a number, although it was incorrect, as the formula first gathered the sequence of Yes and No's, once it had the sequence it applied it to the third column but it started from my earliest entry (not my current month). This code is below.
So alternatively i tried another method. Which was using a query function. Although i'm stuck on how to compare the month of a date to the current today() month. This gives no results, even though the current month is 8, and the dates month is also 8. I've also inputted this code below.
=AVERAGEIF(filter(W8:W800,month(C8:C800)=month(today())),"Yes",Y8:Y800)
=query(query(A8:Z800,"select month(C)+1, W, Y where W ='Yes'",0),"select Col1, Col3, Col4 where Col1 ='"&month(today())&"'",1)
results explained in background
Your nesting is a bit off. If you're using FILTER, use plain AVERAGE instead of AVERAGEIF, and make sure you're grabbing the right column to aggregate. Lastly, don't forget to wrap in IFERROR to handle your empty case.
=IFERROR(AVERAGE(FILTER(Y8:Y800, MONTH(C8:C800)=MONTH(TODAY()), W8:W800="Yes")), 0)
if you have multiple criterions you need to use AVERAGEIFS instead of AVERAGEIF
=ARRAYFORMULA(AVERAGEIFS(C2:C, B2:B, "yes", MONTH(A2:A), MONTH(TODAY())))

Google Sheets: Script to find duplicates in a column and add their corresponding values from another column

I'm trying to accomplish a lot with little knowledge, but I have a spreedsheet with data that i want to convert into something readable that I can then display on my website.
Anyways, in column A I have a list of dates, column B a list of names, and column c a list of contributions. What I would like to do is first total the amount of contributions in C, for a date range in A.
I would then like to find all the duplicates in Column B(repeat donors), and total their contributions in C.
There is a VBA, that I found on here that would accomplish the second task, but ... I'm using Google Sheets. What I've been doing is sorting the sheet and totalling everything manually.
What I would like to do is first total the amount of contributions in
C, for a date range in A.
Use SUMIF
I would then like to find all the duplicates in Column B(repeat
donors), and total their contributions in C.
Assuming you have 100 records, starting from 1st row for below code.
In cell D1, put =UNIQUE(B1:B10), it will fill D column with unique values of given range (B1:B100).
In cell E1, put =SUMIF(B$1:B$100,D1,C$1:C$100)
Repeat same formula in E column

Resources