Excel function that returns difference between month? - excel

I have a worksheet that has a start date and end date. I need to get the number of months between these dates. I used the formula =MONTH(B1)-MONTH(A1) to get that #. However this formula fails when my dates span multiple years - if start date is 1/1/2014 and end date is 1/1/2015, then it returns a "1", when I'd like to get a "12".
I then tried DATEDIF(A1,B1,"m") to get the # of months between the dates, and that worked. My issue, however, can be summarized by the following example:
Situation 1:
Start Date: 1/5/2014
End Date: 3/1/2014
"MonthDif": 2
DateDif: 1
Situation 2:
Start Date: 1/5/2014
End Date: 3/10/2014
"MonthDif": 2
DateDif: 2
As you can see, the DATEDIF function returns the # of complete months, while my month - month function returns the number of months "occupied" by the difference between the start and end dates, regardless if they are complete months.
I need the number of months, regardless if full months, over any date period! Basically, I need exactly what the MONTH(B1)-MONTH(A1) returns, except it will work over multiple years.
Also, I was considering designing a custom VBA function to achieve the above. If anyone has any suggestions there.

If you use DATEDIF but always count from the 1st of the first month you'll get what you need, e.g. if you use =A1-DAY(A1)+1 that will give you the first of the A1 month, so use that in DATEDIF like this
=DATEDIF(A1-DAY(A1)+1,B1,"m")

Assuming B1 contains your end date and A1 contains your start date,
=IF(DAY(B1)>=DAY(A1),0,-1)+(YEAR(B1)-YEAR(A1))
*12+MONTH(B1)-MONTH(A1)

I need exactly what the MONTH(B1)-MONTH(A1) returns, except it will work over multiple years.
Ok, well, at the risk of stating the obvious:
=MONTH(B1)-MONTH(A1) + 12*(YEAR(B1)-YEAR(A1))
Rationale: a year is composed of twelve months.

Related

Formula that translates a date to Fiscal Year, Fiscal Period, and Fiscal Week

