Calculate number of specific days in range - excel

How can I count the (fractional) count of specific day of the week in a range? For instance, if the range is from Monday, June 3 12:00 P.M. to Tuesday June 4 12:00 P.M., and I want to count the number of Mondays, the formula would return the result 0.5.
I've already found a formula for the integer number of days:
=SUM(INT((WEEKDAY($B2-x)+$D2-$B2)/7))
where x is the day of the week of interest (1 to 7). Of course, it would need to be modified to return the aforementioned result. How would I do so?

You can get the total number of Mondays (including both start and end date) with this version (you don't need SUM)
=INT((WEEKDAY($B2-2)+INT($D2)-INT($B2))/7)
...and then do an adjustment to take into account the start/end days, e.g.
=INT((WEEKDAY($B2-2)+INT($D2)-INT($B2))/7)-IF(WEEKDAY($D2)=2,1-MOD($D2,1))-IF(WEEKDAY($B2)=2,MOD($B2,1))
I don't really recommend it (it's much more resource-hungry) but this version should give you the same result
=SUMPRODUCT((TEXT(B2+(ROW(INDIRECT("1:"&ROUND((D2-B2)*1440,0)))-0.5)/1440,"ddd")="mon")+0)/1440
It tests every minute within the date range to ascertain whether or not it falls on a Monday (assumes that you don't go down to seconds)

Related

Find the trading date every 2 weeks (if Saturday or Sunday, then show Friday date)

For a backtesting trading system, I need to rotation my positions every 2 weeks, BUT if the day is a Saturday or Sunday, I need to take the Friday.
Semi-Monthly updates are made twice a month; mid-month and month end.
Mid-month updates are on the 15th calendar day of each month. Should the 15th be a weekend or holiday, the update will occur on the last trading day prior to the 15th.
For example, if the 15th is a Saturday then the update will occur on the close of Friday the 14th.
I need to return a list of dates of rotation based on a start date and end date.
Let's say, I need every 15 days from January 1st 2018 to 31st December 2018, it should return only the valid dates based on the rules described above.
The formula should be for Google Sheet or Excel.
I tried the following:
It is not returning exactly what I need, since the google sheet googlefinance formula allows to use daily and weekly intervals (1 or 7). See below the googlefinance definition (https://support.google.com/docs/answer/3093281?hl=en):
"interval - [ OPTIONAL ] - The frequency of returned data; either "DAILY" or "WEEKLY".
interval can alternatively be specified as 1 or 7. Other numeric values are disallowed."
You need to be more precise in your specifications. You've provided multiple inconsistent intervals. eg: Two weeks (which would be every 14 days and will always fall on the same weekday); every 15 days (which will NOT be mid-month and EOM over most time periods); mid-month and end-of-month.
I suggest developing formulas for each of your desired intervals.
Once you have developed those relevant formulas, depending on your desired interval, to avoid a date falling on a weekend (or holiday if required for your system), you can use the WORKDAY function:
=WORKDAY(computedDate+1,-1, [holiday])
If your computedDate is on a Saturday, or Sunday; by adding 1 day and then subtracting to the previous workday, Friday will result.
If your trading interval is every Two weeks, you only need to ensure that the First date is not on a Saturday or Sunday. For other intervals, you may have to apply the formula to every computedDate.

Count working days of a date interval (period) inside another given date interval (period)

I have several periods. Right now I use a formula to count how many days fall within one period taking another period into account.
For example period 1 has 26 days that fall within the project period. There can be many project periods, but period 1 to 12 will be the same always
Project Period : 20-12-2018 to 15-03-2019
Period 1: 15-12-2018 to 14-01-2019 (26 days)
Period 2: 15-12-2018 to 14-02-2019 (31 days)
Period x etc.
=IF(MAX(MIN($N$7;O4)-MAX($M$7;O3)+1)<0;0;MAX(MIN($N$7;O4)-MAX($M$7;O3)+1))
I need to find out how many working days of period 1,2...12. fall within the project period. The result should be 13 for period 1, when subtracting holidays and weekends. For that I'm using the
=NETWORKDAYS(M7;O4;Holidays)
How do I achieve that?
Assuming that cells A1 & B1 include start and end dates for the project and A2 and B2 are the start and end dates for period 1:
=NETWORKDAYS(MAX($A$1,A2),MIN($B$1,B2))
This will give you the number for workdays in the period (Monday through Friday).
If you want to exclude holidays like 31-12-2018 or 01-01-2019 you need to provide a list of holidays as the optional third argument.
For example, if you have a list of holidays you want to exclude in column F row 1 to 10 your formula would be
=NETWORKDAYS(MAX($A$1,A2),MIN($B$1,B2),F1:F10)
In case you do not want to exclude weekends use the DAYS function instead but be aware that in this function the first argument is the end date while the second argument is the start date.

Week number of a quarter

I'm trying to get the week number of a given quarter based on the date.
I currently have this formula
=1+(WEEKNUM(EDATE(Y4,-1)))-(WEEKNUM(DATE(YEAR(EDATE(Y4,-1)),
LOOKUP(MONTH(EDATE(Y4,-1)),{1,4,7,10}),1)))
But for January, it should be giving me 1 but it's giving me 10. Any suggestions?
How do you expect this to work at the start and end of the quarter? Default WEEKNUM function starts week 1 on the 1st of January every year and week 2 starts on the next Sunday after 1st January.
Assuming your quarter week numbers should work the same way, i.e. week 1 starts on the 1st of Jan/Apr/Jul/Oct and week 2 starts on the next Sunday then that's actually equivalent to counting Sundays since 6 days back into the previous quarter.
You can do that using NETWORKDAYS.INTL function, i.e. with this formula:
=NETWORKDAYS.INTL(EOMONTH(Y4,MOD(1-MONTH(Y4),-3)-1)-5,Y4,"1111110")
format result as number with no decimal places
NETWORKDAYS.INTL function is available in Excel 2010 and later versions - for older versions of Excel you can get the same results with this formula:
=INT((13-WEEKDAY(Y4)+Y4-EOMONTH(Y4,MOD(1-MONTH(Y4),-3)-1))/7)
(Expanded from comment)
when you choose a date in January, it's going back to December. 12 in your lookup array gives 10 as the result. Perhaps instead of EDATE, you should use EOMONTH(Y4,-1)+1, so you look at the 1st of the current month for your calculation
=1+(WEEKNUM(EOMONTH(Y4,-1)+1))-(WEEKNUM(DATE(YEAR(EOMONTH(Y4,-1)+1), LOOKUP(MONTH(EOMONTH(Y4,-1)+1),{1,4,7,10}),1)))
This is fairly interesting, since it changes with the year, and changes with what day of the week is the "start" of the week. So if a quarter starts on Saturday, and the week starts on a Saturday, the entire week is week 1. However, if it starts on a Sunday, week 1 is only one day long, and week 2 starts on Sunday.
The first question we have is, what day is it?
=DayCheck
Additionally, I'm going to call the start of each quarter the following:
Q1Start = Date(Year(DayCheck),1,1)
Q2Start = Date(Year(DayCheck),4,1)
Q3Start = Date(Year(DayCheck),7,1)
Q4Start = Date(Year(DayCheck),10,1)
The next question is, what's the first day of the week? We have some control over this with the Weekday function. For the sake of keeping it simple, Sunday is the start of the week.
Ok, that's our day. Next, what quarter is it?
`Quarter=ROUNDDOWN(MONTH(O16)/4,0)+1`
This gives us 1 for Q1, 2 for Q2, etc.
What day of the week is it now?
=WEEKDAY(DayCheck,1)
Ok, and now, what week are we on?
=WEEKNUM(DayCheck,1)
I'm going to put it together in a not very elegant fashion. I'm sure there's a better way out there.
=(Quarter=1)*((Weeknum(DayCheck)-WeekNum(Q1Start)+1)+(Quarter=2)*((Weeknum(DayCheck)-WeekNum(Q2Start)+1)+(Quarter=3)*((Weeknum(DayCheck)-WeekNum(Q3Start)+1)+(Quarter=4)*((Weeknum(DayCheck)-WeekNum(Q4Start)+1)
Try this:
=CHOOSE((MOD(WEEKNUM(Y4),13)=0)+1,WEEKNUM(Y4)-(ROUNDDOWN(WEEKNUM(Y4)/13,0)*13),13)
This will get the week number of a given date within a quarter.
I used this in one of my applications so you might be able to use it too. HTH.
Note: If you use 1st day other than Sunday, then adjust the WEEKNUM formula.
Can try this as I got this as combination of 2 formula
=WEEKNUM(A1,1)-(INT((MONTH(A1)-1)/3)*13)
second part - INT((MONTH(A1)-1)/3) gives us the quarter number of previous quarter which then multiplied with 13 weeks/quarter gives us how many weeks have passed in all previous quarter before current quarter.
First part - "WEEKNUM(A1,1)" gives us the week number of current week in the year.
so by deducting all the previous weeks in previous quarters from current week number of year, we get the current week number in current quarter.

Count number of hours between 2 dates excluding weekends and between 10pm - 10am?

I'm trying to calculate the number of hours between two date/times, excluding bank holidays and between 10pm and 10am.
I've got the Start Datetime in B3, and then End Datetime in G3. In SLA B13 - B22, I have a list of the bank holiday dates for 2015, and I am using the following forumal
=SUM(G3-B3-COUNTIFS(SLA!B13:B22,">"&B3,SLA!B13:B22,"<"&G3))
For some reason, I cant get this to exclude bank holidays, however I then also nee to take into account the 10pm - 10am bracket.
Can anyone help?
=NETWORKDAYS(B3;G3;SLA!B13:B22)*12
*12 because only 12 hours per day count (exclude time between 10 pm and 10 am)
I've tried your formula and it works fine for whole days for me, as long as you format the cell containing the formula as general or number to avoid formatting it as a date. It doesn't exclude weekends of course which presumably is what you want.
You might want to add 1 to make it inclusive, make > into >= and < into <= in case the start and end dates are bank holidays and multiply by 12 to get the result in hours:-
=(G3-B3+1-COUNTIFS(SLA!B13:B22,">="&B3,SLA!B13:B22,"<="&G3))*12
You could calculate the hours worked directly from datetime values: the basic formula would be:-
=(G3-B3)*24-(INT(G3)-INT(B3))*12
i.e. subtracting 12 hours for each complete day.
You could also exclude the holidays (if between the start and end dates):-
=(G3-B3)*24-(INT(G3)-INT(B3)+COUNTIFS(SLA!B13:B22,">"&B3,SLA!B13:B22,"<"&G3))*12
i.e. subtracting a further 12 hours for each day's holiday.
One more thing you could do is to apply an adjustment for start and end time to the Networkdays formula by adding:-
=MOD(G3,1)-MOD(B3,1)

Lookup formula to subtract last input data point

I am trying to write a formula that will subtract my current value from my last data point. I run a production report and over the weekends we do not produce any product. I would like the value typically from a Friday be used for calculating the change in production for Monday.
I have an input page for all the data and then a calculation page for the reports.
(Input page)
Thursday 1000
Friday 5000
Saturday "blank"
Sunday "blank"
Monday 2000
Ideally the output page would look something like this:
(output)
Friday 4000
Saturday "blank"
Sunday "blank"
Monday -3000
Having the last inputted data (being the 5000 value from Friday) subtracted from the Monday value of 2000.
Please try:
=IF(LEFT(A2,1)="S","",Sheet4!B3-INDEX(Sheet4!B:B,MATCH(1E+100,Sheet4!B$1:B2)))
with adjustment of cell references to suit.
My idea was:
=IF(B2=0,"Nonproductive",IF(B1<>0,B2-B1,B2-OFFSET(B2,-(COUNTIF(OFFSET(C2,-3,0,3),"nonproductive")+1),0)))
Imagine you've got your days in column A, and production in column B.
Copy and paste this from cell C2 onwards.
What this does, is first test if there was any production on a given day, if there was 0 production, or in your above example, "blank", then it will give the value "nonproductive". If there was production on that day, it will test if the day prior had any production, if it did, it will simply deduct yesterdays from today. If the day prior had no production, it will count the number of times that "nonproductive" has occurred in the 3 days prior and add that to the number of days in the past it looks.
On a long weekend, it would count 3 nonproductives, so it would compare Monday to Thursday.
Monday - (1 (default offset for the day prior) + 3 (due to nonproductives)
Monday - 4 = Thursday.
Notes:
This will not work for the first 2 rows because the offset will be trying to find cells that don't exist. Even if
The only problems arise when a break greater than 3 days happens, or you have a day off, then a day on, then a day off, then a day on, such as a Tuesday bank holiday, this is because there are 2 "nonproductive" days in the 3 days prior, but we only want it to increase the offset by 1. This could be avoided through continuing a chain of "if" condition checks, but it doesn't sound as though that is required.
This will work for a 2 or 3 day weekend, and some mid-week holidays

Resources