+ Reply to Thread
Results 1 to 21 of 21

Count Unique Text Strings in a Column, Based on Criteria from a Different Column

  1. #1
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Question Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Hello Excel Forum.

    I am trying to count the unique items on one column, based on criteria from a different column. For example, suppose I have two columns, A and B, as follows:

    Column A Column B
    A 5
    A 5
    A 6
    A 7
    B 8
    B 9
    B 10
    B 11

    Suppose I want everything unique in Column B that aligns to specification "A" in Column A. In this case, the function should spit out "3," since the three unique numbers are 5, 6, and 7. Now, I have tried the following two formulas to no avail. Also note, I am using Google Sheets, which hasn't been an issue up to this point, but I think I may be testing Google's limitations now.

    Option 1:

    ={SUMPRODUCT((('Completed Sales File - 8/22 - ROUGH'!$D1:$D1505=$A$2)/COUNTIFS('Completed Sales File - 8/22 - ROUGH'!$D1:$D1505,'Completed Sales File - 8/22 - ROUGH'!$D1:$D1505&"",'Completed Sales File - 8/22 - ROUGH'!$E1:$E1505,'Completed Sales File - 8/22 - ROUGH'!$E1:$E1505&"")))}

    The above formula is close, but double counts repeat items. For example, if I ran the above formula for the two columns above, it would say that there are four unique values, and it would double count the 5.

    Option 2:

    =SUM(FREQUENCY(if('Completed Sales File - 8/22 - ROUGH'!D:D=$A$2,MATCH('Completed Sales File - 8/22 - ROUGH'!E:E,'Completed Sales File - 8/22 - ROUGH'!E:E,0)),ROW('Completed Sales File - 8/22 - ROUGH'!E:E)-ROW('Completed Sales File - 8/22 - ROUGH'!E1)+1)>0)

    The above formula is returning 0...

    Any idea's as to what is wrong here? Is it just Google Sheets? Is it because I am referencing a column in a different sheet, even though its part of the same workbook? Is my syntax off? Thanks.

    Thanks for the support!

  2. #2
    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 Text Strings in a Column, Based on Criteria from a Different Column

    SlikRick welcome to the forum.

    With data limited to those 8 rows try.

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

  3. #3
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Hi Dave,

    Thanks for the support! I think we're getting closer! I am now getting a Divide by Zero error. Any guesses why? Maybe because its text or blanks?

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by FlameRetired View Post
    =SUMPRODUCT(($A$1:$A$8="A")/COUNTIF($B$1:$B$8,$B$1:$B$8))
    Dave, this fails if A shares any values with B.

  5. #5
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    I tried adjusting Dave's formula from this:

    =SUMPRODUCT(($A$1:$A$8="A")/COUNTIF($B$1:$B$8,$B$1:$B$8))

    To this:

    =SUMPRODUCT(($A$1:$A$8="A")/COUNTIF($B$1:$B$8,$B$1:$B$8&""))

    And I got 2.010799136. The correct answer is 3. So I'm not sure what Excel is doing. But it feels like we're moving in the right direction.

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    This ARRAY FORMULA seems to work:
    *Always press CTRL+SHIFT+ENTER to confirm entry after pasting or editing an ARRAY FORMULA in the Formula Bar.
    When copying the formula to other cells, make sure the copied cell is not part of the Paste Area.

    Please Login or Register  to view this content.
    RE #DIV/0!, in the "standard" version of your formula, the divisor is NEVER zero, as each element appears at least once. This ceases to be true if you add a secondary condition which some elements don't satisfy.
    Last edited by leelnich; 08-24-2017 at 01:26 AM.

  7. #7
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    When I try Leelnich's formula, I am getting the Div / Zero issue. I have a good feeling it's blank related.

    How do I adjust Leelnich's formula to account for potential blanks in Column A and Column B?

    =SUMPRODUCT(IF($A$1:$A$8="A",1/COUNTIFS(A1:A8,"A",$B$1:$B$8,$B$1:$B$8)))
    Last edited by SlikRick; 08-24-2017 at 01:22 AM. Reason: Added Formula

  8. #8
    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 Text Strings in a Column, Based on Criteria from a Different Column

    That is because COUNTIFS(A1:A8,"A",$B$1:$B$8,$B$1:$B$8) is returning FALSE in the array where there are no "A"s. Reciprocation coerces those into 0s. 1/0 is #DIV0! error.

    The one I posted would return those errors too if there where blank cells in B1:B8.
    Last edited by FlameRetired; 08-24-2017 at 01:27 AM.

  9. #9
    Forum Expert shukla.ankur281190's Avatar
    Join Date
    05-17-2014
    Location
    Lucknow, India
    MS-Off Ver
    Office 365
    Posts
    3,935

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    tRY

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


    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer.
    If I helped, Don't forget to add reputation (click on the little star ★ at bottom of this post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)

  10. #10
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Is it still CTRL + SHIFT + ENTER for a Mac?

    Also. Does the Array Feature still work on Google Docs?
    Last edited by SlikRick; 08-24-2017 at 01:29 AM.

  11. #11
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    This ARRAY FORMULA handles the blanks:
    Please Login or Register  to view this content.
    Of course, Ankur's formula (POST #9) is the best option. It's shorter AND faster.
    Last edited by leelnich; 08-24-2017 at 01:40 AM.

  12. #12
    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 Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by SlikRick View Post
    I tried adjusting Dave's formula from this:

    =SUMPRODUCT(($A$1:$A$8="A")/COUNTIF($B$1:$B$8,$B$1:$B$8))

    To this:

    =SUMPRODUCT(($A$1:$A$8="A")/COUNTIF($B$1:$B$8,$B$1:$B$8&""))

    And I got 2.010799136. The correct answer is 3. So I'm not sure what Excel is doing. But it feels like we're moving in the right direction.
    That is because COUNTIF is assigning a value to the empty strings.

    Try

    =SUMPRODUCT((($A$1:$A$8="A")*($B$1:$B$8<>""))/COUNTIF($B$1:$B$8,$B$1:$B$8&""))

    I should have refreshed before posting. Lee's does the same as this one. This one does not have to be array entered.
    Last edited by FlameRetired; 08-24-2017 at 01:36 AM.

  13. #13
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Flame. This looks super close. Let me try it a few times!

  14. #14
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    BINGO. Flame's formula works perfectly...On a MAC...On GOOGLE SHEETS.

    I just started a new job this week and the whole team has been scratching their heads about this issue the whole day. Cannot wait to go into work tomorrow. Thanks a ton for the support folks!

  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 Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by leelnich View Post
    Dave, this fails if A shares any values with B.
    So it does! Nice catch.

    This should cover all the bases ... I hope.

    =SUMPRODUCT((($A$1:$A$8="A")*($B$1:$B$8<>"")*($A$1:$A$8<>""))/COUNTIFS($A$1:$A$8,$A$1:$A$8&"",$B$1:$B$8,$B$1:$B$8&""))

  16. #16
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by FlameRetired View Post
    =SUMPRODUCT((($A$1:$A$8="A")*($B$1:$B$8<>"")*($A$1:$A$8<>""))/COUNTIFS($A$1:$A$8,$A$1:$A$8&"",$B$1:$B$8,$B$1:$B$8&""))
    The second column A test is redundant; it's always true if "A" is found, and irrelevant if not.
    Last edited by leelnich; 08-24-2017 at 02:13 AM.

  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 Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by leelnich View Post
    The second column A test is superfluous; it's always true if "A" is true, and irrelevant if not.
    Yup! I gave up in disgust (with me) and decided to cover it all. Thanks for commenting though.

  18. #18
    Registered User
    Join Date
    08-23-2017
    Location
    Bay Area, CA
    MS-Off Ver
    Mac - 2011
    Posts
    8

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Hey Folks. I wanted to share something interesting I noticed about the Formula.

    I do not want to reveal too many details about my spreadsheet, but what are your thoughts on the following scenario. In my spreadsheet, Column A is the 50 states and let's assume that Column B represents the names of different fast food restaurants. Let's assume that there is one Fast Food Restaurant, call it BurgerVille that is pervasive across the entire document and across multiple states. BurgerVille represents almost 15% of the entries in Column B. If I run the SUMPRODUCT for a state that does not have a BurgerVille, the formula works perfectly. However, let's assume that a given state, say CA, has 12 Burgervilles and 40 other restaurants. When I run the SUMPRODUCT formula, the answer comes out as 40.325035.

    Basically, I think Google Sheets is somehow confused about BurgerVille being in different states, and it's essentially dividing this entry into small chunks. It's as if saying that .325035 of the BurgerVille entries are in CA? Fortunately, this is an easy fix. I just wrapped the SUMPRODUCT with a ROUNDDOWN.

    But I wanted to pass along the bug and hear your thoughts. Thanks again as always folks!
    Last edited by SlikRick; 08-24-2017 at 02:23 AM. Reason: Typos

  19. #19
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Count Unique Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by FlameRetired View Post
    Yup! I gave up in disgust (with me) and decided to cover it all. Thanks for commenting though.
    Still, nice catch on the COUNTIFS($A$1:$A$8,$A$1:$A$8&"",$B$1:$B$8,$B$1:$B$8&"")). I knew there was an obvious fix.
    Last edited by leelnich; 08-24-2017 at 02:21 AM.

  20. #20
    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 Text Strings in a Column, Based on Criteria from a Different Column

    Thanks Lee.

  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 Text Strings in a Column, Based on Criteria from a Different Column

    Quote Originally Posted by SlikRick View Post
    Basically, I think Google Sheets is somehow confused about BurgerVille being in different states, and it's essentially dividing this entry into small chunks. It's as if saying that .325035 of the BurgerVille entries are in CA? Fortunately, this is an easy fix. I just wrapped the SUMPRODUCT with a ROUNDDOWN.

    But I wanted to pass along the bug and hear your thoughts. Thanks again as always folks!
    I am not that familiar with Google sheets. Though the first thing I would check would be the data. Are all spellings consistent? Are there leading/trailing spaces? I don't know what the limitations of Google functions are so could Case sensitivity be an issue? Are all "numbers" numeric or are some text "numbers"?

    Another thought would be the data source. Often when the source is the internet the non breaking space (character "160") is ubiquitous. They are difficult to detect visually and a pain to search for. SUBSTITUTE is your friend there.

    Yet the decimal returns are still odd.

+ 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] count unique records in one column based on criteria in a different column
    By rxg2669 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-10-2015, 12:38 AM
  2. Replies: 2
    Last Post: 11-21-2014, 12:59 PM
  3. [SOLVED] Count unique entries based off unique criteria in another column
    By hambly in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-11-2014, 03:33 PM
  4. Counting Unique text in column B based on a criteria from column a
    By clocmasta in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-18-2013, 06:40 PM
  5. Count unique occurrances based off criteria in a different column
    By Enovy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-05-2013, 03:28 PM
  6. Replies: 5
    Last Post: 05-31-2012, 08:48 AM
  7. Replies: 5
    Last Post: 09-18-2011, 10:44 PM

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