How to get unique values in a column, including cells with multiple values seperated by commas, in Google Sheet?

Viewed 77

I have a column in a Google Sheet, which in some cases, includes multiple values separated by commas — like this:

Value
A example
B example
C example
D example
A example, E example
A example, F example
G example, D example, C example

I would like to count all occurrences of the unique values in this column, so the count should look like:

Unique value Occurrences
A example 3
B example 1
C example 2
D example 2
E example 1
F example 1
G example 1

Currently, however, when I use =UNIQUE(A2:A), the result gives this:

Unique value Occurrences
A example 1
B example 1
C example 1
D example 1
A example, E example 1
A example, F example 1
G example, D example, C example 1

Is there a way I can count all of the instances of letters, whether they appear in individually in a cell or appear alongside other letters in a cell (comma-seperated)?

(This looks like a useful answer in Python, but I'm trying to do this in Google Sheets)

2 Answers

try:

enter image description here

Formula in C1:

=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ")),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))

Or, as per the comments, split on the combination instead:

=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))

2nd EDIT: To order descending by count use:

=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 Order By count(Col1) desc label count(Col1) ''"))

enter image description here

Assuming data in A1:A7:

In C1:

=SORT(UNIQUE(FLATTEN(ARRAYFORMULA(SPLIT(A1:A7,", ")))))

In D1:

=ARRAYFORMULA(MMULT(0+ISNUMBER(SEARCH(", "&ColumnCSpilledRange&", ",", "&TRANSPOSE(A1:A7)&", ")),ROW(A1:A7)^0))

Replace ColumnCSpilledRange appropriately.

Related