+ Reply to Thread
Results 1 to 8 of 8

Using COUNT(IF(... to count number of months in years

  1. #1
    Registered User
    Join Date
    01-13-2014
    Location
    Melbourne
    MS-Off Ver
    365
    Posts
    13

    Using COUNT(IF(... to count number of months in years

    Hi guys,

    I have a large list of dates and need to count the occurrence of months in given years (so that July 2013 and July 2014 don't cause double ups for example).

    The formula I entered
    "=COUNT(IF(AND(YEAR(C2:C176)=2013,MONTH(C2:C176)=7),C2:C176,0))"
    returns a value of 1 (incorrect). If I take out the AND function and use EITHER MONTH or YEAR the function returns the correct individual values as is needed.

    Any thoughts?

    Regards,
    Abid
    Last edited by Abid123; 01-17-2014 at 01:06 AM.

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Using COUNT(IF(... to count number of months in years

    welcome to the forum, Abid. if you want to use that, it has to be an array formula:
    =COUNT(IF(YEAR(C2:C176)=2013,IF(MONTH(C2:C176)=7,1)))
    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer. Press F2 on that cell and try again.

    not advisable as this can cause your workbook to calculate slower. the alternatives are:
    =SUMPRODUCT((YEAR(C2:C176)=2013)*(MONTH(C2:C176)=7))

    or even better since you have excel 2007
    =COUNTIFS(C:C,">=1jul2013",C:C,"<=31jul2013")

    notice the i can use the whole column for the last formula. the other 2 would cause the workbook to be slower if you do

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Using COUNT(IF(... to count number of months in years

    One way...

    =SUMPRODUCT(--(TEXT(C2:C176,"mmm yyyy")="Jul 2013"))
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Registered User
    Join Date
    01-13-2014
    Location
    Melbourne
    MS-Off Ver
    365
    Posts
    13

    Re: Using COUNT(IF(... to count number of months in years

    @Benishiryo,

    Thanks so much mate, worked perfectly.

    Question is, even though I entered mine as an array it still did not work :S
    Second follow up question, can you explain your second formula?

    Thanks again mate, :Thumbsup:

  5. #5
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Using COUNT(IF(... to count number of months in years

    when more than 1 member helps, i think it's good to try/acknowledge the member's effort too.

    Question is, even though I entered mine as an array it still did not work
    AND doesn't work really well with arrays here. in your formula YEAR(C2:C176)=2013, you can select that & press F9 to calculate. you notice you'll get many TRUEs & FALSEs. but if you select AND(YEAR(C2:C176)=2013,MONTH(C2:C176)=7) & press F9, it returns you just 1 single result. most likely, that will be FALSE. that is because ALL C2:C176 needs to be Year 2013 & Month 7 to return TRUE. so if it's TRUE, you want C2:C176. if it's FALSE, you want a 0. so it returns a 0. COUNT will count numbers in the specified range. 0 is a number. hence it returns 1.

    Second follow up question, can you explain your second formula?
    again, when you do logical tests like what you did:
    YEAR(C2:C176)=2013
    MONTH(C2:C176)=7
    they return a bunch of TRUEs & FALSEs. Excel recognizes TRUE as 1 & FALSE as 0. so
    TRUE x TRUE = 1
    TRUE x FALSE = 0
    FALSE x FALSE = 0

    so only when the 2 conditions are met (2013 & 7), then you will get a 1. SUMPRODUCT merely helps you multiply all the TRUEs & FALSEs, and then sum them up. a small eg.
    Data Range
    C
    2
    Jul-13
    3
    Jul-14
    4
    Jul-14
    5
    Jul-13
    6
    Jul-13

    the above would be:
    {TRUE;FALSE;FALSE;TRUE;TRUE}*{TRUE;TRUE;TRUE;TRUE;TRUE}
    so that's:
    {1;0;0;1;1}
    add them up & you get 3.

    do mark this thread as Solved

  6. #6
    Registered User
    Join Date
    01-13-2014
    Location
    Melbourne
    MS-Off Ver
    365
    Posts
    13

    Re: Using COUNT(IF(... to count number of months in years

    Quote Originally Posted by benishiryo View Post
    when more than 1 member helps, i think it's good to try/acknowledge the member's effort too.


    AND doesn't work really well with arrays here. in your formula YEAR(C2:C176)=2013, you can select that & press F9 to calculate. you notice you'll get many TRUEs & FALSEs. but if you select AND(YEAR(C2:C176)=2013,MONTH(C2:C176)=7) & press F9, it returns you just 1 single result. most likely, that will be FALSE. that is because ALL C2:C176 needs to be Year 2013 & Month 7 to return TRUE. so if it's TRUE, you want C2:C176. if it's FALSE, you want a 0. so it returns a 0. COUNT will count numbers in the specified range. 0 is a number. hence it returns 1.


    again, when you do logical tests like what you did:
    YEAR(C2:C176)=2013
    MONTH(C2:C176)=7
    they return a bunch of TRUEs & FALSEs. Excel recognizes TRUE as 1 & FALSE as 0. so
    TRUE x TRUE = 1
    TRUE x FALSE = 0
    FALSE x FALSE = 0

    so only when the 2 conditions are met (2013 & 7), then you will get a 1. SUMPRODUCT merely helps you multiply all the TRUEs & FALSEs, and then sum them up. a small eg.
    Data Range
    C
    2
    Jul-13
    3
    Jul-14
    4
    Jul-14
    5
    Jul-13
    6
    Jul-13

    the above would be:
    {TRUE;FALSE;FALSE;TRUE;TRUE}*{TRUE;TRUE;TRUE;TRUE;TRUE}
    so that's:
    {1;0;0;1;1}
    add them up & you get 3.

    do mark this thread as Solved
    Thanks again mate! Sorry got caught up with meetings.

    EDIT: Repped and solved! Booyah!
    Last edited by Abid123; 01-17-2014 at 01:08 AM.

  7. #7
    Registered User
    Join Date
    01-13-2014
    Location
    Melbourne
    MS-Off Ver
    365
    Posts
    13

    Re: Using COUNT(IF(... to count number of months in years

    Quote Originally Posted by Tony Valko View Post
    One way...

    =SUMPRODUCT(--(TEXT(C2:C176,"mmm yyyy")="Jul 2013"))
    Hey mate, just noticed this post, sorry my heads not been on right all day.

    This worked perfectly also! It's amazing how many different ways there are to do something and yet I knew not one haha!

    Regards,
    Abid

  8. #8
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Using COUNT(IF(... to count number of months in years

    You're welcome. Thanks for the feedback!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Count how many months/years have passed?
    By famico78 in forum Excel General
    Replies: 5
    Last Post: 08-24-2017, 10:04 PM
  2. [SOLVED] Count number of specific dates within several years
    By thed85 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-08-2013, 12:55 PM
  3. Replies: 3
    Last Post: 08-22-2011, 11:58 AM
  4. Count Number Of Elapsed Months
    By simonyglog in forum Excel General
    Replies: 4
    Last Post: 04-08-2011, 10:34 AM
  5. count number of years 2003 in a range of dates
    By Stan Altshuller in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-03-2005, 03:06 PM

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