Working on a formula that will take a date and translate it to the format FYxxPxxWx.
For example. Input the date of 03/22/20 and the formula will give you FY20P06W4 which is correct.
However if you input 02/02/20 the formula will give you FY20P05W2. The correct output would be FY20P05W1. This issue also rears its head with the date 09/29/19. It gives you FY20P12W5. The correct output would be FY20P1W1.
Something else weird happens when you put in the date 04/5/20 you get FY21P07W2 when it should be FY20P07W2.
The formula is
=CONCATENATE("FY",RIGHT(YEAR(DATE(YEAR(D5),MONTH(D5)+(10-1),1)),2),"P",TEXT(CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3),"0#"),"W",WEEKNUM(D5,1)-WEEKNUM(DATE(YEAR(D5),MONTH(D5),1),1)+1)
I think this issue is caused by the strange weeks where the the month ends and another begins throwing off the formula.
I do have a formula that calculates the years fiscal year start date
=(DATE(YEAR(TODAY())-1,10,1)-(WEEKDAY(DATE(YEAR(TODAY())-1,10,1),1)))+1
This outputs 09/29/19 as the start date of the Fiscal year as its the same week as 10/1/19 which is the first month of the fiscal year. IF that makes sense.
The separate formulas are
For FY and grabs only last two digits of year
RIGHT(YEAR(DATE(YEAR(D5),MONTH(D5)+(10-1),1)),2)
For Period (gives me a two digit Period
TEXT(CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3),"0#")
For Week
WEEKNUM(D5,1)-WEEKNUM(DATE(YEAR(D5),MONTH(D5),1),1)+1)
I believe I have a solution for you. Discussion to follow, but here's the full formula:
=CONCAT("FY",RIGHT(YEAR(D5+91+WEEKDAY(DATE(YEAR(D5),10,1))),2),"P",TEXT(IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5),IF(MONTH(D5)=9,1,CHOOSE(MONTH(D5),5,6,7,8,9,10,11,12,1,2,3,4)),CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3)),"0#"),"W",ROUNDUP(((D5-IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5),DATE(YEAR(D5),MONTH(D5)+1,1)-WEEKDAY(DATE(YEAR(D5),MONTH(D5)+1,1))+1,DATE(YEAR(D5),MONTH(D5),1)-WEEKDAY(DATE(YEAR(D5),MONTH(D5),1))+1))/7)+0.01,0))
One issue is that this still calculates 2/2/2020 the way you said was incorrect. When I verify it against a calendar, though, it seems that FY20P05W02 should be correct. If the week that includes the first of the month begins a new pay period, that would mean 2/1/2020, falling on a Saturday, would be the last day of fiscal week 1. That would make 2/2/2020 the first day of fiscal week 2.
To calculate fiscal year, I used RIGHT(YEAR(D5+91+WEEKDAY(DATE(YEAR(D5),10,1))),2). Since you can count on there always being 91 days from the beginning of October to the end of December, it helps with this calculation. In your formula, you had MONTH(D5)+(10-1), which would push you 9 months out past the month in D5. This explains why your result for 4/5/2020 was off by a year.
Fiscal period was a bit trickier, requiring a couple nested IF statements. I used IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5) first to account for days at the end of the month that would fall into the next fiscal period, then IF(MONTH(D5)=9 to account for the few days at the end of September that might fall into the next fiscal year. Days at the end of September would default to 1, days at the end of a month that are included in the next fiscal period use the first CHOOSE function (they need the next month's number), and everything else gets the CHOOSE function as you wrote it.
The fiscal week took a bit more, but in the end I evaluated the beginning of the current fiscal month and subtracted it from the date in D5, then divided by 7 and added 0.01 so that even numbers would round up correctly.
I tested this out over a few years of dates and it seemed to be functioning correctly, but let me know if you have questions or issues.
One thing to consider when using WEEKNUM is that you'll have a week that is counted twice at the beginning of the year unless you use option 21 or ISOWEEKNUM. These give the same result as each other, and ensure that only one week number is assigned to any given day, no matter the year.

Excel Date difference without weekend days

Last time I posted a quite vague story about a date difference challenge which I haven't solved yet. I will try to elaborate since I have tried everything in my power and the problem still isn't fixed.
I currently have three columns.
Column 1 (F)
the date a car starts its repairs (format DayOfWeek-DD-MM-YYYY)
Column 2 (G)
the number of days in which the car is repaired (service level agreement [SLA]; the standard is 10 days)
Column 3 (H)
the output, which is the date the car should be finished. So the number of days after the startdate*
*Th thing which makes this case difficult is that only weekdays are included.
So, for example:
If a car starts repairs on Monday 1st of August, the finish date is Tuesday the 14th of August.
I tried to solve this with the following formula:
=IF(WEEKDAY(F218)=2;(F218+11);
IF(WEEKDAY(F218)=3;F218+12;
IF(WEEKDAY(F218)=4;F218+13;
IF(WEEKDAY(F218)=5;F218+14;
IF(WEEKDAY(F218)=6;F218+15)))))
In other words:
If startdate = Monday then startdate + 11,
if startdate = Tuesday then startdate + 12, etc.
This works, but I have 300+ rows and dragging this function down doesn't change the cell references.
I know about the NETWORKDAYS and WEEKDAY functions, but I encounter problems with any Monday where only 1 weekend passes and other days where 2 weekends pass.
First of all, I am assuming that your first day - whatever day that may be - is considered day one (1). So in my scenario, if a SLA states 2 days to complete a repair and the start date is a Monday, I'm assuming the repair should be completed by Tuesday.
My assumption is based off this comment by #RonRosenfeld:
...although you might have to subtract 1 from the number of days
With all that being said, try this formula in your cell instead:
 NOTE: You may need to change things like commas and semi-colons to adjust for your region.
=WORKDAY($F2,$G2-1)+LOOKUP(WEEKDAY(WORKDAY($F2,$G2-1),16),{1;2;3},{2;1;0})
What it does:
WORKDAY($F2,$G2-1)
First we want to find out exactly what day the repairs should be completed by if weekend days (Saturday and Sunday) were included. This part of the formula will simply give us a place to start.
$F2 is your repair start date
$G2 is the number of days a repair is supposed to take (you may need to add a column for this, because, as you stated, the SLA may change and you need the formula to be easily adjusted)
WEEKDAY(WORKDAY($F2,$G2-1),16)
The WORKDAY function from above is wrapped inside a WEEKDAY function. This WEEKDAY function is written to account for each day of a week to be assigned numbers. The [return_type] parameter of 16 tells Excel to label them as "Numbers 1 (Saturday) through 7 (Friday)". We chose 16 so that our LOOKUP function is easier to write. This part of the formula only returns a one-digit number, which in turn will be used to figure out what day of the week we actually want when excluding weekends.
LOOKUP(WEEKDAY(WORKDAY($F2,$G2-1),16),{1;2;3},{2;1;0})
We finish the formula by adding the result from a LOOKUP function using the first form of the function: LOOKUP(lookup_value,lookup_vector,[result_vector])
We found our lookup_value in the previous bullet point using the WEEKDAY function. Now we want Excel to use the lookup_vector - {1;2;3} in our formula - to find the correct value to add to the first part of our formula (which is found using the [result_vector] - {2;1;0} in our formula).
The lookup_vector only has three values: 1, 2, and 3.
1 signals Saturday
2 signals Sunday
3 signals all other days
Think of the lookup_vector and [result_vector] as forming a matrix/table from which our value is found:
1   2
2   1
3   0
If our number of repair days pushes us to:
a Saturday (1), the formula adds 2.
a Sunday (2), the formula adds 1.
any weekday, the formula adds 0 (since weekdays are acceptable).
Hopefully all of this makes sense. Best of luck to you!

Weekday and Workday Formula gives Value Error in terms of calculating actual due dates

I'm working on calculating our work completion dates, and I want to think I have a pretty good understanding of IF statements, but I can't seem to figure this one out at all.
So we have three products: policy, quotes and rating, and they each have their own service times/dates process.
- If a policy is requested before 4 pm, then it has to be completed the same day, otherwise if it comes in after 4:00 pm, then you have till 11:59 the next day.
- With quotes you have 48 hours to complete them, and with rating you have 7 days.
I also have a lookup sheet which references company holidays, and I decided to use workday function because some services get in on Fridays, and we are only count weekdays when calculating our due dates. I have also attached my formula in text below, do you guys think that I'm messing up in terms of how I am using both (OR) and (AND) functions to nest the IF statement?
=IF(OR(WEEKDAY(IF(A2="Quote",D2+2,IF(A2="Rating",Q2+7,"")))=1,WEEKDAY(IF(A2="Quote",D2+2,IF(A2="Rating",D2+7,""))=7),
IF(A2="Quote",D2+2,IF(A2="Rating",D2+7,""))+2,
IF(A2="Quote",D2+2,IF(A2="Rating",D2+7,""))),
IF(OR(A2="Policy",HOUR(C2<=16),INT(C2)+TIME(23,59,0)),
IF(OR(A2="Policy",HOUR(C2)>16),WORKDAY(D2,1,Lookup!$M$2:$M$20)+TIME(23,59,0),"ERROR"))
Photo of work and columns
If I've understood your requirements correctly, then you should be able to use =IF(A2="Policy",IF(HOUR(C2)<16,D2,WORKDAY(D2,1)), WORKDAY(D2,IF(A2="Quote",1,IF(A2="Rating",7,0))))
That just returns a date. If you want it to return a datetime, then use this:
=IF(A2="Policy",IF(HOUR(C2)<16,D2+1,WORKDAY(D2,1)+1)-1/1440, WORKDAY(D2,IF(A2="Quote",1,IF(A2="Rating",7,0)))+TIME(HOUR(C2),MINUTE([StartTime]),0))
...which returns this:
The Due By column has a custom number format applied of mmmm d, yyyy h:mm AM/PM

Using a dynamic single formula to calculate a date

This is a follow up to an earlier question.
#ForwardED I am trying to convert your original single static formula into a single dynamic formula.
Unfortunately my employer's filters will let me up certain things to a hyperlink, but will not let me download or view from the same site. I am also trying to come up with a formula for floating dates.
Below is a copy of the expanded explanation I gave on the original question. I am not sure if you missed it or not. It deals with holidays that have a set date like Christmas, 25th of December of every year. However if it fall on a Saturday the time of work is the Friday and if it is on a Sunday the day off is the Monday.
Dealing with a holiday falling on a Saturday or Sunday
Again we need to refer to some cell in your spreadsheet with the year so I will again use Q10 as the example and we will assume a date of 2014/10/24.
=IF(WEEKDAY(DATE(YEAR(Q10),12,25))=7,DATE(YEAR(Q10),12,24),IF(WEEKDAY(DATE(YEAR(Q10),12,25))=1,DATE(YEAR(Q10),12,26),DATE(YEAR(Q10),12,25)))
The formula checks first if the weekday is a Saturday. We do this using a function that will return the day of the week See step 2) from the original question. It is this part from the equation above:
WEEKDAY(DATE(YEAR(Q10),12,25))
It will return a single integer 1 through 7 corresponding to the day of the week the date function results in, in this case. If its a 1 we known its Sunday, if its 7 we know its Saturday. So the check for Saturday is:
WEEKDAY(DATE(YEAR(Q10),12,25))=7
If WEEKDAY()=7 is true then we provides the date of the day before which is really just subtracting 1 from the date we were looking at. We use this part of the formula to calculate that:
DATE(YEAR(Q10),12,24)
notice how I changed the day from 25 to 24. An alternate way would be to recycle our date and make the computer do one more calculation using this formula:
DATE(YEAR(Q10),12,25)-1
or
DATE(YEAR(Q10),12,25-1)
That all sits in the TRUE portion of the if statement. so if the date does not fall on Saturday then we wind up in the FLASE portion of the IF statement. Here we check with a second IF for the date falling on a Sunday. we use the same theory and process as we did for the Saturday check.
IF(WEEKDAY(DATE(YEAR(Q10),12,25))=1,DATE(YEAR(Q10),12,26),DATE(YEAR(Q10),12,25))
Placing an IF statement inside an IF statement is commonly referred to as "nesting". This whole IF statement happens in the FALSE portion of the previous IF that checked to see if it was Saturday. This time we checked for Sunday:
WEEKDAY(DATE(YEAR(Q10),12,25))=1
When this is true, then we need to increase the date by 1 day instead of decreasing it like was done for Saturday:
DATE(YEAR(Q10),12,26)
or
DATE(YEAR(Q10),12,25)+1
or
DATE(YEAR(Q10),12,25+1)
So that was the true portion of the Sunday check. Logically speaking the only way to get to the FALSE portion of this nested IF statement is to fail the Saturday check and then fail the Sunday check. Which means you do not need to go through and check if is the WEEKDAY comes out as 2, 3, 4, 5 or 6! Its one of those by the process of eliminating Sunday and Saturday (1 and 7). And if the date falls on Monday-Friday we dont need to change the date and can leave it just as is:
DATE(YEAR(Q10),12,25)
And I realized I did not explain how the date function works, though I think I tried to in one of the previous questions...regardless! DATE(arg1,arg2,arg3) requires three different arguments as integers or other functions that return integers.
arg1 is the year so 2014, 1995, 1965 are all acceptable integers. Also we could use YEAR(Q10), where the cell Q10 holds the date of 2014/10/24. In this case YEAR(Q10) would return 2014.
arg2 is the month and needs to be an integer in the range of 1 to 12. Again you can always use a formula that returns an integer in that range as well such as MONTH(Q10) which from our previous value of Q10 would return 10.
arg3 is day and similar to the above it needs to be an integer. A formula such as DAY(Q10) would return a value of 24.
What this means is if we know what day a holiday is on we can force it to a date by supplying a set month and day, and letting the year be determined by a formula that supplies the year you are interested in. So if you look at the last formula you can see we fixed the month at 12 and the day at 25. They year will be determine from the year of the date supplied in cell Q10.

Elapsed Days Hours Minutes Excluding Weekends and Holidays Time

This sounds simple but I have been pulling my hair out trying to figure out a solution. Any help before I go bald would be great.
I need a formula which is able to
calculate the duration in (days, hrs, mins) between two date\time values (eg 05/12/2012 5:30 PM and say 07/12/2012 5:45 PM);
excluding weekends and holidays.
I would like the result of the formula to read as follows "e.g 2 Days 0 Hrs and 15 Mins".
Thanks
Link to sample workbook
You can use NETWORKDAYS and NETWORKDAYS.INTL to achieve this
A bit of manipulation is required as these return net whole days:
Use these functions to calculate the number of non workdays, then subtract from the difference between start and end dates
=E3-D3-(NETWORKDAYS.INTL(D3,E3,"0000000")-NETWORKDAYS(D3,E3,$A$16:$A$24))
This returns the working day difference, where 1.0 = 1 day
NETWORKDAYS.INTL(D3,E3,"0000000") calculates whole days between the two dates (no weekends, no holidays)
NETWORKDAYS(D3,E3,"0000000",$A$16:$A$24) calculates whole working days days between the two dates (Sat/Sun weekends, holidays as per your list in $A$16:$A$24)
Difference in non-working days between the two dates.
E3-D3 is time between start and end date/times (1.0 = 1 day)
Use custom number formatting to display thye result in the format you require
d "Days" h "Hours" mm "Mins"
Note: this format won't work for negative values, you will need an alternative for when end date is before start date.
The following formula works like a treat with no additional formatting or manipulation.
To make it more stable I have turned all the holiday dates for UK 2012/13 into ‘Excel Serial Number’ format and placed them in an array bracket.
Replacing the "D5" in the formula with your cell reference for your course or metric "End Date" and "E5" with your course or Metric for "Completion Date".
=IF(E5<D5,"-"&TEXT(D5-E5,"h:mm"),NETWORKDAYS(D5,E5,({40910,41005,41008,41036,41064,41065,41148,41268,41269,41275,41362,41365,41400,41421,41512,41633,41634}))-1-(MOD(E5,1)<MOD(D5,1))&" days "&TEXT(E5-D5,"h:mm"))

Resources