Search code examples
google-sheetsgoogle-sheets-formulaarray-formulas

Count unique comma-separated values in column B based on criteria in column A


How do I list and count unique comma-separated values (column B in the example below) if the number in column A is larger (or smaller) than X? In other words, how do I turn the below table...

   Day   |          Fruits
+--------|--------------------------+
         |
    20   |   Apple, Banana, Pearl
         |
    24   |   Apple, Pearl
         |
    32   |   Banana, Pearl
         + 

...into this 👇, with criteria: Day < 28.

  Fruit    |   Frequency
+----------|---------------+
           |
  Apple    |       2
           |
  Pearl    |       2
           |
  Banana   |       1
           +

A solution proposed by @AdamL in this question is really close to what I want to achieve, but I can't figure out how to list values based on criteria from another column. Here's what Adam came up with:

=ArrayFormula(QUERY(TRANSPOSE(SPLIT(JOIN(",",A:A),",")&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))

Solution

  • use:

    =ARRAYFORMULA(QUERY(SPLIT(FLATTEN(
     IF(IFERROR(SPLIT(B2:B, ","))="",,A2:A&"♦"&TRIM(SPLIT(B2:B, ",")))), "♦"),
     "select Col2,count(Col2) 
      where Col1 < 28 
        and Col1 is not null 
      group by Col2 
      label count(Col2)''"))
    

    enter image description here