How do I Query with date range included? - google-sheets

I have built a google sheets query and I want to improve it by adding a date range. Any ideas on how I can improve my code so that it actually works so far it hasn't been working.
=QUERY(IMPORTRANGE("url", "Masterfile!A:AS"), "where Col35 is not null" & "where Col41 > date '"&TEXT($A$2,"yyyy-mm-dd")&"' and Col41 <= date '"&text($B$2,"yyyy-mm-dd")&"'",1))

It is always best to provide a copy of the sheet when asking for google sheet help. In this case I am working blind as cannot see the values of A2 and B2. Assuming they are both dates. The syntax is off for the 2nd parameter of QUERY() which happens quite often and sometimes takes some time to figure out the issue from the generic error the QUERY() function throws.
I typically break the 2nd parameter out into a formula to see how it evaluates when I get an error. Using the two dates I picked here are the results:
The formula is:
="where Col35 is not null" & "where Col41 > date '"&TEXT($D$49,"yyyy-mm-dd")&"' and Col41 <= date '"&text($D$50,"yyyy-mm-dd")&"'"
And this evaluates to:
where Col35 is not nullwhere Col41 > date '1959-10-21' and Col41 <= date '1960-10-21'
Now one can see the problem after the NULL keyword. In QUERY() we can use logical operators like AND, OR an NOT.
="where Col35 is not null AND where Col41 > date '"&TEXT($D$49,"yyyy-mm-dd")&"' and Col41 <= date '"&text($D$50,"yyyy-mm-dd")&"'"
Evaluates to:
where Col35 is not null AND where Col41 > date '1959-10-21' and Col41 <= date '1960-10-21'
So, putting it together should work:
=QUERY(IMPORTRANGE("url", "Masterfile!A:AS"), "where Col35 is not null AND where Col41 > date '"&TEXT($D$49,"yyyy-mm-dd")&"' and Col41 <= date '"&text($D$50,"yyyy-mm-dd")&"'",1))

Related

Google sheet query has no output

