Search code examples
google-sheetsjoinsplitflattengoogle-query-language

How to create a comma separate aggregate in Google Sheets?


Given the following data set:

https://docs.google.com/spreadsheets/d/1wr7v93CM_kWygRNHyqMWcBFvd1XXkC5SYbjLjauS4SM/edit?usp=sharing

**people**  **channel**
person1 channel1
person2 channel1
person1 channel2
person3 channel2

How could I write a QUERY (or anything else that makes sense) such that the C column shows a comma separated list of the channels that a given person is in?

For example, I'd like the following output.

**people**  **channel**
person1 channel1 channel1, channel2
person2 channel1 channel1
person1 channel2 channel1, channel2
person3 channel2 channel2

Solution

  • try:

    =ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:A&"×", B2:B&",", ROW(A2:A)}, 
     "select max(Col2) where Col2 <> ',' group by Col3 pivot Col1"),,9^9)), "×")), ",$", ))
    

    enter image description here