+ Reply to Thread
Results 1 to 39 of 39

Count unique values with countif

  1. #1
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Count unique values with countif

    I have this:

    Please Login or Register  to view this content.
    to count the number of unique values in a range. what do I need to add to this to count the unique values in the same range but only for rows that the column of A equal x.

    which I have here: (but doesn't work)

    Please Login or Register  to view this content.

    any help would be great

    thanks
    BD

  2. #2
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Count unique values with countif

    Wouldn't it be

    =SUM(1/COUNTIFS('Hole Section Data - March'!P2:P424,'Hole Section Data - March'!P2:P424,'Hole Section Data - March'!A2:A424,A4))

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    See post # 11 by Tony Valko in this thread:http://www.excelforum.com/excel-form...mula-work.html
    This should do the trick:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    You'll to edit the cell refrences of course.
    Last edited by Tsjallie; 03-26-2015 at 05:41 PM. Reason: Oops
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    Quote Originally Posted by Jonmo1 View Post
    Wouldn't it be

    =SUM(1/COUNTIFS('Hole Section Data - March'!P2:P424,'Hole Section Data - March'!P2:P424,'Hole Section Data - March'!A2:A424,A4))
    I tried this before posting and I get a div/0.

    TSjallie, whats the thread?

  5. #5
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif


  6. #6
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    that seems to be a formula to handle special operators. when im using the <>, its to count everything that's not that value.

  7. #7
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    It's handling wild cards.

  8. #8
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    Right, im needing to populate a unique list based on a count if of another column

  9. #9
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    populate a unique list based on a count if of another column
    That sounds like more than the thread title is saying.
    But for the countif you'd only need to change <>"" into <>"x" or any other value in the range which you want to exclude from the count.
    So you'd get something like this
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    And change A1:A5 to the range you're looking at.
    Just upload your workbook if you want me to look at it.

  10. #10
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    attached is an excerpt of what I'm working with. I also included another issue im running into. please advise on both is possible

    thanks

    Countif Example.xlsx

  11. #11
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    I think this will do the "green one" (for Sheet2!E5).
    Assumed that with "based on column A" you're refering to Sheet2!A4 (contains "Alice").
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Looking into the "yellow one" yet.

  12. #12
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    And for the "yellow one" (for Sheet2!F2):
    Needed to add an extra condition to exclude empty cells as you going through the whole column (which I would not recommend).
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Need to count solely for "Alice" in Sheet2!F5? Then just add another condition for that.

  13. #13
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    The yellow one works as designed. When I tried to use the Green one with my live data (after adjusting the rows) it does not work, I get a 1.359552756.

  14. #14
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    Not sure what I was thinking (if I where at all) on the green one. Must have something to do with the color
    Should have sticked to the original suggestion.
    Next try:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Finish with [Ctrl]+[Enter]

  15. #15
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Here's another variation on your SUM(1/COUNTIFS....... formula.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    It does not have to be array-entered. It returns a 3 as well. Hope this helps.

  16. #16
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count unique values with countif

    @FlameRetired

    I suspect that works here only because all the names are "Alice" - that approach can only work when the condition can't include and exclude the identical values in the "count unique" column
    Audere est facere

  17. #17
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by daddylonglegs View Post
    @FlameRetired

    I suspect that works here only because all the names are "Alice" - that approach can only work when the condition can't include and exclude the identical values in the "count unique" column
    Actually there is a filter applied in column A of sheet Hole Section Data - March. There is one Casper with a unique corresponding Casing Size of 7 .....or have I missed the point?
    Last edited by FlameRetired; 03-30-2015 at 08:16 PM.

  18. #18
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count unique values with countif

    Quote Originally Posted by FlameRetired View Post
    Actually there is a filter applied in column A of sheet Hole Section Data - March. There is one Casper with a unique corresponding Casing Size of 7
    Apologies, I missed that - but my point still stands - your suggested formula works because Casper has a Casing Size which Alice doesn't have. If you change the Casper entry to have a Casing Size the same as one of Alice's entries you'll probably get a result which isn't a whole number

  19. #19
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by daddylonglegs View Post
    Apologies, I missed that - but my point still stands - your suggested formula works because Casper has a Casing Size which Alice doesn't have. If you change the Casper entry to have a Casing Size the same as one of Alice's entries you'll probably get a result which isn't a whole number
    Nope. I still get the same result.... which I believe is correct. What am I missing?

  20. #20
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count unique values with countif

    OK, sorry I should have been more specific - if I change the Casper entry in P12 to be the same as the row above, 5.5, then the formula now returns 2.9375, which obviously isn't a valid answer when counting different values

  21. #21
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by daddylonglegs View Post
    OK, sorry I should have been more specific - if I change the Casper entry in P12 to be the same as the row above, 5.5, then the formula now returns 2.9375, which obviously isn't a valid answer when counting different values
    I get that same result using one of Tsjallie's formulas (post 11). Mine is in post 15. Could we be talking about different formulas?

  22. #22
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    Forget post #11. I was drunk then.
    Post #14 should do the trick.

  23. #23
    Forum Contributor
    Join Date
    05-30-2014
    Posts
    112

    Re: Count unique values with countif

    Easy everyone haha. Thanks for all the input though.

    Quote Originally Posted by Tsjallie View Post
    Not sure what I was thinking (if I where at all) on the green one. Must have something to do with the color
    Should have sticked to the original suggestion.
    Next try:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Finish with [Ctrl]+[Enter]
    This worked, after adjusting the rows thanks!!



    Quote Originally Posted by FlameRetired View Post
    Here's another variation on your SUM(1/COUNTIFS....... formula.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    It does not have to be array-entered. It returns a 3 as well. Hope this helps.
    I got a long decimal after entering everything in. The example I gave was just a small snippet of what I'm actually working with in which there are 8 "Districts" and 22 different "casing sizes"

    thanks for the attempt though.

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

    Re: Count unique values with countif

    Quote Originally Posted by FlameRetired View Post
    I get that same result using one of Tsjallie's formulas (post 11). Mine is in post 15. Could we be talking about different formulas?
    daddylonglegs is correct. The solution in post #15 is flawed and only works due to a rather large coincidence.

    Regards
    Click * below if this answer helped

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

  25. #25
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by XOR LX View Post
    daddylonglegs is correct. The solution in post #15 is flawed and only works due to a rather large coincidence.

    Regards
    I found my problem and it's not a coincidence.

    When I copied and pasted my formula into a different cell I too got 2.9375. The original cell persisted in returning 3 or 4 as the case may be. It turns out that the original cell formatting was set to Fraction "Up to 1/4" !!!!?!!!!!

    Also when I Fx'd each formula they also returned 3 and 2.9375 respectively. I don't know what that's all about.

    Thank you daddylonglegs and XOR LX for persisting on this one. I would have never caught this one.
    Last edited by FlameRetired; 03-31-2015 at 12:13 PM.

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

    Re: Count unique values with countif

    Quote Originally Posted by FlameRetired View Post
    It turns out that the original cell formatting was set to Fraction "Up to 1/4"
    Ouch! That's a tough one to spot!

  27. #27
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by XOR LX View Post
    Ouch! That's a tough one to spot!
    Yup....pretty humiliating! But if I'm not challenged I don't grow. LOL

    Thanks, again!

  28. #28
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    OK. Here's my re-worked formula. I checked formatting and everything. It seems to work.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

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

    Re: Count unique values with countif

    Quote Originally Posted by FlameRetired View Post
    OK. Here's my re-worked formula. I checked formatting and everything. It seems to work.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Yes. That will do it!

    Not to be too negative, but I used to use similar constructions myself in such cases. Unfortunately, one of the things about them is that they tend to be quite resource-heavy.

    You wouldn't think it, with just a COUNTIFS inside a SUMPRODUCT, but trust me, the larger the ranges become, the more this set-up struggles.

    Whereas the FREQUENCY/MATCH construction, which if anything you would think would be the less efficient of the two (and especially since it requires the use of an exact match_type parameter), is actually surprisingly efficient over larger ranges.

    Still, nice alternative and plus one for correctly re-working a difficult construction.

    Cheers

  30. #30
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count unique values with countif

    Quote Originally Posted by XOR LX View Post
    You wouldn't think it, with just a COUNTIFS inside a SUMPRODUCT, but trust me, the larger the ranges become, the more this set-up struggles.
    Agreed,

    That's why Aladin always (now) suggests the FREQUENCY version - even when it's only a "countdiff" without a condition.

    I did a speed test with these two formulas

    =SUM(IF(FREQUENCY(IF(D$3:D$1000=D3,IF(B$3:B$1000<>"",MATCH(B$3:B$1000,B$3:B$1000,0))),ROW(B$3:B$1000)-ROW(B$3)+1),1))

    =SUMPRODUCT((D$3:D$1000=D3)*(B$3:B$1000<>"")/COUNTIFS(D$3:D$1000,D$3:D$1000,B$3:B$1000,B$3:B$1000&""))

    Running several hundred iterations of each the first one is about 7 times faster

  31. #31
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    I love this thread.
    Nice puzzle. Much more fun than Sudoku or Dutch television

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

    Re: Count unique values with countif

    Quote Originally Posted by daddylonglegs View Post
    Running several hundred iterations of each the first one is about 7 times faster
    Yes - that sounds about right from my experiences of the two set-ups, an even greater disparity than I'd imagined, if anything.

    Thanks for taking the time to perform the testing.

    Cheers

  33. #33
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    Maybe somewhat off topic, but there is something weird going on in the formula with the frequency.
    When it comes at point of evaluating the frequency function which would be likes this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    it counts the 1's 8 times while there are only 7 1's in the array. The 3's are counted correctly.
    This doesn't affect the outcome though.

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

    Re: Count unique values with countif

    @Tsjallie

    You don't appear to have posted the bins_array as part of your formula, so it's difficult to give you a certain answer.

    I take it you're aware that, in general, FREQUENCY returns an array comprising a number of entries which is one greater than that for the bins_array?

    Regards

  35. #35
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count unique values with countif

    Quote Originally Posted by Tsjallie View Post
    it counts the 1's 8 times while there are only 7 1's in the array.
    It's not counting eight 1s - it puts 8 in the 1 bin because there are 8 values <= 1 - 7x1 and 1x0

  36. #36
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by XOR LX View Post
    Yes. That will do it!

    ............... Unfortunately, one of the things about them is that they tend to be quite resource-heavy.

    You wouldn't think it, with just a COUNTIFS inside a SUMPRODUCT, but trust me, the larger the ranges become, the more this set-up struggles.

    Whereas the FREQUENCY/MATCH construction, which if anything you would think would be the less efficient of the two (and especially since it requires the use of an exact match_type parameter), is actually surprisingly efficient over larger ranges....................

    Cheers
    Thanks for the feedback and heads-up tips. And yes, I wouldn't think COUNTIFS inside SUMPRODUCT would be resource-heavy......I will keep that in mind.....ditto on the FREQUENCY/MATCH.

  37. #37
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Count unique values with countif

    Quote Originally Posted by Tsjallie View Post
    I love this thread.
    Nice puzzle. Much more fun than Sudoku or Dutch television
    Amen. Quite intriguing and challenging, and not nearly as easy as it first appears.

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

    Re: Count unique values with countif

    Quote Originally Posted by Tsjallie View Post
    I love this thread.
    Nice puzzle. Much more fun than Sudoku or Dutch television
    Agreed re Sudoku, though unfortunately can't comment on the Dutch television comparison!

  39. #39
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Count unique values with countif

    @Daddylonglegs, XOR LX
    Thanks for clarifying on the frequency. Or rather, waking me up on it :blush:

+ 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] Unique Total Value Count per Unique Lookup Values
    By KnightVision in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-02-2014, 05:03 AM
  2. countif- count Unique data
    By Liju144 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-19-2014, 10:03 AM
  3. [SOLVED] potential indirect countif: trying to count unique values giving same responses
    By j.farr3ll in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-14-2013, 08:00 AM
  4. [SOLVED] count unique values in 3 columns, but countif function not working?
    By frustrated with exce in forum Excel General
    Replies: 3
    Last Post: 06-13-2012, 11:13 AM
  5. Replies: 17
    Last Post: 08-24-2009, 08:58 AM

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