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