+ Reply to Thread
Results 1 to 14 of 14

Count Uniques

  1. #1
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Count Uniques

    Hi,

    I have two columns of data - one with IDs and another with States. In column F, I have the deduplicated list of unique IDs, but I need helping counting the number of unique states that are associated with each ID from columns B:C. Any help would be much appreciated!

    Thanks,
    John
    Attached Files Attached Files
    Last edited by John Bates; 10-12-2010 at 01:53 PM. Reason: Solved

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Count Uniques

    Try:

    Please Login or Register  to view this content.
    confirmed with CTRL+SHIFT+ENTER not just ENTER and copied down.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Re: Count Uniques

    perfect!! Thank you, this works beautifully!

  4. #4
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Re: Count Uniques

    Hi NBVC,

    Is there a way to perform the same procedure without the use of an array? The actual data is 147,000 rows and is "not responding".

    Thanks,
    John

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

    Re: Count Uniques

    To do it efficiently sort the data by ID & State, then in adjacent cell (D)

    Please Login or Register  to view this content.
    Your results are then a basic SUMIF which will perform quickly

    Please Login or Register  to view this content.

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

    Re: Count Uniques

    Another alternative - again working from sorted data

    Please Login or Register  to view this content.
    which might be quicker...

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Count Uniques

    I would agree with DO.

  8. #8
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Count Uniques

    One more way, assuming you are using XL 2007 as implied my attachment... and if you don't want to sort...

    in D3:

    =COUNTIFS(B$3:B3,B3,C$3:C3,C3)

    copied down

    Then in G3:

    =COUNTIFS(B:B,F3,D:D,1)

    copied down.

  9. #9
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Re: Count Uniques

    Thanks OD and NBVC!! These are great!! The only one that didn't seem to work was the following:

    D3:
    =IF(B3&C3<>"",IF(B3=B2,D2+C3<>C2,1),0)
    copied down
    (assumes D2 to be blank)

    G3:
    =LOOKUP(F3,B:D)*(LOOKUP(F3,B:B)=F3)
    copied down

    It seems to throw errors and not work. Any thoughts?

    Thanks,
    John

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

    Re: Count Uniques

    Quote Originally Posted by John Bates View Post
    ...the following:

    D3:
    =IF(B3&C3<>"",IF(B3=B2,D2+C3<>C2,1),0)
    copied down
    (assumes D2 to be blank)

    G3:
    =LOOKUP(F3,B:D)*(LOOKUP(F3,B:B)=F3)
    copied down

    It seems to throw errors and not work. Any thoughts?
    It requires the data (B:C) be sorted by ID & State and in present form it also requires D2 to be blank (else 0).

    If you have values other than IDs located throughout Column B (ie these ID references only represent a portion of the column content) then limit the LOOKUP ranges accordingly (eg B2:D170000 rather than B:D etc...)

  11. #11
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Re: Count Uniques

    Hi,

    I sorted B:C (first by B and then by C) and D2 is blank, but it still isn't working properly. See attachment (Formula4! worksheet).

    Thanks,
    John
    Attached Files Attached Files

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

    Re: Count Uniques

    whoops - typo on my part - I should really read things !

    Please Login or Register  to view this content.
    note the parentheses around C3<>C2

    Regards the summary LOOKUP calc used in conjunction with above...

    LOOKUP is very fast because it utilises binary search algorithm and as such processes only a tiny subset of the precedent range(s)
    It follows that where the precedent range is vast the savings can be significant as a result.
    Binary Searches of this nature require the data be sorted accordingly.

    We use 2 LOOKUPs as it is an approximate match so the 2nd is used to ensure the value returned is for the correct ID - if not result will be 0.
    Given the nature of LOOKUP function calculating 2 LOOKUPs per cell will still be blindingly fast.

  13. #13
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Re: Count Uniques

    Awesome, thanks!! Thank you so much for the explanation of LOOKUP as well. I never use this function but I'm sure I'll start using it more and more.
    Thanks!!

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

    Re: Count Uniques

    LOOKUP in this context is very similar to using VLOOKUP with range_lookup set to TRUE.

    Overall LOOKUP is an invaluable function as it offers the user a lot of flexibility.

+ 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