How many months within 2 date ranges were in specific years/months?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
919
Office Version
  1. 365
  2. 2019
Hi everyone!

Quite a spreadsheet project I'm working on today, but I'm just a bit stumped on one little thing.

I'll explain a simple version of it that will probably be enough to get me going.

I have a date range in columns E & F, then in column G I've calculated the number of months within this date range with a DATEDIF. Now the part I'm stuck on is in column H, I need to calculate the number of 2020 Jan through Oct months that occur within this date range, and the same thing in column I for 2021, so the highest value possible for H or I is 10 for the 10 Jan through Oct months.

Here you can see a mockup where I've manually populated the highlighted H and I values to show the results I need to automate.

2021-10-26 Investment Comparison.xlsx
EFGHI
1Start DateEnd DateTotal Project Months2020 Jan through Oct Months2021 Jan through Oct Months
201/01/202111/30/2021101010
304/07/201703/31/20203530
412/01/201701/29/202137101
505/01/201706/30/20203760
602/01/201802/28/20202402
704/01/201801/31/20202110
809/27/202009/30/20211299
Project
Cell Formulas
RangeFormula
G2:G8G2=DATEDIF(E2,F2,"m")


Any suggestions? Thanks!

EDIT: Row 8 of my example should be 2 & 9, not 9 & 9. Sorry.
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Sorry, some wrong values in the yellow section above, here's an updated table with correct values (can't edit above post):

2021-10-26 Investment Comparison.xlsx
EFGHI
1Start DateEnd DateTotal Project Months2020 Jan-Oct Months2021 Jan-Oct Months
201/01/202111/30/202110010
304/07/201703/31/20203530
412/01/201701/29/202137101
505/01/201706/30/20203760
602/01/201802/28/20202402
704/01/201801/31/20202110
809/27/202009/30/20211229
Project
Cell Formulas
RangeFormula
G2:G8G2=DATEDIF(E2,F2,"m")
 
Upvote 0
Your expected results still don't add up correctly, is this what you want?
Book1
EFGHI
1Start DateEnd DateTotal Project Months2020 Jan through Oct Months2021 Jan through Oct Months
201/01/202130/11/20211009
307/04/201731/03/20203520
401/12/201729/01/20213790
501/05/201730/06/20203750
601/02/201828/02/20202410
701/04/201831/01/20202100
827/09/202030/09/20211218
Sheet19
Cell Formulas
RangeFormula
G2:G8G2=DATEDIF(E2,F2,"m")
H2:H8H2=DATEDIF(MEDIAN(E2,F2,DATE(2020,1,1)),MEDIAN(E2,F2,DATE(2020,10,31)),"m")
I2:I8I2=DATEDIF(MEDIAN(E2,F2,DATE(2021,1,1)),MEDIAN(E2,F2,DATE(2021,10,31)),"m")
 
Upvote 0
How about
Excel Formula:
=IFERROR(DATEDIF(MAX(E2,DATE(2020,1,1)),MIN(F2,DATE(2020,10,31)),"m")+1,0)
 
Upvote 0
Solution
try this :
Excel Formula:
=IFERROR(MIN(DATEDIF("01/01/2021",$F2,"m"),DATEDIF(E2,$F2,"m")),0)
 
Upvote 0
Thank you all!

And sorry again, even after my second attempted at a data mockup, I still had one value wrong.

I very quickly tried all 3 formulas. I only got one of them to work perfectly, and for the other two, it very well could be user error:

jasonb75, your formula was giving me 9 months for I2 when it should be 10.

offthelip, yours seemed perfect for 2021, but I couldn't edit it correctly to work for 2020.

Fluff, yours works perfectly for both years with an easy edit for 2021.

Thank you! This forum is amazing!
 
Upvote 0
A lot of ways to skin a cat.

Book1
ABCDE
1Start DateEnd DateTotal Project Months2020 Jan through Oct Months2021 Jan through Oct Months
21/1/202111/30/202110010
34/7/20173/31/20203530
412/1/20171/29/202137101
55/1/20176/30/20203760
62/1/20182/28/20202420
74/1/20181/31/20202110
89/27/20209/30/20211229
Sheet2
Cell Formulas
RangeFormula
C2:C8C2=DATEDIF(A2,B2,"m")
D2:D8D2=LET(r,SEQUENCE($B2-$A2+1,,$A2),b,r>=DATE(2020,1,1),e,r<=DATE(2020,10,31),n,r/(b*e),COUNT(UNIQUE(MONTH(FILTER(n,NOT(ISERROR(n)))))))
E2:E8E2=LET(r,SEQUENCE($B2-$A2+1,,$A2),b,r>=DATE(2021,1,1),e,r<=DATE(2021,10,31),n,r/(b*e),COUNT(UNIQUE(MONTH(FILTER(n,NOT(ISERROR(n)))))))
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top