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

Customizing order of joined cell + header values when cell value isn't blank in Google Sheets (1A)


How can I modify the equation in this Google Spreadsheet so that a user can customize the order of header & Value pairs?

Goal 1: Order display based on the number before a header. Goal 2: Don't display the header/value pair when there is no number present in the header (alt: when a wildcard value is presented before the header don't display)

This equation results in the following display

INDEX(TRIM(SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(IF(A2:D6="",,"♦"&A1:D1&" "&A2:D6)),,9^9)), "♦", CHAR(10))))

enter image description here

However, it would be preferable if the order could be arranged based on providing an order in the header value like the following display where empty cells (where ISBLANK=TRUE) are not displayed (2 & 3). Also, the header/value pair for "History" are not displayed since a _History is provided in the column header without a number.

enter image description here

I believe the following formula related to another question, which reordered headers (but also did other functions) can be modified to achieve the desired function

INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
 IF(TRANSPOSE(SORT(TRANSPOSE(A9:D10), FLATTEN(A8:D8), 1))="yes", 
 REGEXREPLACE(TRANSPOSE(SORT(FLATTEN(A8:D8))), "(^\d+_)", )&"", )),,9^9))), ",$", )) 

The shared spreadsheet is Google Spreadsheet is here


Solution

  • Try:

    =ARRAYFORMULA(TRIM(SUBSTITUTE(TRIM(FLATTEN(QUERY(REGEXREPLACE(SORT(TRANSPOSE(
     IF((A2:D4<>"")*(REGEXMATCH(A1:D1, "^\d+_")), "♥"&A1:D1&" "&A2:D4, )), 
     FLATTEN(A1:D1), 1), "\d+_", ),,9^9))), "♥", CHAR(10))))
    

    enter image description here

    enter image description here