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

JOIN header row values across a row based on non-blank values in cells


So I have two rows:

ID TagDog TagCat TagChair TagArm Grouped Tags (need help with this)
1 TRUE TRUE TagDog,TagArm

Row 1 consists mainly of Tags, while rows 2+ are entries. This data ties ENTRIES to TAGS.

What I'm needing to do is concatenate/join the tag names per entry. For example, look at the last column above.

I suspect we could write a formula that would:

  • Create an array of non-empty cells in the row. (IE: [2,4])
  • Return it with the header row A (IE: [A2,A4])
  • Then join them together by a comma

But I am unsure how to write the formula, or if this is even the best approach.


Solution

  • Here's the formula:

    ={
      "Grouped Tags (need help with this)";
      ARRAYFORMULA(
        REGEXREPLACE(TRIM(
          TRANSPOSE(QUERY(TRANSPOSE(
            IF(NOT(B2:E11),, B1:E1)
          ),, COLUMNS(B1:E1)))
        ), "\s+", ",")
      )
    }
    

    enter image description here

    The trick used is called vertical query smash. That's the part:

    TRANSPOSE(QUERY(TRANSPOSE(...),, Nnumber_of_columns))
    

    You can find a brief description of this one and his friends here.