+ Reply to Thread
Results 1 to 6 of 6

CountIF date/time not counting correctly

  1. #1
    Registered User
    Join Date
    12-02-2019
    Location
    louisville, ky
    MS-Off Ver
    16
    Posts
    3

    CountIF date/time not counting correctly

    I'm using Index/Match to fill other rows with the dates in order they occur. It intermittently fails and I believe the nested Countif formula isn't working consistently with the date. Column A is formatted "m/d/yyyy h:mm:ss"

    The dates start in A1, and B1 formula is =COUNTIF($A$1:$A$6,"<="&A1). I believe this should return the order in the range in which the time occurs.
    11/27/2019 15:04:05
    11/27/2019 14:49:06
    11/27/2019 14:34:06
    11/27/2019 14:19:06
    11/27/2019 15:14:05

    This populates Column B row 3 & 4 with 1, and subsequent formulas dont' work.
    If I change row A3 by 1 second the formula works.

    Is there a problem with the date/format? I've tried using a serial date and that doesn't work either. I appreciate any advice.
    Attached Files Attached Files
    Last edited by lvl133; 12-02-2019 at 06:50 PM. Reason: attachment

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: CountIF date/time not counting correctly

    Welcome to the forum

    Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert Ace_XL's Avatar
    Join Date
    06-04-2012
    Location
    UAE
    MS-Off Ver
    2016
    Posts
    6,074

    Re: CountIF date/time not counting correctly

    Use SUMPRODUCT instead

    =SUMPRODUCT(--($A$1:$A$5<=A1))

    Copy down
    Life's a spreadsheet, Excel!
    Say thanks, Click *

  4. #4
    Registered User
    Join Date
    12-02-2019
    Location
    louisville, ky
    MS-Off Ver
    16
    Posts
    3

    Re: CountIF date/time not counting correctly

    Thanks Ace_XL, this would work except cell A6 sometimes has a date, sometimes it is blank. I've checked that the error still occurs whether cell A6 is populated or not.

  5. #5
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: CountIF date/time not counting correctly

    An attachment was not necessary. The phenomenon is easy to reproduce. Adding to the mystery:

    1. If we enter the =A1<=$A$3 into C1 and copy down, C3 and C4 are TRUE, confirming that there should be 2 matches.
    2. =COUNTIF($A$1:$A$6,"<=11/27/2019 2:34:06 PM") returns 2.
    3. =COUNTIF($A$1:$A$6,"<=11/27/2019 14:34:06") returns 2.
    4. Simplification of the problem: =COUNTIF(A3,"<="&A3) returns zero(!).

    My guess is: the problem is a binary floating-point anomaly, compounded by the games that Excel plays.

    Note that =A3-TEXT(A3,"0.00000000000000E+0")<=0 returns FALSE, but A3<=TEXT(A3,"0.00000000000000E+0") returns TRUE.

    In the first formula, Excel compares the exact binary difference.

    In the second formula, Excel compares each operand rounded to 15 significant digits.

    The exact binary value of 11/27/2019 14:34:06 can be represented by 43796.6070138888 + 8.73E-11, where for A3, Excel displays 43796.6070138888(!) when rounded to 15 significant digits (formatted as Number with 12 decimal places).

    (That should round to 43796.6070138889. Off-hand, I cannot explain why it doesn't with impunity. I suspect that it is related to the same formatting defect that causes 42304.973 to be displayed as 42304.9729999999 in the Formula Bar.) (1)

    Two work-arounds:

    =COUNTIF($A$1:$A$6,"<="&TEXT(A3,"m/d/yyyy h:m:s"))
    or
    =SUMPRODUCT(($A$1:$A$6<>"")*($A$1:$A$6<=A3))


    -----
    (1) I confirmed that the conditions that apply to the defective formatting of 42304.973 also apply to the binary value of 11/27/2019 14:34:06, which can represented by 43796.6070138888 + 8.73E-11, to wit: (a) the integer part is less than 65536 (2^16); (b) the fractional part can be represented in 32 bits or less (i.e. any bits to the right are zero); and the 16th significant digit is 5 or more. In that case, Excel fails to round up the 15th significant digit, as we would expect.
    Last edited by joeu2004; 12-02-2019 at 07:57 PM.

  6. #6
    Registered User
    Join Date
    12-02-2019
    Location
    louisville, ky
    MS-Off Ver
    16
    Posts
    3

    Re: CountIF date/time not counting correctly

    joeu2004, both worked to put in order like I wanted

    =INDEX($A$1:$A$6,COUNTIF($A$1:$A$6,"<="&TEXT($A$1:$A$6,"m/d/yyyy h:m:s")))

    =INDEX($A$1:$A$6,(SUMPRODUCT(($A$1:$A$6<>"")*($A$1:$A$6<=A1))))

    and auto fill down.

    I appreciate the binary floating-point anomaly suggestion as possible culprit, something like that would explain why 95% of the time the formula worked but sometimes failed.

    super thanks!!!

+ 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. [SOLVED] Countif statement not counting correctly for percentages
    By dcoates in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-10-2016, 02:04 PM
  2. [SOLVED] COUNTIF not counting correctly
    By helencj in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-24-2015, 07:42 AM
  3. Countif not counting date correctly
    By ciresuark in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-03-2015, 07:27 PM
  4. [SOLVED] SUMPRODUCT, COUNTIF formulas not correctly counting based on two sets of criteria
    By justinbelkin in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-11-2013, 10:12 AM
  5. Replies: 2
    Last Post: 08-12-2013, 02:38 PM
  6. Replies: 7
    Last Post: 12-16-2012, 08:24 PM
  7. Countif not counting correctly...
    By AaronK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-18-2012, 12:43 PM

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