+ Reply to Thread
Results 1 to 16 of 16

COUNTIF function Using Dates

  1. #1
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    COUNTIF function Using Dates

    Hello all,

    First post so forgive me in advance if posting is incorrect. Straight to the point. Here is what I am trying to do:

    I have a column of contract dates (6/7/06, 8/14/09 etc.) and I would like to set up a cell to count the number of contracts written between specific dates. So, for instance, if I enter the date 10/12/09 in cell A1, I would want cell A2 to count the number of contracts written between 10/5/09 and 10/11/09.

    This was my thought: COUNTIF(b1:b300,AND(b1:b300>=(a1-7),b1:b300<=a1-1)

    This formula does not return an error, but does not give the correct output.

    Questions: Is COUNTIF the right function to use for this purpose? Can I use the AND function of I want to have multiple conditions? Also, is there always issues when adding and subtracting from dates (ie, a1-7) and is it best to avoid doing that if possible and just stick to referencing cells?

    Thanks in advance for any help.

    Brandon

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    If you're using XL2007 as your profile implies you can make use of the COUNTIFS function.

    =COUNTIFS(B1:B300,">="&A1-7,B1:B300,"<="&A1-1)

    if not (re: 2007) you're looking at SUMPRODUCT but the above is more efficient.

  3. #3
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    COUNTIFS not COUNTIF...So close, yet so far away! Thank you very much Donkey!

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    Yes, COUNTIFS was introduced in XL2007 along with the likes of SUMIFS, AVERAGEIF, AVERAGEIFS - none are thus backwards compatible with earlier versions of XL - in earlier versions you would be looking at either SUMPRODUCT or Arrays to conduct multiple criteria aggregations.

  5. #5
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    That sounds very handy. Unfortunately I realized as I returned from holiday that I am using Excel 2003 not 2007. I am working on getting an upgrage so I do not have to venture down the Array or Sumproduct road but will refer to your "reccomended reading" if need be.

    Thanks again.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    OK but for ref.

    =COUNTIFS(B1:B300,">="&A1-7,B1:B300,"<="&A1-1)

    could be replaced by

    =SUMPRODUCT(--(ROUNDUP((B1:B300-A1)/7,0)=-1))

    If you want to do more complex date calcs (ie other than 1 week prior) you can use a similar construct to the COUNTIFS

    =SUMPRODUCT(--(B1:B300>=A1-7),--(B1:B300<=A1-1))

    the latter is the more intuitive approach (in addition to being more flexible)

  7. #7
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    Would you mind translating those formulas into words for me? I would love to understand what they mean so I can use/tweak them as needed in the future.

  8. #8
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    I was using your tutorial on sumproduct when I saw your reply and this was as far as I had gotten:

    {=COUNT(IF(BM!D497:BM!D497>=A3-7,1,0)*IF(BM!D497>BM!D497<=A3-1,1,0))}

    Should that work as well?

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    In all honesty the SUMPRODUCT link in my sig. is about as good as you will/can get... I wouldn't/don't want to detract from it with my own ramblings on the subject!

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    =COUNT(IF(BM!D497:BM!D497>=A3-7,1,0)*IF(BM!D497>BM!D497<=A3-1,1,0))}
    not quite sure re: ranges (altered for sake of demo) but you would want either

    =COUNT(IF((BM!D497:D1000>=A3-7)*(BM!D497:D1000<=A3-1),1))
    CSE

    or just

    =SUM((BM!D497:D1000>=A3-7)*(BM!D497:D1000<=A3-1))
    CSE

    (and not my tutorial unfortunately, author is Bob Phillips (MS Excel MVP))

  11. #11
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    So in conclusion, with a single array being used, the COUNT function with two IFs should get the job done. And when I need to use two arrays SUMPRODUCT is the only way for me to go...

  12. #12
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    YES! Victory!

  13. #13
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    Quote Originally Posted by wbmcwhorter View Post
    So in conclusion, with a single array being used, the COUNT function with two IFs should get the job done. And when I need to use two arrays SUMPRODUCT is the only way for me to go...
    Not entirely sure I grasp the above but I would put it like this and again I stress this is aimed primarily at those running a version pre XL2007...

    If you have a single condition you should adopt standard COUNTIF/SUMIF functions as these are incredibly efficient.

    If you have multiple conditions, then first see if you create a singular condition based on helper cells utilising concatenation... if you can do so and revert to the standard COUNTIF & SUMIF functions as outlined already, this approach will be more efficient.

    If you can not create a single condition (ie concatenation etc not viable) then you're traditionally left with a choice between:

    SUMPRODUCT

    and

    CSE Array
    (CSE being CTRL + SHIFT + ENTER of course)

    It's generally accepted that there is little difference between the two in terms of efficiency... SUMPRODUCT is processed in the same way as an Array - on that basis neither are quick.

    The main advantages of SUMPRODUCT over CSE is the fact it doesn't require CSE (!) so from an end users perspective it is perhaps a little more robust.

    CSE Arrays have an advantage over SUMPRODUCT in so far as they are more flexible - eg handling underlying error values in source data etc etc...

    If you find yourself conducting multiple SUMPRODUCT formulae - say to create an Average then I would generally say you're better off reverting to a single array, eg instead of

    =SUMPRODUCT(--(A1:A100="x"),--(B1:B100="y"),C1:C100)/SUMPRODUCT(--(A1:A100),--(B1:B100="y"))

    I would opt for

    =AVERAGE(IF((A1:A100="x")*(B1:B100="y"),C1:C100))
    CSE

    I hope that clears things a little.

    (there are also the Database functions like DGET etc but they are seen less often though they are certainly no less efficient, many would argue the opposite but they can be trickier to setup)
    Last edited by DonkeyOte; 11-30-2009 at 11:45 AM.

  14. #14
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    Makes sense. After going through your post and reading the SUMPRODUCT link a few times, I am getting a much better grasp of how to handle single and multiple arrays.

    Can you tell me if you see anything wrong with this SUMPRODUCT formula (since I do not have COUNTIFS capability) ?

    {=SUMPRODUCT(MONTH(Inventory!F6:F287)=MONTH(A3))*(YEAR(Inventory!F6:F287)=YEAR(A3))*(Inventory!F6:F287)}

  15. #15
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    =SUMPRODUCT(--(MONTH(Inventory!F6:F287)=MONTH(A3)),--(YEAR(Inventory!F6:F287)=YEAR(A3)),(Inventory!J6:J287))

    I am guessing this looks format better to you? (ignoring cell ref of course)

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: COUNTIF function Using Dates

    There are two methods of coercion yes - multiplication and double unary.

    The latter is preferable for many as it is deemed the slightly more efficient coercer and also because it's slightly better at handling non-numerics in any summation range.

    However the multiplication method has an advantage over double unary in that it can handle arrays of different dimensions (double unary can not).

    Needless to say therefore each has it merits and circumstances often preclude either/or

    Your first formula:

    Please Login or Register  to view this content.
    Implied your committing as an array { } ... this is not required. This is really the fundamental advantage of SUMPRODUCT over an Array - ie does not require CSE.

    The above looks ok to me in terms of syntax though I'm curious as to why you're summing the date values ?
    Given usage of MONTH & YEAR the above would return an error should any value in F6:F287 be of a type that prevents coercion this is because both functions (MONTH & YEAR) will by their nature coerce the underlying values as though they are dates (numbers) thus if those values are not numeric (text strings etc (including Nulls)) then the above will generate #VALUE! error
    Similarly given explicit coercion of the final range regardless of MONTH/YEAR should any value in summation range be non-numeric it too would generate a #VALUE! error

    Your second formula

    Please Login or Register  to view this content.
    Makes more sense in terms of ranges etc (ie use of J rather than F as final array) but the above would of course still generate #VALUE! errors given use of MONTH & YEAR should any values in F be of a type that prevents coercion.

    If the #VALUE! error is an issue on occasion (eg formula nulls etc) you can use an alternative (slower) route of

    Please Login or Register  to view this content.
    If coercion is on the other hand not an issue another option would be:

    Please Login or Register  to view this content.
    which would be slightly quicker given only one test is conducted

    (though the same is true of the TEXT based test the string manipulation would be deemed relatively expensive and thus the MONTH & YEAR tests would be seen as quicker - per Bob Phillips himself - author of white paper)

+ 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