Search code examples
arraysgoogle-sheetsgoogle-sheets-formulaflattengoogle-query-language

Google Sheets - Flattening a table into two columns, but returning results based on varying conditions


My table has data similar to the following: Raw Data

I am flattening the data into two columns using the following:

=INDEX(QUERY(SPLIT(FLATTEN(IF(SheetName!B1:D=TRUE, SheetName!B1:D1&"×"&SheetName!A1:A,)), "×"),"where Col2 is not null order by Col1 asc"))

The result is depicted here: Flattened Data

However, I also need to return within the "flattened data" columns the following:

1@email.com | 1@email.com

2@email.com | 2@email.com

3@email.com | 3@email.com

In other words, I need to return SheetName!A1:A&"×"&SheetName!A1:A for each email address contained in the email column (Column A), in addition to the other data that is being flattened into the column. I have tried variations using IF/IFS statements, wildcards (not permitted within IFs), etc. However, I am now seeking some help after striking out many times. Thanks for any help you can offer!

Update Example spreadsheet with sample data and current vs. desired results (formula in cell H2): https://docs.google.com/spreadsheets/d/1wq9kR4UqYeWsqSCbnkGGHbR-TIHTfaBb_ixGk41X2cs/edit?usp=sharing


Solution

  • perhaps:

    =INDEX(QUERY(SPLIT(FLATTEN(IF(SheetName!B1:D=TRUE, 
     SheetName!B1:D1&"×"&SheetName!A1:A&"×"&SheetName!A1:A,)), "×"),
     "where Col3 is not null order by Col1 asc"))
    

    enter image description here


    update:

    =INDEX({QUERY(SPLIT(FLATTEN(
     IF(Sheet1!B1:F=TRUE, Sheet1!B1:F1&"×"&Sheet1!A1:A,)), "×"),
     "where Col2 is not null order by Col1"); 
     TEXT(UNIQUE(FILTER(A2:A, A2:A<>"")), {"@", "@"})})
    

    enter image description here