A formula to average data from the last month - google-sheets

I am trying to make a formula that will give an average of the numbers from column 'D' of 'TEST SHEET' into another Tab. That is easy enough, but I only want to use the data from the last 30 days.
I've been trying to create a formula, but I am new to these things and have only received error messages.
TEST SHEET

Try the following formula:
=AVERAGEIF('TEST SHEET'!A3:A,">"&today()-30,'TEST SHEET'!D3:D)
To get average for the days between last 60 and last 30 days ago, try the following formula:
=AVERAGEIFS('TEST SHEET'!D3:D,'TEST SHEET'!A3:A,">"&today()-60,'TEST SHEET'!A3:A,"<"&today()-30)

Related

Filtering Overlapping Dates within Set Date Range

I have a couple of questions I need for a semi-annual report:
I have a list of interns, each with their own start and end dates of service. I am looking to filter the list of those who worked within a given range of dates. I want any intern who has their term of service that overlaps with the range dates.
I am also looking to calculate how many weeks + months they worked within the same range of dates.
Here is the Spreadsheet.
I have tried your basic filter with date ranges, but I am not able to figure out the overlapping aspect of the list. For example, if the date range is 2021-2022, and an intern worked from 2020-2022, it would not filter it, as the start date is outside of the range.
Any help would be greatly appreciated!
This formula will work for all 3 columns:
=FILTER(
{
A2:A,
ARRAYFORMULA(
1+
TRUNC(ARRAYFORMULA(IF(C2:C<G2,C2:C,G2)-43101)/7)-
TRUNC(ARRAYFORMULA(IF(B2:B>F2,B2:B,F2)-43101)/7)
),
ARRAYFORMULA(
1+
DATEDIF(
ARRAYFORMULA(IF(B2:B>F2,B2:B,F2)),
ARRAYFORMULA(IF(C2:C<G2,C2:C,G2)),
"M"
)
)
},
ARRAYFORMULA((IF(C2:C>G2,G2,C2:C) - IF(B2:B<F2,F2,B2:B)) >0 )
)
For calculating the months the formula uses the DATEDIF function.
For calculating the weeks, I am choosing as reference an arbitrarily defined week zero starting on Monday, 2018-01-01 (day 43101 in the Network Time Protocol).
Link to Google Sheet
Check for Overlap
For a simple pull-down formula to check if each intern has any time overlapping the range, use the standard Overlap Formula:
Min(end1,end2)-Max(start1,start2)
which gives
=if(min(C2,G$2)>=max(B2,F$2),"Yes","No")
or for an array formula:
=ArrayFormula(lambda(start,end,if(if(end<G2,end,G2)>=if(start>F2,start,F2),"Yes","No"))
(filter(B2:B,A2:A<>""),filter(C2:C,A2:A<>"")))
Months
#Markus's approach is perfect, but for an alternative one could use:
=datedif(eomonth(max(B2,F$2),-1)+1,eomonth(min(C2,$G$2),0)+1,"M")
(for Jeremy as an example) in other words dial the start date back to the first of the month and the finish date forwards to the start of the next month and use Datedif to get the overlap in full months. The point of this is that it could be adapted for weeks as well.
With the lookup on names in column I2:
=datedif(eomonth(max(xlookup(I2,A$2:A$8,B$2:B$8),F$2),-1)+1,eomonth(min(xlookup(I2,A$2:A$8,C$2:C$8),$G$2),0)+1,"M")
or as an array formula:
=ArrayFormula(lambda(rNames,Names,start,end,datedif(eomonth(if(xlookup(rnames,Names,start)>F2,xlookup(rnames,Names,start),F2),-1)+1,eomonth(if(xlookup(rnames,Names,end)<G2,xlookup(rnames,Names,end),G2),0)+1,"M"))
(filter(I2:I,I2:I<>""),filter(A2:A,A2:A<>""),filter(B2:B,A2:A<>""),filter(C2:C,A2:A<>"")))
Weeks
You have a few options here.
(1) Having found the number of complete or partial months of overlap, just divide the number of days in those months by seven to get corresponding number of weeks:
=ArrayFormula(lambda(rNames,Names,start,end,quotient(datedif(eomonth(if(xlookup(rnames,Names,start)>F2,xlookup(rnames,Names,start),F2),-1)+1,eomonth(if(xlookup(rnames,Names,end)<G2,xlookup(rnames,Names,end),G2),0)+1,"D"),7))
(filter(I2:I,I2:I<>""),filter(A2:A,A2:A<>""),filter(B2:B,A2:A<>""),filter(C2:C,A2:A<>"")))
(2) Use the same logic as for months to get number of partial or complete working weeks of overlap:
=ArrayFormula(lambda(rNames,Names,start,end,quotient(datedif(if(xlookup(rnames,Names,start)>F2,xlookup(rnames,Names,start),F2)-weekday(if(xlookup(rnames,Names,start)>F2,xlookup(rnames,Names,start),F2)),if(xlookup(rnames,Names,end)<G2,xlookup(rnames,Names,end),G2)+7-weekday(if(xlookup(rnames,Names,end)<G2,xlookup(rnames,Names,end),G2),3),"D"),7))
(filter(I2:I,I2:I<>""),filter(A2:A,A2:A<>""),filter(B2:B,A2:A<>""),filter(C2:C,A2:A<>"")))
(3) Just get number of complete weeks of overlap by dividing number of days by 7:
=ArrayFormula(lambda(rNames,Names,start,end,quotient(datedif(if(xlookup(rnames,Names,start)>F2,xlookup(rnames,Names,start),F2),if(xlookup(rnames,Names,end)<G2,xlookup(rnames,Names,end),G2),"D"),7))
(filter(I2:I,I2:I<>""),filter(A2:A,A2:A<>""),filter(B2:B,A2:A<>""),filter(C2:C,A2:A<>"")))

