WARNING: this tutorial contains some advanced formula concepts which may incur the following side effects:
Negative side effects:
Headache, urge to bang head on desk, fidgeting in seat, urge to look for distractions like e-mail to give your brain a rest.
Positive side effects:
Jumping up and cheering out loud when you realise you cracked it (which may result in people pointing and staring at you), desk-slapping joy, being known as the ‘office geek’ or ‘Excel Guru’.
Proceed at your own risk, and when you do succeed wear those labels with pride.
Ok, let’s say you have a contract which spans several months or years, with evenly spread cash flows.
Here are the details:
Contract Price: $430,500
Start Date: August 15, 2014
End Date: May 29, 2015
Note: the contract price could be income or expense since the logic is the same.
Enter your email address below to download the sample workbook.
We need to spread the cost/income over the months like I have done in the table below:
Ahhh, this takes me back to my accounting days where we used to amortise the cost of assets and prepaid expenses. Sorry, I digress.
Ok, there are 3 steps to creating the table above and we’ll start out slowly:
Step 1: Calculate amount per day
The calculation for the amount per day is pretty straight forward. In English it reads:
= total contract price / (contract end date – contract start date)
=$430,500 / (29/5/2015 - 15/8/2014)
=$430,500 / 287 days
=$1,500 per day
Note: my dates are dd/mm/yyyy format.
Next we need to work out how much to allocate each month, and that’s not so straight forward! It wouldn’t be so tricky if we could just allocate an equal amount for each month, but our boss does things by the book and wants it based on the actual number of days in each month.
And here’s the stinger; notice how the contract Start and End dates are mid month. That right there adds another layer of complexity, but don’t worry I’ve got a clever way to handle that.
Step 2: Determine if the month falls within Contract dates
In cell B9 (see image below) we use a logical test formula which says:
=Are any of the dates in the month in cell A9 between the dates in the contract period?
The formula looks like this:
And evaluates to TRUE or FALSE as you can see in cells B9:B20 below:
Whoa there! Right now you might be thinking what has MEDIAN got to do with this?
Well, MEDIAN returns the number in the middle of a set of given numbers. The syntax is:
=MEDIAN(number1, [number2], [number3]…)
And when we give MEDIAN the following dates:
- Contract start date – 15/08/2014
- Contract end date – 29/05/2015
- A date in the month you’re checking – let’s say September 01, 2014 in cell A11.
If the month you’re checking is BETWEEN the contract start and end dates, then that date is the median. You can imagine that your MEDIAN part of the formula in cell B10 for September will look like this:
And therefore the result is:
So when we then compare the result returned by MEDIAN to the date in cell A11 (for September,) it looks like this:
In English it reads:
=Does the date in cell A11=The Date Returned by MEDIAN
And the result is:
In English TRUE reads: absolutely positootly, yes!
Don’t stand up and cheer yet. We’re not quite done. You may have noticed that I haven’t addressed the elephant in the MEDIAN formula; EOMONTH, so let’s look at that next.
Remember the ‘stinger’ I mentioned earlier where the contract dates are mid month?
Not only that, the ‘months’ in column A are actually just one day in the month, in this case the 1st of every month; if you look in the formula bar you can see the date in cell A9 for July 2014 is 1/07/2014:
Now this is only a problem for the first and last months in the contract period. Let’s look at August 2014 as an example.
And then our logical test would be:
Does the month date in cell A10= MEDIAN(15/08/2014,29/05/2015,01/08/2014)
And the answer is:
In English; FALSE reads: no way, José 😉
What we actually want it to say is TRUE, because some of August is in the contract period.
In order to get a TRUE returned from our logical test we need to change the contract start date to the first day in the month. And to avoid this same problem in May 2015 (i.e. when the contract ends), we also need to change the contract end date to the last day in the month.
For reference here is the formula again:
The EOMONTH function finds the last day of the month before or after a specified number of months. The syntax is:
The months argument can be:
- A positive value for months ahead
- A negative value for months prior
- A zero for the end of the current month
In our case we want to calculate the first day of the month the contract starts so our formula is:
Which evaluates to:
The -1 for the months argument finds the last day of the month prior:
Then we add 1 to take it to the first day of the following month:
Likewise for the last day of the contract:
Which evaluates to:
Remember a zero takes you to the end of the month, so it then becomes:
So now we know that the MEDIAN formulas in column B are actually testing to see if the date in column A falls between 01/08/2014 and 31/05/2015. We can see that August 2014 returns a TRUE, as does May 2015.
Aside: you might be thinking you could use the MONTH function to compare the month in column A to the Contract month dates, and you’d be right, but this will cause you problems when contracts span multiple years, which is why I’ve chosen this method.
Step 3: Calculate the number of days in the contract period for each month
In theory this calculation is quite straight forward:
=month end – month start
For example September will be:
=30/09/2014 – 01/09/2014
= 30 days
Except for the first and last months of the contract where we must use the contract dates as opposed to the start and end dates of the month. Stung again!
Therefore we need to get a bit clever and employ the aid of MIN and MAX to help Excel know when to use the contract dates vs the start/end dates of the month in column A. The formula we’ll use for August is:
Let’s look at the MIN part for our month end calculation first:
In English it reads, find the minimum of the contract end date and the end of the month in cell A10 i.e.
Which evaluates to:
And our month start calculation is:
To finally evaluate to:
And the two together are:
Which evaluates to:
If we look at column C below we can see the results of the MIN/MAX calculation for each month:
Uh oh….. July 2014 and June 2015 are negative days! Don’t panic, the final step which brings the 3 components together will eliminate the negative days quicker than Darth Vader can turn on his Light Sabre (or Light Saver as my kids say).
Bringing it All Together
Now we can bring our 3 components together (we’ll use August as our example):
1. Cost per day
2. Logical Test to see if month falls within contract period
3. Number of days in contract period for the month
And altogether now:
Which evaluates to:
When you apply a math operation to a Boolean TRUE or FALSE value they convert to their numeric equivalents, which are 1 and 0 respectively. So in actual fact our formula looks like this:
You probably know where I’m going next. Yep, those negative days. Let’s look at the formula for July 2014:
Remember FALSE converts to a zero so our formula looks like this:
And you know from school math that anything multiplied by a zero = zero! So we don’t have to worry about the negative day calculation because it gets eliminated by the logical test in step 2.
Now, you might be thinking that you could have written this formula with several nested IF’s and AND’s, and you’re right, but don’t you think this is more cleverer? 😉
I learnt that MEDIAN trick from my friend, Roberto Mensa.
Personally I think MEDIAN is the star in this formula. The way Roberto dresses MEDIAN in a BETWEEN Superhero cape is both elegant and unexpected.
Now it's your turn to take the baton. Think about ways you can use this idea in other Excel challenges you might have. Hint: It's not limited to dates.
Thanks for the formula, it works perfectly.
Is there a way of doing this so that full months are equally instead of the number of days in the month?
Hi Greg, Yes, you can modify it to apportion the amounts equally over each month irrespective of the number of days. In step 1 you’ll need to calculate a monthly amount rather than a daily amount. Then in step 3 you’ll want to simply find if the month falls inside the contract period, rather than the number of days in the contract period. If you get stuck please post your question on our Excel forum where you can also upload a sample file and we can help you further. Mynda
Thanks for your reply. I tried to implement what you have advised above, but I am unable to get it to work.
I have put a post in the excel forum as you have advised.
Hi, love your work here. I’m wondering if there’s a way to carry out the exact same process, but for numerous contracts in an array?
Or a different process that achieves the same results?
Hi Dommy, Please post your Excel question and sample file on our forum where we can help you further. Mynda
How do I change and calculate the above formula if the working days for a contract is only mondays for between start and end dates and 3 hrs a day with hourly rate say 30 for example.
Thanks for help!
Hi Hiral, please post your question on our forum where you can upload a sample Excel file with your data and desired result. Thanks, Mynda
Mind blowing…I wasted 2 days in trying different and long formulas. You made my day!!!
Thank you very much
Pleased to hear it was helpful!
Superb workings, it is helpful a lot.
Is there a formula instead of EOMonth to say EOyear?
I want to do something similar calculation for year bucket so where you use months, can you use the year e.g. 2013, 2014, 2015,2016 etc and share it with us? Thanks a lot.
You can replace the EOMONTH formulas with the following:
First day of year: DATE(YEAR($B$4),1,1)
Last day of year: DATE(YEAR($B$5),12,31)
This is very informative! If I wanted to show calculations for only the fiscal years in the contract (not details by month), how would I incorporate the formulas for first and last days mentioned above?
Assuming Contact Price is in cell A3 and amount is in B3:
Contact Price 430,500
Start Date. 08/15/14
End Date 05/29/15
Cost Per Day 1,500
How would the formula be written to get the amount of the contract price in FY 2014 (cell B8) and 2015 (cell B9)?
Please post your question and sample Excel file on our forum where we can help you further: https://www.myonlinetraininghub.com/excel-forum
I need to calculate our employees’ projected length of service for 5, 10, 15, 20 years etc (respectively) based on their respective start dates., and I will require a formula for this.
Is this something that you can assist with please?
Try this one:
Replace StartDate with a reference to your start date cell.
This was a cool tutor , i was using this but when the duration is one day meaning if it starts and finishes in the same day, it won’t show on the table. I need help .
You could either adjust the start date by 1 day or put in an IF statement at the front of the formula to test if the start and end day are equal, and then enter the full amount on that day.
hi there, thank you for this. What if your start and end date is the same, how do you account for that? Right now, utilizing this formula, it is giving me an error.
If the start and end dates are the same then you don’t need this formula since the whole cost applies to just one date.
Nothing short of genius. Thank you!!!
Glad it was useful 🙂
I love this formula. Can it be modified to calculate only working days in a month?
Thanks, Peter. Yes, you can use the WORKDAY function to return working days.
I used the above formula to make a projected earnings chart from a CPM schedule. I came up with 2 problems.
1) The formula does not work when the start and end dates are the same. I fixed this by manually changing the hours on either the start or end date so they were no longer identical.
2) If either a start or end date falls on the last day of a month, the formula does not work right. Is there a solution for this? To visualize this with the above example, change the end date to “31/05/2015 1:00:00 AM”.
If the start date is the same as end date, there is no reason to spread the amount over months, it makes no sense, as the first month will have the entire amount, there is really no calculation needed here.
If either a start or end date falls on the last day of a month, the formula does work right, the time amount should not be there. Try with: “31/05/2015”, it will work.
Ben is correct in the fact that this does not work. I have an expense that should have a duration of 1 day, but the issue is that this formula recognizes the amount both in September and October which doubles my expense.
Is there a solution to this? I’ve tried to add IF statements, but can’t seem to get it to work.
My guess is that you are not using the same structure described in this article and in the downloadable file.
For other data structures, please upload a sample file to our forum so we can see and understand your situation.
Thanks for understanding.
How would you approach a dataset like that of utility bill records where the utility is metered between dates that straddle months? If you want to convert a table of records with straddled months into a table with readings for prorated months what would be the approach?
You could probably use Power Query to do this. If you post your question and sample Excel file on our forum we can help you further.
Amazing formula – big thank you!
One comment, though: it will NOT work if the data is in a table. Took me 20 minutes to determine this was the cause. No biggie though, I’ll just stop formatting the data in a table.
Is it possible to do this formula using fiscal quarters instead of months?
It can be done, but fiscal years are not the same in each country.
Can you please upload a sample file with your layout on our forum? (create a new topic). A manual example of the desired result will speed up the process, please take your time to write a good description of the problem on the forum.
Hi there. Would this also work for creating cost profiles with various costs over various time scales?
Possibly, Chris. Can’t say for certain without seeing your examples. If you get stuck please post your question in our Excel Forum.
What if you want to do it by week instead of month? I want to spread the revenue over the weeks to see where we have holes by week instead of month.
Take a look at this version from our OneDrive folder.
The original formula is looking at the date from column A, and calculates the number of days left until the end of month (EOMONTH formula). The dates in column A are always the first day of the month. In the example file, I added new lines, with 8/09/2014, 15/09/2014 and 22/09/2014 dates, but the formula should calculate now the number of days between 2 consecutive dates instead of initial EOMONTH formula.
You will see the new formula in the file.
There are some problems: you will not be able to split the number of days in a month into 4 equal subsets, the last week will have more days than the rest (the formula will calculate only full days, not fractions of days).
Thanks for this info! Super useful. I had, however, a small glitch while working with my file.
Th formula that you have posted for the ‘Weeks’ ends up including the entire months that the start and end periods are for, even if the dates were somewhere in the middle of the month.
How do we fix this?
Can you upload a sample file with data on our forum? It will be easier to help based on your data structure.
Uploaded ! 🙂
Clever use of MEDIAN. This opens up opportunities to use this function, as I now understand what it does. Great!
kind regards, René
Cheers, René 🙂 Glad you liked it.
You’re welcome, Mohammed 🙂
Hi again Mynda
I finally got around to looking at this post – albeit whilst eating lunch when I do a lot of Excel reading & research.
Also being a bean counter from way back, such that month-end dates are now ingrained in my soul (:-(, I too went in the same direction as Richard. This also allows the use of the DAY function to easily return the number of days in a month.
I also spotted the issue that Dave identified – contracts of this type are usually active for both start and end dates, unlike financing/banking transactions (e.g. for interest calculation purposes) wherein the start date is counted but not the end (repayment) date.
My formula to calculate the days in each month (example for July, but same for all months) then became:
No IFs, or buts!!
Cheers from the West
Thanks for sharing, Col.
I think you meant to reference A9, not F9 in the last EOMONTH formula.
Ooops, sorry! I missed that reference when editing my formula to match your table – mine was built in your spreadsheet but to the right of your table.
No worries, Col. I figured that was the case 🙂
Modifying Richard’s formula:
Love it, Don.
Perfect for all those set in their ways of entering month dates as the 1st 🙂
It appears there may be a small issue with the number of days.
The formulas presented calculate the number of days between August 15, 2014 and May 29, 2015. They do not count both August 15 and May 29. Always consider whether the beginning and ending dates are inside or outside of the reporting period and add or subtract one day as necessary.
I forgot to mention considering the ‘actual’ start and end dates of the contract. Thanks for clarifying that point.
I thought I was the only one who thought like this…..Absolutely wonderful, I followed right along knowing what your were doing.
Oh! it has been a long since I’ve heard it but I loved your “absolutely positootly, yes!” comment it also reminded me of the Three Stooges Curley’s “Soitenly!” saying!
Finally, try Excel “Wizzard”…. for a title! I’ve picked up that one over the years from co-workers. At one time I even had the Mickey Mouse Wizzard hat on display so new folks could figure out who they were talking about.
You know your life is to happy…..really now…….get serious. If you do I’ll quit reading your columns.
Great to hear you enjoyed the read. I ‘soitenly’ enjoyed writing it 😉
Thanks for the post.
The way I have tackled these types of calculations in the past has been by formatting the dates so the days can be seen (dd-mmm-yyyy), which means you can dispense with the eomonth parts of the formula and work on an actual basis which would allow for intra month calculations. You also avoid the US date format issue.
Then A8 would be your starting date or the end of the preceeding month and if full month calculations are required and A9 could be as follows:
=eomonth($A8,1) which can then be dragged down as far as needed
or A9 is any date for which the amount is required.
The formula can then be as simple as:
Awesome formula, thanks for sharing.
Now if only we can get everyone to enter their dates as the last day of the month instead of the first we can simplify many formulas. A great lesson, although a habit hard to break 🙂
One thing you mention that I’d like to clarify for others:
“formatting the dates so the days can be seen (dd-mmm-yyyy), which means you can dispense with the eomonth parts of the formula and work on an actual basis which would allow for intra month calculations”
Formatting the dates so the days can be seen does not simply allow you to dispense with the EOMONTH formula, the key to getting rid of EOMONTH is entering the date as the end of the month, instead of the first. EOMONTH is then redundant.
You can then format the date any way you want, including ‘mmm-yy’ and it doesn’t change the result of your formula.
This is a pretty good post. Works well. Thanks for sharing.
Cheers, Jef 🙂
As I see the sign of this part:
also tells if the month is within the contract period or not, so I maybe the formula is simpler this way:
=$B$3/($B$5-$B$4) * (MIN($B$5,EOMONTH($A9,0))>MAX($B$4,$A9-1)) * (MIN($B$5,EOMONTH($A9,0))-MAX($B$4,$A9-1))
Thanks for sharing, Kris 🙂
Can I shorten?
Mynda I learnt that MEDIAN trick from our friend Daniel Ferry 🙂
or with EOMONTH …
🙂 of course you can! Thank you for sharing, Roberto.
Thanks to Daniel too.