google-sheetsgoogle-sheets-formulagoogle-query-language

How to retrieve the first row where the date is more recent than x


In Google Sheets we have one sheet called InvoicingPeriods:

     A           B                C 
 4/26/2024  5/23/2024   - Period 26/04 - 23/05 
 5/27/2024  6/23/2024   - Period 27/05 - 23/06 
 6/24/2024  7/21/2024   - Period 24/06 - 21/07 
 7/22/2024  8/18/2024   - Period 22/07 - 18/08 
 8/19/2024  9/15/2024   - Period 19/08 - 15/09

In another sheet called Invoice we would like to retrieve the string in column C when a given date is more recent than column A.

So if the given date is 6/23/2024 it should retrieve Period 24/06 - 21/07.

This query returns the first row of the sheet and doesn't apply the where properly:

QUERY(InvoicingPeriods!$A$3:C, "select C where A > '2024/06/23' order by A asc limit 1", 0)

Test sheet here.


Solution

  • You may try:

    =xlookup(date(2024,6,23)+1,InvoicingPeriods!A:A,InvoicingPeriods!C:C,,1)
    

    OR

    =QUERY(InvoicingPeriods!A3:C, "select C where A > date '"&text(today(),"yyyy-mm-dd")&"' order by A asc limit 1",0)