datedif formula -how to create a 0 Days result if the time between the cells is zero?

I'm looking for one heck of a complicated formula!
I'm counting the years, months, and days between two cells in Google Sheets.
In the first row in the image below I'm counting the years, months, and days between column C and column D. In the second row, I'm doing the same, but I also need to add that if the result is zero days then the output/answer should just be 0 Days. How do I do that?
This is the formula I've used so far to get what you see in the image.
=datedif($C3,$D3,"y")&" Yr "&datedif($C3,$D3,"ym")&" Mths "&datedif($C3,$D3,"md")&" Days"
sample out put
Try
=if(datedif($C3,$D3,"y")=0,,datedif($C3,$D3,"y")&" Yr ")&
if(datedif($C3,$D3,"ym")=0,,datedif($C3,$D3,"ym")&" Mths ")&
datedif($C3,$D3,"md")&" Days"
You should replace the "," with the ";"
This way the syntax of your formula would be correct. I believe it works like this:
=datedif($C3;$D3;"y")&" Yr "&datedif($C3;$D3;"ym")&" Mths "&datedif($C3;$D3;"md")&" Days"
See image:

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)

Time series chart till current week

I have this google sheet which contains 3 fields.
Week number
Weekly website page views for this year. It contains data until current week (19) this year.
Week website pageviews for last year. It contains data for all the 52 weeks during previous year.
In data studio, I am using a time series chart to visualize this data.
Following are a couple of issues that I want to fix
The chart is displaying data for all 52 weeks since last year's page views field contains data for 52 weeks. I only want the chart to display till the current week. That is, the chart x-axis should be until week 19 only.
In case the chart is displayed in its current state will all 52 weeks, the blank cells for weeks in the current year are displayed as 0. (see the blue line coming down to zero and extending till week 52). How to avoid this and just end the line on week 19.
Please advise how to go about this.
Update:
Based on the answer
=query(Data!A1:C, "where B is not null", 1)
I have stripped the data for the remaining weeks but the chart is still showing week till 52, though the sheet only contains data till week 19. The week field only contains week numbers from 1 to 19.
To create a helper table that only includes rows where column B contains data, use Insert > New sheet and this formula in cell A1 of the new sheet:
=query(Data!A1:C, "where B is not null", 1)
Then use the helper table as your data source.

Need help creating a formula for dynamic average of last 4 weeks expenses

I am looking to create a spreadsheet that my staff fill out, it then gives me a master sheet with all the data, then I import dynamically to my financial spreadsheet telling me the average cost of my client over the last 30 days.
I am looking to create an AVERAGE formula of the last 30 days when Date = Today (Monday) (I want the weekday Monday as that's when staff hand in invoices)
Hope this makes sense, it's really tough!
Here's a video of me explaining my desired outcome
https://www.loom.com/share/3a9cb75052b246d1af2ba2f9ce9180a7
I've followed several guides & can't figure it out.
=ArrayFormula(iferror(query(average(if(today() - weekday(today(),3)-30)))))
I expected $90 average and I just get blank
You could use this formula:
=AVERAGE(VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-6,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-13,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-20,A:H,2,FALSE))
To break it down in to its component parts, the AVERAGE is taken from VLOOKUP results:
VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE)
The VLOOKUP is looking for the last Monday from the current date:
TODAY()-WEEKDAY(TODAY(),2)+1
Then
TODAY()-WEEKDAY(TODAY(),2)-6
and so on...
When using on your sheet, you will have to specify the column you want to reference in your look up, for colunm B (brand1) use: A:H,2,FALSE), for colunm C (brand2) use: A:H,3,FALSE), for colunm d (brand3) use: A:H,4,FALSE) and so on...
=INDEX(QUERY({INDIRECT("A2:D"&ROW()-1)},
"select avg(Col2),avg(Col3),avg(Col4)
where Col1 <= date '"&TEXT(TODAY(), "yyyy-MM-dd")&"'
and Col1 >= date '"&TEXT(TODAY()-30, "yyyy-MM-dd")&"'"), 2, )

Resources