• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Calculate duration (day/month) of period, between a selected period

Challenge worthy?


  • Total voters
    1

Stensol

New Member
Dear Excel-goeroes,

For my work I need to know how many contractual funding is available in a certain period in time. Funding covers a certain period between 2 dates, and the selected period covers a period between 2 dates as well.

I've visualized an example of this in below screenshot. With this I've visualized the fundingperiod and selectedperiod overlap with a timeline. Timeline is just to make you understand the relation, not for final formula, as I only need results as marked in green cells. Original workbook attached to this post.
  • In the examples my core fundingperiod (blue cells) covers 3 months (91 days exactly) with a total fund of € 9.000,-.
  • But I only want to know the available funds in a selected period (red cells)
    • Examples A,B,C,E have different selected periods, with each different outcomes and available funds for that period.
      • all four examples have correct outcome
    • Example D and F is where my formula results in wrong outcome
      • ex.D should be 1 month
      • ex.F should be 6 months
PDC_07 Oct. 30 15.52.jpg

For this I've build quite a large and complex formula, of which I think there should be an easier way and shorter formula. The formula calculates the "amount of months within a period overlap", which is enough for me to calculate the funds per month. Besides that, the current formula doesn't show correct result, when funding period overlaps selected period.

Current formula in English (translated):
Code:
=IF(IF(AND($C2>=$C3;$D2<=$D3);(IFERROR(DATEDIF($C2;$D2;"m")+1;0));(IF(SUM(IFERROR(DATEDIF($C2;$D3;"m")+1;0))-(IFERROR(DATEDIF($C3;$D3;"m")+1;0))<=0;(IFERROR(DATEDIF($C2;$D3;"m")+1;0));(SUM(IFERROR(DATEDIF($C2;$D2;"m")+1;0))-(SUM(IFERROR(DATEDIF($C2;$D3;"m")+1;0))-(IFERROR(DATEDIF($C3;$D3;"m")+1;0)))))))<=0;0;IF(AND($C2>=$C3;$D2<=$D3);(IFERROR(DATEDIF($C2;$D2;"m")+1;0));(IF(SUM(IFERROR(DATEDIF($C2;$D3;"m")+1;0))-(IFERROR(DATEDIF($C3;$D3;"m")+1;0))<=0;(IFERROR(DATEDIF($C2;$D3;"m")+1;0));(SUM(IFERROR(DATEDIF($C2;$D2;"m")+1;0))-(SUM(IFERROR(DATEDIF($C2;$D3;"m")+1;0))-(IFERROR(DATEDIF($C3;$D3;"m")+1;0))))))))

Current formula in Dutch (original)
Code:
=ALS(ALS(EN($C2>=$C3;$D2<=$D3);(ALS.FOUT(DATUMVERSCHIL($C2;$D2;"m")+1;0));(ALS(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0))-(ALS.FOUT(DATUMVERSCHIL($C3;$D3;"m")+1;0))<=0;(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0));(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D2;"m")+1;0))-(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0))-(ALS.FOUT(DATUMVERSCHIL($C3;$D3;"m")+1;0)))))))<=0;0;ALS(EN($C2>=$C3;$D2<=$D3);(ALS.FOUT(DATUMVERSCHIL($C2;$D2;"m")+1;0));(ALS(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0))-(ALS.FOUT(DATUMVERSCHIL($C3;$D3;"m")+1;0))<=0;(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0));(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D2;"m")+1;0))-(SOM(ALS.FOUT(DATUMVERSCHIL($C2;$D3;"m")+1;0))-(ALS.FOUT(DATUMVERSCHIL($C3;$D3;"m")+1;0))))))))

I hope someone can give me a fresh start why this goes wrong, and how I could fix this. But maybe there's a way in which this can be done with an easier formula.

Sidenote: I don't prefer to use VBA as this isn't supported in our company...

Thanks for your interest and valuable braintime...

Best regards,

Stensol, The Netherlands
 

Attachments

  • Chandoo post.xlsx
    17.7 KB · Views: 8
Hi Stensol,

For the calulation of months, you can simply use something like this (place in H2, then you can copy and paste in the other cells):
Code:
=ROUND(G2/30,)

Here is also a heavy weight calulation of #days (place in G2, then you can copy and paste in the other cells):
Code:
=SUMPRODUCT((ROW(A:A)>=$C2)*(ROW(A:A)<=$D2)*(ROW(A:A)>=$C3)*(ROW(A:A)<=$D3))
 
Here is a little less process-heavy formula for calculation the #days:
Code:
=SUMPRODUCT((ROW(INDIRECT("A"&MIN($C2:$C3)):INDIRECT("A"&MAX($D2:$D3)))>=$C2)*(ROW(INDIRECT("A"&MIN($C2:$C3)):INDIRECT("A"&MAX($D2:$D3)))>=$C3)*(ROW(INDIRECT("A"&MIN($C2:$C3)):INDIRECT("A"&MAX($D2:$D3)))<=$D2)*(ROW(INDIRECT("A"&MIN($C2:$C3)):INDIRECT("A"&MAX($D2:$D3)))<=$D3))

I'm sure there should be an easier way, but I can't think of it now :)
 
Here another way to get # of months, w/o using DATEDIF (buggy) or INDIRECT (volatile)
=SUMPRODUCT(1*(ISNUMBER(MATCH(ROW(INDEX(A:A,TEXT(D2,"yyyymm")):INDEX(A:A,TEXT(C2,"yyyymm"))),ROW(INDEX(A:A,TEXT(D3,"yyyymm")):INDEX(A:A,TEXT(C3,"yyyymm"))),0))))
 
Hi,
Just for fun, here is one more approach for calculating number of overlapping months:
=TEXT((NPV(0,TEXT(MIN(D2,D3),{"y","m"})/{1,12})-NPV(0,TEXT(MAX(C2,C3),{"y","m"})/{1,12}))*12+1,"#;0")+0

Cheers,
Sajan.
 
Here another way to get # of months, w/o using DATEDIF (buggy) or INDIRECT (volatile)
=SUMPRODUCT(1*(ISNUMBER(MATCH(ROW(INDEX(A:A,TEXT(D2,"yyyymm")):INDEX(A:A,TEXT(C2,"yyyymm"))),ROW(INDEX(A:A,TEXT(D3,"yyyymm")):INDEX(A:A,TEXT(C3,"yyyymm"))),0))))
Hi Luke ,

The formula considers an array of numbers which does not reflect the way the calendar changes e.g. from 201312 the next number should be 201401.

The formula will work with a set of dates which coincide in the same year , but when more than one year is considered , it will not ; check it out with dates such as :

Code:
1/1/2013    12/31/2014
1/1/2012      1/1/2015
Narayan
 
Hi Luke ,

The formula considers an array of numbers which does not reflect the way the calendar changes e.g. from 201312 the next number should be 201401...

Doh! You are, of course, right. How silly of me to forget! :(
 
Back
Top