Very strange, a query function I have used for years with no issue is not working on my latest google sheet.
It keeps saying no output even though I know it should have an output.
This is the query
=(Query(Results!A2:E,"Select A,C,B,D,E Where A >= date '"&TEXT(B10,"yyyy-MM-dd")&"' and A <= date '"&TEXT(B10,"yyyy-MM-dd")&"' "&if(len(J6)," and C='"&J6&"' ",)&" Order by A Desc",0))
You can view the sheet at:
https://docs.google.com/spreadsheets/d/1vobnovcVLpMM3lbbDXPekNAkoj_zhlG5Kd5ksQg5rVg/edit?usp=sharing
Your help is greatly appreciated
Transform Column A in dates by
=arrayformula(iferror(int(A2:A*1),date("20"&mid(A2:A,7,2),mid(A2:A,1,2),mid(A2:A,4,2))*1))
try:
=ARRAYFORMULA(QUERY({TO_DATE(1*REGEXREPLACE(TO_TEXT(Results!A2:A),
"(\d+)/(\d+)/(\d+)(?:.*)?", "$2/$1/$3")), Results!A2:F},
"select Col1,Col4,Col3,Col5,Col6
where Col1 >= date '"&TEXT(D4, "yyyy-MM-dd")&"'
and Col1 <= date '"&TEXT(G4, "yyyy-MM-dd")&"'"&IF(LEN(J6),"
and Col4='"&J6&"' ",)&"
order by Col1 desc", 0))

Using Query to get the dates from a cell Google sheets

Context
Following on from this question, I am trying to make this more dynamic. I want to input data in a cell 'D1' to then make Query bring back the data from another spreadsheet. Below is a depiction of what this looks like.
This is the formula I tried:
=QUERY(Query(ImportRange("ID", "AvevaGroupPrice"),"offset 1", 0), "where Col1 >= Date " &D1)
I used this format because eventually I want to create a dynamic date range where I type the respective dates I need in the first two 'D' rows.
Problem
How can I make this work using data in cell D1?
Based on the Query Language Documentation, when you include date keyword you need to use a date with yyyy-mm-dd format
In your current function, the date was converted to a number when appended to the string.
Sample: ="date "&D1
Result: date 43830
You need to use TEXT() to convert the date to yyyy-mm-dd format.
Sample: ="where Col1 >= Date '"&TEXT(D1,"yyyy-mm-dd")&"'"
Result: where Col1 >= Date '2019-12-31'
(UPDATE)
You can also use TO_TEXT() to convert the Cell Value to a text value.
Sample: ="where Col1 >= Date '"&To_text(D1)&"'"
Result: where Col1 >= Date '2019-12-31'
Your Formula:
=QUERY(Query(ImportRange("ID", "AvevaGroupPrice"),"offset 1", 0), "where Col1 >= Date '"&TEXT(D1,"yyyy-mm-dd")&"'")
or
=QUERY(Query(ImportRange("ID", "AvevaGroupPrice"),"offset 1", 0), "where Col1 >= Date '"&To_text(D1)&"'")

Filter Array If Date >= Today()

I am trying to use the filter function to show only data in combination with the importrange function that meets certain conditions. I currently have the following formula:
=filter(ImportRange("XXXXXXX","Sheet1!A2:A"),ISBLANK(ImportRange("XXXXXXX","Sheet1!C2:C")),CONDITION2?)
So I currently have 1 condition that works, import the range if the value in range(C2:C) is blank; however, I am having trouble figuring out adding the second condition where it will import the range if there is a date and if that date is >= Today() in range(C2:C).
It is preferable to you use Query instead:
=QUERY(ImportRange("XXXXXXX","Sheet1!A2:C"),"Select Col1 where Col1 is not NULL and Col3 is NULL or Col3 > now()")
In order for this solution to work, column C must contain the dates in the correct format or null values (empty cells).

query and importrange function with condition btween two date and sort in order

I want to set query and importrange formula where i want the new sheet to show date that fall into today and yesterday date only and sort in ascending date and time
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1xk0Cvafixr5dayJkBkrzlYLFoA4E_PHYfPNQis12rc8/edit#gid=1702961582","CONSOLIDATE!A1:H"),"Select Col1,Col2,Col3,Col4,Col5,Col6,Col8 where Col1 <= date '"&text(B1,"yyyy-mm-dd")"' and >= date '"&text(B2,"yyyy-mm-dd")"' and order by Col1,Col2")")"
A couple of syntax errors, should be:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1xk0Cvafixr5dayJkBkrzlYLFoA4E_PHYfPNQis12rc8/edit#gid=1702961582","CONSOLIDATE!A1:H"),"Select Col1,Col2,Col3,Col4,Col5,Col6,Col8 where Col1 <= date '"&text(B1,"yyyy-mm-dd")&"' and Col1>= date '"&text(B2,"yyyy-mm-dd")&"' order by Col1,Col2")
i.e. remember to put '&' after the date as well as before, but don't put 'and' before order by.

Using Query & Importrange to filter between 2 dates and if condition to change the value of the dates based on condition

I am using query and importrange function to collect data from another Google sheet, I am filtering by the Publisher and the user-specified Start Date (F1) and End Date (G1). What I have right now works just for the start date, except that I haven't been able to filter it between the two dates. So I would like to keep all of the data that has fallen within the start date or the end date. Start date is Col9 and End Date is Col10 in the source data
The next step for this would be to change the start date and end date if it is less than or greater than the user-specified dates. For example, if the start date based on the importrange function is July 15th, but the user-specified date is August 1st then I want the cell with that date to be August 1st. The same logic applies to the end date, except any end date greater than the user-specified date should be changed to August 31st in this case. Is including this aspect in a single formula possible? Or would I have to create a separate query/script?
Below is a link to a copy of the formula I'm working with:
https://docs.google.com/spreadsheets/d/1hdE2q8UDdStx98WH9de3PljiMZzasZzhBChUAxLPgGQ/edit#gid=28163102
Below is the source data:
https://docs.google.com/spreadsheets/d/1N8m_YtO1Nc5AXSCAvT5Z1RiKs30bwjt6XDk9zA5QSOE/edit?usp=sharing
If I remove " and Col10 <= date '"&text(G1,"yyy-mm-dd")&"' " from the code, it works with the start date.
=QUERY(importrange("https://docs.google.com/spreadsheets/d/1N8m_YtO1Nc5AXSCAvT5Z1RiKs30bwjt6XDk9zA5QSOE/edit#gid=0","Activity Tracker - Publisher Co!B10:K"),"Select Col6, Col1, Col2, Col4, Col9, Col10 where Col2 is not null and Col9 is not null and Col1 contains 'Publisher 1' and (Col9 >= date '"&text(F1,"yyy-mm-dd")&"' and Col10 <= date '"&text(G1,"yyy-mm-dd")&"')")
try:
=QUERY(IMPORTRANGE(
"1N8m_YtO1Nc5AXSCAvT5Z1RiKs30bwjt6XDk9zA5QSOE",
"Activity Tracker - Publisher Co!B10:K"),
"select Col6,Col1,Col2,Col4,Col9,Col10
where Col2 is not null
and Col9 is not null
and Col1 contains 'Publisher 1'
and Col9 <= date '"&TEXT(G1, "yyyy-mm-dd")&"'
and Col10 >= date '"&TEXT(F1, "yyyy-mm-dd")&"'", 0)

Resources