+ Reply to Thread
Results 1 to 27 of 27

Count uniques with criteria

  1. #1
    Registered User
    Join Date
    11-01-2012
    Location
    Jacksonville
    MS-Off Ver
    Excel 2010
    Posts
    74

    Count uniques with criteria

    Hey I've been trying to get this for about 3 hours now and I'm stumped How can I get a count of Unique values (Column B) to each unique value in (Column A). Example attached
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    11-01-2012
    Location
    Jacksonville
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: Count uniques with criteria

    I've tried google with Frequency, Sumproduct and a combo of those with count, countif, countifs

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Count uniques with criteria

    I would think you are looking for...

    =COUNTIFS($A$2:$A$8,A2,$B$2:$B$8,B2)

    ...but this does not match your expected outcome in column C.

    Can you explain why you are getting your results? The last three rows all seem the same so why is it 1?
    HTH
    Regards, Jeff

  4. #4
    Registered User
    Join Date
    11-01-2012
    Location
    Jacksonville
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: Count uniques with criteria

    I'm trying to see how many different values of Column B match to each unique value in A so DEF only has 1 unique value in Column B whereas ABC has 2 different ones.

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Count uniques with criteria

    Maybe...

    =SUM(IF(FREQUENCY(IF($A$2:$A$8=$A2,MATCH($B$2:$B$8,$B$2:$B$8,0)),ROW($B$2:$B$8)-MIN(ROW($B$2:$B$8))+1),1))

    IMPORTANT
    • This is an array formula
    • Enter the formula >> press F2 then >> CTRL + SHIFT + ENTER
    • If entered correctly, the formula will be enclosed in {brackets}
    • Do not enter the {brackets} manually

  6. #6
    Registered User
    Join Date
    11-01-2012
    Location
    Jacksonville
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: Count uniques with criteria

    ugh! I'm so not used to the bracket thing and just learning the CTRL+SHIFT+ENTER. I will google the meaning of it so I better understand it but let me try the formula (which the part from ROW to the end I'll need to study too. Thank you so much for the help I'll let you know

  7. #7
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Count uniques with criteria

    You're very welcome. Good to hear and thanks for the feedback.

    I don't know if there is a non-array alternative, but maybe one of these other formula guru's can offer a suggestion.

    https://www.mrexcel.com/excel-tips/C...ormulas-excel/

  8. #8
    Registered User
    Join Date
    11-01-2012
    Location
    Jacksonville
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: Count uniques with criteria

    Is this one of those formula's that can make a sheet drag? The calculating is super slow since I put it in there yet it DOES work which I'm super stoked about just hate it when I don't fully get what's it's doing... but I WILL

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Count uniques with criteria

    Quote Originally Posted by MnMCarta View Post
    Is this one of those formula's that can make a sheet drag? The calculating is super slow since I put it in there yet it DOES work which I'm super stoked about just hate it when I don't fully get what's it's doing... but I WILL
    Yes, a CSE formula will drag a sheet down, especially depending on the number of cells to calculate.

  10. #10
    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 uniques with criteria

    If you are able to use a helper column this may take some of the "load" off the calculations. Column D is the "helper" column. It returns unique numbers that correspond with unique pairings of columns A and B. Then the final formula in C references that helper. Though they are both technically array formulas neither of these require CSE.


    A
    B
    C
    D
    E
    1
    Agent / Level
    Agent/Template
    Expected Outcome
    helpter
    2
    ABC
    AACAGT Template
    2
    1
    In D2: =INDEX((A2=$A$2:$A$8)*MATCH($B$2:$B$8,$B$2:$B$8,0),ROWS(C$2:C2))
    3
    ABC
    AACAGT Template
    2
    1
    In C2: =SUM(--(FREQUENCY($D$2:$D$8,D2)>0))
    4
    ABC
    AACAGT Trailer Request Template
    2
    3
    5
    ABC
    AACAGT Template
    2
    1
    6
    DEF
    DEFAGT Template
    1
    5
    7
    DEF
    DEFAGT Template
    1
    5
    8
    DEF
    DEFAGT Template
    1
    5


    Does this help?
    Last edited by FlameRetired; 11-14-2018 at 05:02 PM.
    Dave

  11. #11
    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 uniques with criteria

    Here are three more formulas. They are also array that do not require CSE. I am skeptical that they will perform any faster than what Jeff has proposed. All you can do is test them.

    The first 2 build ranges unique to ABC/DEF. I suspect the first one is the slowest of the 3.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Formula: copy to clipboard
    Please Login or Register  to view this content.
    and this one concatenates the ranges to force unique pairings.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by FlameRetired; 11-14-2018 at 05:16 PM.

  12. #12
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    A non-array alterantive with a helper column.

    In C2
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    In D2
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Then fill both down to the end of your data.

  13. #13
    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 uniques with criteria

    Another non CSE.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

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

    Re: Count uniques with criteria

    Yes, the construction using SUMPRODUCT with a reciprocal COUNTIF is fine for very small ranges, but testing shows it's incredibly slow relative to the FREQUENCY/MATCH construction over mid to large ranges.

    Regards
    Click * below if this answer helped

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

  15. #15
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    What about

    =SUMPRODUCT((A2=$A$2:$A$8)/SomeRange)

    Where SomeRange refers to =COUNTIF($B$2:$B$8,$B$2:$B$8)

    As countif is only using absolute ranges, calculation hierarchy should mean that it is only processed once per recalc as opposed to for each cell holding the formula.

    Not sure how it would perform on larger ranges compared to the 2 column countifs method that I suggested.
    Last edited by jason.b75; 11-25-2018 at 05:56 PM.

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

    Re: Count uniques with criteria

    Quote Originally Posted by jason.b75 View Post
    SomeRange refers to =COUNTIF($B$2:$B$8)
    Refers to what? That doesn't look like a valid syntax!

    Regards

  17. #17
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    It does now! Typo in previous post corrected.

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

    Re: Count uniques with criteria

    Ok, thanks. But isn't this in any case just a one-off formula to count the number of unique entries within a range? Why would it need to be recalculated more than once?

    Regards

  19. #19
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    No, it's to be copied down as in the OP's sample file, hence the relative range used in sumproduct.

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

    Re: Count uniques with criteria

    Ah, yes. I hadn't read the question properly. In that case you may well be right.

    Regards

  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 uniques with criteria

    @ jason
    RE posts 12 and 16.

    Quote Originally Posted by jason.b75 View Post
    A non-array alterantive with a helper column.
    In C2
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    In D2
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Then fill both down to the end of your data.
    Quote Originally Posted by jason.b75 View Post
    What about

    =SUMPRODUCT((A2=$A$2:$A$8)/SomeRange)

    Where SomeRange refers to =COUNTIF($B$2:$B$8,$B$2:$B$8)

    As countif is only using absolute ranges, calculation hierarchy should mean that it is only processed once per recalc as opposed to for each cell holding the formula.

    Not sure how it would perform on larger ranges compared to the 2 column countifs method that I suggested.
    Suppose one used this for the helper column D. Not sure how concatenating ranges would affect performance though.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by FlameRetired; 11-25-2018 at 06:46 PM. Reason: clarify focus of comment

  22. #22
    Forum Contributor
    Join Date
    11-21-2013
    Location
    zimbabwe
    MS-Off Ver
    Excel 2003
    Posts
    124

    Re: Count uniques with criteria

    Really it is not simple as I thought
    Array formula in D2
    Please Login or Register  to view this content.
    Last edited by soledad; 11-26-2018 at 06:54 AM.

  23. #23
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Count uniques with criteria

    Please try at C2 press Ctrl+Shift+Enter and copy down

    =SUM(--(FREQUENCY(IF(A2=$A$2:$A$99,MATCH($B$2:$B$99,$B$2:$B$99,)),ROW($A$1:$A$99))>0))

    I add data at row 9 to demonstrate result, and check if I understand it right.
    Attached Files Attached Files

  24. #24
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    Quote Originally Posted by FlameRetired View Post
    Suppose one used this for the helper column D. Not sure how concatenating ranges would affect performance though.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    I can't remember who pointed it out, but I have seen it said that array concatenation is slow compared to array calculation.

    Not given this method much thought, but this should perform better in the helper.

    =MATCH(2,($A$2:$A$8=$A2)/($B$2:$B$8=$B2))

  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 uniques with criteria

    Withdrawn by FR.

    Please see next post.
    Last edited by FlameRetired; 11-26-2018 at 01:14 PM.

  26. #26
    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 uniques with criteria

    Actually I now recall reading that as well ... I think it was Tony Valko.

    Edited Overlooked detail in formula.
    Another without array or concatenation.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  27. #27
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Count uniques with criteria

    I'm not sure that your last formula is doing what you think, evaluating it, I'm just seeing a longer version of
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Also, using exact match may negate any performance advantage in the method.

    Try it with an entry in column B that appears next to both ABC and DEF in column A, in that instance it will only count the unique record against one entry in column A, not both (which I believe is the requirement, the OP has implied this in their posts, but not in the data sample).

    I'm liking this for a helper,
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    But I still think that the double column countifs in post #12 will outperform any form of array.

+ 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 numeric uniques using multiple criteria
    By jaypaw in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-28-2017, 02:36 AM
  2. Count Uniques
    By tiger01 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-24-2014, 02:00 AM
  3. Count uniques within duplicates
    By ckramer in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-24-2013, 07:06 PM
  4. Count Uniques
    By John Bates in forum Excel General
    Replies: 13
    Last Post: 10-12-2010, 01:57 PM
  5. Count Uniques with 2 criterias
    By ROYW1000 in forum Excel General
    Replies: 2
    Last Post: 11-03-2008, 05:48 PM
  6. count uniques
    By prasjohn in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-20-2008, 01:34 PM
  7. Count uniques, 2 columns
    By krozar in forum Excel General
    Replies: 4
    Last Post: 11-16-2006, 04:48 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