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

Finding a value within a range(condition) in each array of values in google sheets


So, i have a data set where each row contains an array of values ranging from 0-600 which will eventually extend till the end of the sheet. It looks something like this. I'm trying to find the value that is within 100 and 200 then display it in the first column. Currently I'm using a FILTER Function in this format:

=FILTER(H2:BI2, H2:BI2 > 100, H2:BI2 < 200)

This returns results as expected for each row. However, I would like to use an array formula to apply it to the entire column which I did so in this format:

=ARRAYFORMULA(FILTER(H2:BI, H2:BI > 100, H2:BI < 200))

However, this does not work with an error message "FILTER range must be a single row or a single column.". What's the best way to solve this? Thanks in advance.

[EDIT : My data is a controlled set of data where only one of the values will be within that range. If in rare cases two values appear, it can be ignored or just select the first one.]


Solution

  • If you might have more than one value satisfying the conditions (for the only one in a row it also works) then this formula will give you the column with the first ones for every row:

    =ARRAYFORMULA(
      IFNA(INT(REGEXEXTRACT(
        TRANSPOSE(
          QUERY(
            TRANSPOSE(
              IF((H2:BI > 100) * (H2:BI < 200), H2:BI, "")
            ),
            ,
            COLUMNS(H2:BI)
          )
        ),
        "^\s*(\d+)"
      )))
    )
    

    enter image description here

    UPD

    If you are sure that there is no more than one value that satisfies the conditions, then you could also use this formula:

    =TRANSPOSE(
      SPLIT(
        TEXTJOIN(
          "",
          True,
          {
            ARRAYFORMULA(IF((H2:BI > 100) * (H2:BI < 200), H2:BI, "")) ,
            {
              TRANSPOSE(SPLIT(REPT(",-", ROWS(H2:BI) - 1), "-", True, True)); ""
            }
          }
        ),
        ",",
        True,
        False
      )
    )
    

    There will be a number in a cell, or a cell will have "" if there is no number in a row that is >100 and <200.

    enter image description here