+ Reply to Thread
Results 1 to 3 of 3

Calculate the # of months between 2 dates, but only count the same month/year once

  1. #1
    Registered User
    Join Date
    06-21-2012
    Location
    Brasil
    MS-Off Ver
    Excel 2003
    Posts
    1

    Calculate the # of months between 2 dates, but only count the same month/year once

    I'm in the final stages of building a spreadsheet that calculates the time a person has worked in his life in years, days and months (it's for retirement purposes, it will be released for public use for laywers and federal justice workers).

    The user inputs all the periods he has worked in his life in 2 columns, and then there's the third column that calculates the # of months between the begin and end dates.

    [Start date] [End date] [# of months]
    Job1_begin Job1_end [Job1_end - Job1_begin + 1]
    Job2_begin Job2_end [Job1_end - Job1_begin + 1]

    The formula to calculate the # of months between each one of these two dates is done (just for reference, if only 1 day inside a month is worked, it should count the entire month). Also, we always sum +1 because if the begin and start month/year are the same, the result would be 0, but per the previous rule we need to count 1.

    But there's one problem / challenge: the same month can't be counted twice !

    So if you have 3 periods like these ...

    March 1st 2010 - April 1st 2010
    April 2nd 2010 - April 5th 2010
    April 6th 2010 - April 29th 2010

    ... you should count APRIL 2010 only one time. So the third column, in this example, would be like this:


    March 1st 2010 - April 1st 2010 [2 months, march and april]
    April 2nd 2010 - April 5th 2010 [0 months, because april/2010 has already been counted]
    April 6th 2010 - April 29th 2010 [0 months, because april/2010 has already been counted]

    So as you see, the formula in the third column has to loop trough all the other lines and make sure that its not counting the same month/year twice.

    One thing the spreadsheet already does is to ensure that there are NO overlapping between the dates. But warning, the dates can be in any order, so we can have:

    April 2nd 2010 - April 5th 2010 [1 months, because april/2010 hasn't already been counted]
    April 6th 2010 - April 29th 2010 [0 months, because april/2010 has already been counted]
    March 1st 2010 - April 1st 2010 [1 month, because april/2010 has already been counted]

    I already created a formula that helped me get 99% in achieving my goal, but there's one scenario that it's failing. It uses a complicated joint of arrayformula and sumproduct and Month() and year(), but I'd rather not post it here because maybe a "fresh thinking start" can be helpfull. If nobody gets to a solution, I will post my broken solution and maybe we can evolve it, but it will be really hard even to explain what I'm doing now.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Calculate the # of months between 2 dates, but only count the same month/year once

    Hmm? ...
    it will be released for public use for laywers and federal justice workers
    Sounds like a professional job to me, these guys are smart cookies ...

  3. #3
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Calculate the # of months between 2 dates, but only count the same month/year once

    cant you just count the months between the start date and end date or are there gaps?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1