+ Reply to Thread
Results 1 to 8 of 8

COUNTIF beaviour with dynamic arrays

  1. #1
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    UK
    MS-Off Ver
    365
    Posts
    693

    COUNTIF beaviour with dynamic arrays

    Hi, can someone pls explain to me why the following first example works but the second one does not?

    There's apparently restrictions in what Countif (countifs etc.) are willing to accept as a range argument:

    Works:
    A1 = UNIQUE(B1:B10)
    C1 = COUNTIF(A1#,"*")

    Does not work (i.e. combination into one formula):
    D1 = COUNTIF(UNIQUE(B1:B10),"*")

    As a further question: Why does UNIQUE output an empty cell as a Zero?
    What would be a way to have UNIQUE show both empty cells and cells with a true zero as distinct unique outputs?
    What UNIQUE does instead is output two Zeroes as distinct unique items.

    Any help much appreciated.

    Thanks and Regards
    Last edited by RaulSerg; 05-21-2020 at 12:44 PM.

  2. #2
    Forum Expert
    Join Date
    02-10-2019
    Location
    Georgia, USA
    MS-Off Ver
    Office 365
    Posts
    3,217

    Re: COUNTIF beaviour with dynamic arrays

    Do you need to use countif? This works if it matches what you want to do:
    =COUNTA(UNIQUE(B1:B11))

  3. #3
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    UK
    MS-Off Ver
    365
    Posts
    693

    Re: COUNTIF beaviour with dynamic arrays

    Many thanks for the suggestion, Greg. This works, too.
    I do not have to use Countif specifically.

    After some more searching I did find an answer to my question.
    It appears that Countif (and the same seems to apply to the entire family of *IFS-functions) simply wasn't programmed to handle array calculations in its arguments, whether it be the range or the criteria argument.
    Additionally to your solution, this may be one of the few situations left where SUMPRODUCT might still be useful with dynamic arrays introduced now.

    Cheers
    Last edited by AliGW; 05-20-2020 at 12:22 AM.

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: COUNTIF beaviour with dynamic arrays

    Quote Originally Posted by RaulSerg View Post
    It appears that Countif (and the same seems to apply to the entire family of *IFS-functions) simply wasn't programmed to handle array calculations in its arguments, whether it be the range or the criteria argument.
    Not quite. The criteria argument can comprise array calculations of any type. The range argument can comprise a combination of one or more functions, provided, however, that this combination of functions resolves to an actual range reference.

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  5. #5
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    UK
    MS-Off Ver
    365
    Posts
    693

    Re: COUNTIF beaviour with dynamic arrays

    Quote Originally Posted by XOR LX View Post
    Not quite. The criteria argument can comprise array calculations of any type. The range argument can comprise a combination of one or more functions, provided, however, that this combination of functions resolves to an actual range reference.

    Regards
    Got it.

    Cheers

  6. #6
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    UK
    MS-Off Ver
    365
    Posts
    693

    Re: COUNTIF beaviour with dynamic arrays

    I have to admit that I always struggle to get my head around some of the particularities of the INDEX function, e.g. the somewhat subtle difference that it returns either a value or a reference to a value; I try to avoid OFFSET for e.g. dynamic ranges because it's volatile and formulas are either impossible or very hard to trace/audit.

    But what XOR LX explained for me basically means that a function like :
    =COUNTIF(B$2:INDEX(B$2:B$50,D$2,),">1") works just fine
    while the same function with the range argument already resolved, say this being: {1;2;1;3;4}, that is:
    =COUNTIF({1;2;1;3;4},">1") is something that does NOT work. (Again, SUMPRODUCT wouldn't have the same limitation in this particular instance.)

    I find this pretty neat.
    Thanks again for pointing this out for me. (:

  7. #7
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: COUNTIF beaviour with dynamic arrays

    Quote Originally Posted by RaulSerg View Post
    =COUNTIF(B$2:INDEX(B$2:B$50,D$2,),">1") works just fine
    while the same function with the range argument already resolved, say this being: {1;2;1;3;4}, that is:
    =COUNTIF({1;2;1;3;4},">1") is something that does NOT work. (Again, SUMPRODUCT wouldn't have the same limitation in this particular instance.)
    Good explanation.

    Regards

  8. #8
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: COUNTIF beaviour with dynamic arrays

    As a further question: Why does UNIQUE output an empty cell as a Zero?
    What UNIQUE does instead is output two Zeroes as distinct unique items.
    In reality, =[blank] --> 0 so, in effect, UNIQUE appears to mimic that behaviour

    What would be a way to have UNIQUE show both empty cells and cells with a true zero as distinct unique outputs?
    =UNIQUE(IF(ISBLANK(B1:B10),"",B1:B10))
    (above should ensure underlying values maintain data types aside the blank(s) which will be cast to a null string)

    as a separate observation, I suspect FILTER will often be used with UNIQUE - e.g.

    =COUNTA(UNIQUE(FILTER(B1:B10,ISTEXT(B1:B10))))
    to count unique strings (per * in post 1) -- would include any null strings in underlying data (but not true blanks)

+ 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. Dynamic Arrays, Dynamic Drop down list, Filters and multiple rows
    By Big_Kev007 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-09-2020, 06:35 AM
  2. Dynamic Arrays
    By andrewc in forum Excel General
    Replies: 4
    Last Post: 02-29-2020, 03:41 AM
  3. [SOLVED] Countif using vba arrays
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-10-2016, 09:20 PM
  4. [SOLVED] CountIF VBA Arrays
    By lloydgodin in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-26-2013, 08:09 PM
  5. [SOLVED] COUNTIF and arrays?
    By aikorei in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-16-2012, 11:27 AM
  6. Using Dynamic Arrays
    By ozi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2007, 09:35 AM
  7. Dynamic Arrays
    By Chiba in forum Excel Formulas & Functions
    Replies: 24
    Last Post: 09-06-2005, 12:05 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