Home > Back-end >  Query function in Google Sheets - Get non-empty last cell in a column depending on another column
Query function in Google Sheets - Get non-empty last cell in a column depending on another column

Time:10-27

I'd need to retrieve the last non-empty cell in a column based on changes on the adjacent column.

In the example below, the yellow cells are my target. They are defined by a change in column A.

enter image description here

CodePudding user response:

Your task can be accomplished in two steps:

  1. determine the rows that contain the desired data =index(query({A2:B20,ROW(A2:B20)},"select max(Col3) where Col2 is not null group by Col1 label max(Col3)''")) and assemble the result to a regular expression using formula JOIN("|", -- query result --) enter image description here the result we have a string: 5|11|14|19
  1. use a new query to output the values from the previously found rows =index(query({A2:B20,ROW(A2:B20)},"select Col1,Col2 where Col3 matches '"& join("|",query({A2:B20,ROW(A2:B20)},"select max(Col3) where Col2 is not null group by Col1 label max(Col3)''")) &"'")) enter image description here

the range A2:B20 is used for the example, but the formula also works with the open range A2:B

  • Related