+ Reply to Thread
Results 1 to 24 of 24

Count unique values by attribute

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Count unique values by attribute

    Hello, everyone.

    I am using Excel 2010. I am working with data which I need to count unique values of one column from the unique values of another column. I have numerically-identified "events" in column A and numerically-identified "patches" in column B.

    e.g.,

    event patch
    001 001
    001 002
    001 003
    002 004
    ... ...

    I want to know how many *unique* patches occur for each *unique* event. Moreover, I want these counts to be printed in a column. In the above example, this would be

    event unique patch count
    001 3
    002 1
    ... ...

    I know that there are formulas to count the total unique values of a single column, but I need to preserve the order of the counts by a second column (events). Thank you in advance for any help!

  2. #2
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Count unique values by attribute

    Try..
    =SUMPRODUCT((A:A<>"")/COUNT.IF(A:A;A:A&""))
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  3. #3
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Count unique values by attribute

    Try..
    =SUMPRODUCT((A:A<>"")/COUNT.IF(A:A;A:A&""))

  4. #4
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    This formula gives me an error and Excel highlights =SUMPRODUCT((A:A<>"")/COUNT.IF(A:A;A:A&""))

    Here are some sample data attached.
    Attached Files Attached Files
    Last edited by ppickell; 05-19-2012 at 07:06 PM.

  5. #5
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Count unique values by attribute

    hi.
    try now..
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by marreco View Post
    hi.
    try now..
    This is close and I will certainly be able to use this as well, but I need the unique count of patches by unique events.

    e.g.,

    event unique patch count
    20000001 1
    20000002 1
    20000003 1
    20000004 1
    20000005 1
    20000006 1
    20000007 1
    20000008 1
    20000009 1
    20000010 1
    20000011 3
    20000012 1
    20000013 1
    20000014 2
    ... ...

    I need unique counts of patches for every event.

  7. #7
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Count unique values by attribute

    hi.

    try...

    ...
    =CountIf(A:A;$A$2:A3)


    In Excel, the CountIf function counts the number of cells in a range, that meets a given criteria.

    The syntax for the CountIf function is:

    CountIf( range, criteria )

    range is the range of cells that you want to count based on the criteria.

    criteria is used to determine which cells to count.

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

    Re: Count unique values by attribute

    Maybe this will work for you...
    Attached Files Attached Files
    HTH
    Regards, Jeff

  9. #9
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    Maybe this will work for you...
    This is nearly what I am looking for. But this has essentially summed the number of records for each event. I need the *unique* number of patches for each event.

  10. #10
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by marreco View Post
    hi.

    try...

    ...
    =CountIf(A:A;$A$2:A3)
    This is similar to the formula jeffreybrown provided, but again, I need the counts of unique values in column B (patches) for unique values in column A (events).

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

    Re: Count unique values by attribute

    You have most likely surprised my formula ability, but why not use a pivot table?

    Question, what is your answer for...?

    Event
    20000084

    Patch
    20000135

  12. #12
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    You have most likely surprised my formula ability, but why not use a pivot table?

    Question, what is your answer for...?

    Event
    20000084

    Patch
    20000135
    The data I am working with are spatial data. Each "patch" represents a polygon shape. Each "event" is a disturbance in a forest which is composed of patches. When I get the data from the program, it lists every patch as a record (i.e., row) and assigns each patch to an event. But I need the event records, not the patch records. So I need to summarize the number of unique patches for every event.

    I have tried to use pivot tables, but I am only able to get as far as you have, which is to count the total number of patches by event rather than the number of unique patches by event. To my knowledge, pivot tables do not allow unique values to be summarized.

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

    Re: Count unique values by attribute

    Thanks for further explanation, but is there any way for you to answer the question for "Event" and "Patch" in question?

    Is it 304?

  14. #14
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    Thanks for further explanation, but is there any way for you to answer the question for "Event" and "Patch" in question?

    Is it 304?
    Ah, sorry, I think I misunderstood you. For event 20000084, I count 55 unique patches (or 368 total records) using the formula:

    Please Login or Register  to view this content.
    So I guess I need a dynamic code (macro?) that applies the above formula to each unique event identifier (20000001 through 20000150 in the sample data I provided).

  15. #15
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    Thanks for further explanation, but is there any way for you to answer the question for "Event" and "Patch" in question?

    Is it 304?
    I am attaching the solution that I am seeking which I produced by manually counting the unique values, but I don't know how to automate this process for my entire data set. You can see how this differs from the solution you provided which is simply a sum of the total number of records for each event.

    sample_solution_ppickell.xlsx
    Last edited by ppickell; 05-19-2012 at 09:51 PM.

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

    Re: Count unique values by attribute

    Sorry but I just can't crack this nut. I have put in a request to the other forum guru's for a possible solution.

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

    Re: Count unique values by attribute

    How about this in column G

    Notice I used some named ranges.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    How about this in column G

    Notice I used some named ranges.
    This works like a charm. Thank you so much for your persistence!

  19. #19
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by jeffreybrown View Post
    How about this in column G

    Notice I used some named ranges.
    How can I adjust the formulas to account for more than 569 records?

    EDIT: Nevermind; I reassigned the array assigned to the names "patch" and "event".
    Last edited by ppickell; 05-20-2012 at 02:27 AM.

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

    Re: Count unique values by attribute

    To make the ranges dynamic, change the named ranges to the following...

    BigNum =9.99999999999999E+307 >> This is only if the column is full of numbers
    event =Sheet1!$A$2:INDEX(Sheet1!$A:$A,MATCH(BigNum,Sheet1!$A:$A))
    patch =Sheet1!$B$2:INDEX(Sheet1!$B:$B,MATCH(BigNum,Sheet1!$B:$B))

  21. #21
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Count unique values by attribute

    I introduced a helper column. I think the attached contains the solution you want...?

    EDIT: Ahh, didn't see that jeffreybrown had already found a solution. Anyway; I'll leave this on as a different solution to the same problem.
    Attached Files Attached Files
    Last edited by Søren Larsen; 05-20-2012 at 10:27 AM.
    Sincerely
    S?ren Larsen

    "Give a man a fish, and you'll feed him for a day. Give a man a fishing rod, and he'll steal your yacht!"

  22. #22
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Count unique values by attribute

    One more won't hurt then?
    Attached Files Attached Files

  23. #23
    Registered User
    Join Date
    05-18-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Count unique values by attribute

    Quote Originally Posted by WHER View Post
    One more won't hurt then?
    Thanks, everyone! They all work very well!

  24. #24
    Registered User
    Join Date
    11-19-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Count unique values by attribute

    Hello,

    I did code an automatic unique within pivot for excel 2007. should work in 2010.
    If you want to try:
    http://lazyvba.blogspot.co.uk/2010/1...-to-count.html
    have a good day

+ 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