Search code examples
google-sheetsgoogle-sheets-formula

Grouping Preferences by email in Google Sheets


Can you help me with below using Google Sheets to transform the Google Form data below to have a list of activities with a list of potential attendees?

Context:

  • I am arranging a set of activities for a conference tour and wanted to try arrange everything using Google Forms.
  • I have collected the data from the group attendees using the following format.
  • Attendees can vote for any number of activities from a selection of 15
  • The output format is: Email address - Comma Seperated field containing the target activities. (see below)

Table of Data

How do I switch the table to sort the data by indvidual activity, so I have the following format sorted by activity: Data sorted by Activity


Solution

  • The expected results are constructed in several stages:

    Final Outcome

    stage 4


    1. Split and transpose results over 2 columns

    =query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")

    Refer: How to transpose & split multiple columns and repeat specific cells in a column

    2. Group everything by Email; Activities as a comma-delimited list

    =query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col1")

    Refer: Group everything by Column A and have Column B be a comma-delimited list of values

    3. Count number of email addresses for each Activity

    ={"Number of Attendees";arrayformula(if(N2:N<>"",iferror(len(regexreplace(N2:N,"[^@^]","")),),))}

    Refer: Count occurrences of string in a single cell in Google Sheets

    4. Create "helper" queries as substitutes in Filter formula

    • Email

    =query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col1") ``

    • Activities

    =query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col2")

    5. Combine everything into a single formula

    ={UNIQUE(FILTER(query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col2"), query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col1")<>"")),{"Number of Attendees";arrayformula(len(regexreplace(BYROW(UNIQUE(FILTER(query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2")},"select Col2"), query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2")},"select Col1")<>"")), LAMBDA(aa, JOIN(", ", FILTER(query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2")},"select Col1"), query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2")},"select Col2")=aa)))),"[^@^]","")))}, BYROW(UNIQUE(FILTER(query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col2"), query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col1")<>"")), LAMBDA(aa, JOIN(", ", FILTER(query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col1"), query({query({ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B2:B5, ", ",false))="",, if(A2:A5="","none",A2:A5)&"×"&SPLIT(B2:B5, ", ",false))), "×"), "where Col2 is not null")))},"select Col1, Col2 label Col1 'Email', Col2 'Activity'")},"select Col2")=aa))))}


    Raw Data

    raw data


    Stage 1 - split and transpose results over 2 columns

    stage1


    Stage 2 - group by Activity; comma-delimited email addresses

    stage 2


    Stage 3 - Count email addresses in each Activities row

    stage 3


    Stage 4 - Helper queries

    helper queries