+ Reply to Thread
Results 1 to 9 of 9

Return 1 or 0 based on Date Range

  1. #1
    Registered User
    Join Date
    03-02-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Lightbulb Return 1 or 0 based on Date Range

    I need a function that will give a 1 if a range covers a month and a 0 if it does not. The entire month does not need to be covered by the range, just a single day, to return a 1.

    Here is what I have (short version) the full list goes through 2014:

    start 5/23/07
    end 9/20/07

    The result should be this:
    2007
    January 0
    February 0
    March 0
    April 0
    May 1
    June 1
    July 1
    August 1
    September 1
    October 0
    November 0
    December 0
    Attached Files Attached Files
    Last edited by kwjoh; 03-07-2011 at 01:39 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Return 1 or 0 based on Date Range

    Here it is with A done for you, the remaining columns are left as an exercise to the student

    I changed your month names to be actual dates. That helps. The fact that your data has two columns for each letter but your results only has one makes it a pain to write formulas because you can't just copy to the right. The layout of your data also makes it difficult to do this with a simpler formula, like an array formula or SUMPRODUCT.

    If you do copy to the right, then you can find & replace to change the column references.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Return 1 or 0 based on Date Range

    Hi kwjoh and welcome to the forum,

    Look at the attached with a smaller formula than 6SJ but I needed a helper column. See if this works for you in the attached. I did Column B. I wonder if someone can come up with a shorter formula for Column C?
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Return 1 or 0 based on Date Range

    Quote Originally Posted by MarvinP View Post
    Look at the attached with a smaller formula than 6SJ but I needed a helper column. See if this works for you in the attached. I did Column B. I wonder if someone can come up with a shorter formula for Column C?
    Clever solution. You really don't need the helper column, though; the references to J can refer to the dates one row higher in B (although the reference to J1 might be an issue).

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Return 1 or 0 based on Date Range

    Oooh, sorry Marvin, I have to take it back. In column B, your solution marks only April and November for 2007, but all the months in between have to be marked too. You have to do some date arithmetic for this problem because of the need to analyze the span for each pair of start and end dates.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Return 1 or 0 based on Date Range

    I guess that's not how I read the problem. I think s/he wants my answer and just didn't know how to ask it.

    I'll keep scratching my brain to see if the On until Off then On again solution comes to mind.
    What happens if there are 3 dates in the same month? On, Off and On all in the same month. I still like my answer.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Return 1 or 0 based on Date Range

    Quote Originally Posted by MarvinP View Post
    I guess that's not how I read the problem. I think s/he wants my answer and just didn't know how to ask it.

    I'll keep scratching my brain to see if the On until Off then On again solution comes to mind.
    What happens if there are 3 dates in the same month? On, Off and On all in the same month. I still like my answer.
    In kwjoh's post and attached example, it shows

    start 5/23/07
    end 9/20/07

    In the example results, May, September, and all the months in between are marked as 1. That's a pretty stark example. My interpretation is "Mark any month which contains at least one day that falls within any of the start/end intervals."

    Hopefully kwjoh will come back to visit and confirm.

  8. #8
    Registered User
    Join Date
    03-02-2011
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Return 1 or 0 based on Date Range

    6StringJazzer,

    Thanks for you help! After I finished modifying the sheet you gave me I noticed I needed more granularity, weeks vice months. I attempted to modify what you did to work with weeks and it looks like everything worked with one exception. When the last day of the week conicides with the first day of use I get a 0 instead of a 1. I tried to figure out why the error was occurring but my attempts to modify the formula create either an error or the same result. In the attached file on the "weeks covered" sheet, row 390 for asset A is an example of there being no tally, 0 vice 1. I am guessing this is something similar to counting the number of days between 2 dates including the start and end date. The only way I could get an accurate number was to add 1 every time. Any help you can give me is greatly appreciated!

    Thanks again!
    Attached Files Attached Files

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,691

    Re: Return 1 or 0 based on Date Range

    In my monthly version, I used only the date for the first day of the month for all calculations. In the weekly version, you added a column for Week End Date and tried to modify your formula to take that into account. I didn't try to figure out what was wrong with your formula. It was easier to just copy over the formula used for months. My column is the one highlighted in yellow.

    If it works for you, just delete your own column for A, then copy over my column. But.

    One problem with this kind of copying is that the source data has data in every other column, but your formulas are in adjacent columns. So you can't just copy the formulas into the next column. One trick for doing this kind of copying is to insert a blank column between every column where you are going to put the formulas, then copy the column for A into the desired columns. They will refer to the correct source columns. Now you can delete the extra blank columns and your formulas will still point to the right place.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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