In Excel, how to calculate # of days between a date range, counting only specific days of week - excel

I'm trying to get # days between 2 dates, but had to count only days of the week specified per pair of dates in questions.
For example:
Date of operation = 9/1/2013 and 5/16/2014
Days of the Week in operation = Monday, Wednesday, Friday
I want to calculate total number of days in actual operation, given the days in operation per week.

If you have Excel 2010 or later you can use NETWORKDAYS.INTL function which allows you to customise which days are counted, e.g. the following formula will count Mondays, Wednesdays and Fridays between a start date in A2 and an end date in B2 (inclusive) while excluding holiday dates listed in H2:H10
=NETWORKDAYS.INTL(A2,B2,"0101011",H$2:H$10)
The string "0101011" defines the days to include - 0 is included, 1 is excluded - it starts with Monday
In earlier excel versions you can use this formula for the same result
=SUMPRODUCT((WEEKDAY(ROW(INDIRECT(A2&":"&B2)))={2,4,6})*(COUNTIF(H$2:H$10,ROW(INDIRECT(A2&":"&B2)))=0))
where {2,4,6} defines the weekdays to include (1=Sun through to 7=Sat)

The number of days between to dates is just date1 - date2.
One method is to divide the number of days in operation by the number of days in the week. In this case that would be 3/7. In this example, the number of days in operation would be 110.14 days. Below is your example. cell B4 has 1-sep-13, b6 has 16-may-14, both formatted as dates.
B
4 41518
6 41775
8 =B6-B4 Days
9 =B8/7 Weeks
10 =B8*3/7
01-Sep-13
16-May-14
257 Days
36.71 Weeks
110.14
To get more accurate, you would need to know which day of the week, weekday(), the first day in your range was and which day of the week your last day was.

Related

How to identify the week within a month with criteria in Excel

How can I calculate the week of the month? I understand that with the WEEKNUM function and ISOWEEKNUM function I can get the week of the year but this is not what I am looking for.
A week has only 4 weeks so the function must look at a date written in a cell and only identify a week 1-4
There is one set of criteria that I found especially difficult to add:
Week 1 for July 2019 begins on June 30th - Ends on July 27th
Week 1 for August begins on July 28th - Ends on August 24th
How can something like this added to an excel function?
This formula calculates a week in month (assuming your have a date at A1 cell)
=WEEKNUM(A1,2)–WEEKNUM(DATE(YEAR(A1),MONTH(A1),1),2)+1
Use WEEKNUM to get the week of year.
Use WEEKNUM to from the date of the first day of the month the date falls within.
Subtract the two week numbers (add 1 in case result is 0 - first week in month).
Sample:
8/21/2019
=WEEKNUM(A1,2) -> 34
WEEKNUM(DATE(YEAR(A1),MONTH(A1),1),2) -> 31
34-31 = 3 -> third week of August

How to find number of days in a date range for each month in dax

