Search code examples
google-sheetsgoogle-sheets-formula

Filter by date in Google sheet


I am having trouble filtering data by dates which will filter only active permits. I have a set of data coming in through an importrange.(A1)

=ImportRange("https://docs.google.com/spreadsheets/d/1aA_yAOnGa_yJOguCd9f24qWohFj3ciBCwqZiBfIf2Z4/edit?usp=sharing","Formula1!M1:Q10")

Today's date is set (G2)

=TODAY()

I showed the wished result under active permits

Under test formula you can see the formula I put, but only one line comes up.

=query({A1:E},"select Col1,Col2,Col3,Col4,Col5 where Col1 is not null "&if(len(G2)," and Col3 <= date '"&text(G2,"yyyy-mm-dd")&"' ",)&if(len(G2)," and Col4 >= date '"&text(G2,"yyyy-mm-dd")&"' ",)&" ",1)

I tested when instead of getting data from an importrange I get them by copy and paste and the formula works. See Copy of Sheet1 tab

The wished result is to get the query to only import the permits which are active (Today is between start and end date)

Please help

Link to test sheet https://docs.google.com/spreadsheets/d/1JYedYUt15SFJ50P-8Wxv4akYLmZKTkdss9vvvMQ4hUw/edit?usp=sharing


Solution

  • 01 - Format date and time correctly, these cells have 15, 20 months X_X

    enter image description here

    02 - Use this formula with i adjusted the col selection,

    =query(IMPORTRANGE("1R13d4k1q3P7KbSGCYHP7a5oWLl4BjWGwjNL8-40AVIM","Sheet1!A1:E"),"
           select * where Col1 is not null "&if(len(K2)," 
           and Col3 <= date '"&text(K2,"yyyy-mm-dd")&"' ",)&if(len(K2)," 
           and Col4 >= date '"&text(K2,"yyyy-mm-dd")&"' ",)&" ",1)
    

    enter image description here Note: the yellow table to check the logic you want, return the rows when start is <= TODAY() And End is >= TODAY(), and the returned result is correct, your desierd output end date is proir to TODAY() see the red cells, and its correct in the source data

    enter image description here