I need to calculate, using DAX (Data Analysis eXpressions), how many days within a given range fall into each calendar month.
I am given 2 dates, which define a date range; for example 2016-07-01 to 2016-08-03. I need to find how many days in that range fall in to each month i.e. how many fall into July, and how many into August.
In the example given, the expected result is 30 days in July and 2 days in August.
Assuming your dates are called "Date1" and "Date2" you could calculate the number of days in the second month as:
DaysInMonth2 = 1 + Date2 - MAX(Date1, DATE(YEAR(Date2),MONTH(Date2),1)
The Max is required in case the Date1 is on the same month
DaysInMonth1 = MAX(0,DATE(YEAR(Date2),MONTH(Date2),1)-Date1)
This second formula would calculate the number of days in the 1st month, and again in the case in which Date1 and Date2 are on the same month DaysInMonth1 would be 0.

Randomly generate 2 dates per week over 2 years

How do I write an excel formula, which randomly picks 2 dates in each week over a 2 year period, where:
The 2 dates per week are not the same as each other
So this should not happen:
05/02/2015
05/02/2015
The first date in any given week is always before the second date of that same week:
So this should not happen:
06/02/2015
05/02/2015
Where Sunday is the beginning of the week and Saturday is the end of the week:
Here is an example of what I am after:
week day date
1 1 01/01/2015
1 2 03/01/2015
2 1 05/01/2015
2 2 08/01/2015
And so on, all the way to the end of 2016.
This is what I have so far, but this only randomly gives me a day of a week, and doesn't fulfill any of the criteria I've described above:
=CHOOSE(WEEKDAY(ROUND((RAND()*(7-1)+1),0)),"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
There are 21 different combinations of 2 days in a week - to ensure an even distribution you can use a helper column, so assuming your weeks start at A2 and days at B2 (always showing 1 then 2 in days column for each week) then use this formula in D2
=IF(B2=1,RANDBETWEEN(1,21),"")
and this formula in C2
=IF(B2=1,E$1+MATCH(D2,{1,7,12,16,19,21})-8+A2*7,C1+D1-LOOKUP(D1,{1,7,12,16,19,21})+1)
where E1 contains the start date (a Sunday) of week 1
fill both formulas down the column
You can hide column D if required
See example here

Excel: Sum partial weeks for EOM report

I'm looking for a formula that will let me get a weekly total of a daily totals column during partial weeks at the end month for Excel 2010. Using the US week of Monday-Friday.
I have a Day(A), Date(B), Daily Total(J) and Weekly Total(N) column for one month. I'd like to show the totals for the week (M-F) either on Fridays, or the last day of the month. For full weeks, if the Day="Fri", I can sum column J by using a 5 cell range like $J11-$J15 which would sum the daily totals from Monday thru Friday of a full week in the middle of the month.
That doesn't work for a partial week at the beginning, so I have modified versions for the first 5 days, if a Friday is detected and that seems to work. I'm open to other ideas if there's a better way to handle the first week.
--------------------------------------------------------------------
A B J (N) Week
1 Day Date Total Total Formula for Column N Week Total
2 Thu 1/1/2015 1 =IF($A2="Fri",$J2,"")
3 Fri 1/2/2015 2 3 =IF($A3="Fri",SUM($J$2:$J3),"")
4 =IF($A4="Fri",SUM($J$2:$J4),"")
5 =IF($A5="Fri",SUM($J$2:$J5),"")
6 =IF($A6="Fri",SUM($J$2:$J6),"")
--------------------------------------------------------------------
A6/N6 would be the last possible Friday for the first 5 days of the first week, so the rest of the formulas, until towards the end of the month are similar to:
N7 would be =IF($A7="Fri",SUM($J3:$J7),"")
N8 would be =IF($A8="Fri",SUM($J4:$J8),"")
It handles summing a 5 day full week, in the middle of the month, and only shows the weekly total in column N for Fridays.
I don't know how to handle the last week since it could be a partial week of between 1-4 weekdays, not ending on a Friday. I still want show a total for that last partial week, on the last weekday of the month.
I thought about checking to see if the days in the last week were either Friday or the end of the month, and came up with a check for either "Fri" or the end of the month. For example in January day 31 would be:
(N32) =IF(OR($A32="Fri",EOMONTH($B$2,0)=$B32),SUM($J28:$J32),"")
That works if and only if, the 31st is a Fri, but doesn't work if Jan 31st is a Mon-Thurs since the range to sum is fixed at 5 days ($J28:$J32) and I don't know how to make it variable, based on how many days are in that last week of the month.
I would appreciate your ideas on how to do a monthly report where it can show weekly totals for full or partial weeks. The "plan" is to have 4 empty master workbooks to use for months with 28, 29, 30 and 31 days since I don't know how to make it all purpose and not screw up borders.
Thanks!
Does your data include Saturdays or Sundays? I'm assuming not. If not then you need to check whether the date is the last workday of the month....or a Friday.....and you could just sum everything in column J up to that point and subtract the column N totals form above, i.e. in N2 copied down
=IF(B2="","",IF(OR(A2="Fri",MONTH(WORKDAY(B2,1))<>MONTH(B2)),SUM(J$2:J2)-SUM(N$1:N1),""))
I assume N1 will contain a text header

Calculate the number of days of a date range occur between two dates

I have a datasheet that contains a list of start and end dates for a task. I need to calculate how many days between the start date and end date are part of our Christmas break (11th December to 7th January)
So for example,when start date is 10/12/2012 and end date is 12/01/2013, 28 of the days are between those dates. when the start date is 15/12/2012 and the end date is 12/03/2013, then 22 days of days are between those dates. If the start date is 10/12/2012 and the end date is 12/01/2014, 56 of the days are between those dates (because there's two years of the range).
I need to do this with a formula because of the requirements that I've been set.
Initially I decided to use the number of times Christmas Day (25th December) occurs and just calculate 4 weeks per occurrence.
The formula I used was
=FLOOR((E12-A25)/365,1)+IF(OR(MONTH(E12)=12,
MONTH(A25)=12),
IF(AND(DAY(A25)<=25,DAY(E12)>=25),1,0),
IF(OR(MONTH(A25)>=12,
IF(MONTH(E12)<MONTH(A25),
MONTH(E12)+12,
MONTH(E12))>=12),1,0))*28
But obviously this doesn't help if the range start and end date falls between those two dates.
Any suggestions? I'm at a dead end
Your date math on the second example is wrong -- there are 24 days in that range, not 22.
I can get you there for one holiday period:
LET:
A1 contain the holiday start (11-Dec-2012)
A2 contain the holiday end (7-Jan-2013)
B1 contain the start date
B2 contain the end date
FORMULA:
=MAX(MIN(A2+1,B2+1),A1) - MIN(MAX(A1,B1),A2+1)
The formula basically finds the overlapping date range, if there is one, and subtracts to get the number of whole days. The "+1" is because your "end dates" are actually inclusive, so for date math you need to have the holiday ending on 8 Jan, not 7 Jan, to capture that last day.
But this only works for a single year's holiday. You could store holiday ranges in other cells and use the same formula and add them all up, but you'll be limited to however many years you set up.
This formula will count dates between 11th December and 7th January inclusive within any date range, even across multiple years
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0>=1211)+(TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0<=107))
where your start date is in B1 and end date in B2
This converts every date in the range to a number , e.g. 1st Dec becomes 1201, 4th March becomes 304, then it counts those dates that are either greater or equal to 1207 (7th December) or smaller than or equal to 107 (7th January), so that will give 56 for your last example
You can shorten the formula if you subtract 7 from every date (based on 7th January as the end date) then you only need to check that resultant numbers are >= 1204, i.e.
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1-7&":"&B2-7)),"mmdd")+0>=1204)+0)
.....and a third option which should also give the same result, closer to richardtallent's approach - gets the number of years and multiplies by 28 and then adjusts the figures based on the start/end date
=(YEAR(B1-7)-YEAR(B2-7)+1)*28-MAX(0,B2-DATE(YEAR(B2-7),12,11))-MIN(28,DATE(YEAR(B1-7)+1,1,7)-B1)

Resources