Google Sheets Query Syntax Where Between Two Dates That Are Variables - google-sheets

In Google Sheets, I am trying to query data from my 'ALL DATA' sheet that is between two dates.
The two dates are in B1 and B2 of my 'CALCS' sheet. Here is what I have tried so far in the CALCS sheet:
=QUERY('ALL DATA'!$A:$AW,"select C,F,K,AA,AK,AM where C=1 and AK >= date '"&B1&"' and AK <= date '"&B1&"'")
I am getting: "Unable to parse query string for Function QUERY parameter 2: Invalid date literal [44713]. Date literals should be of form yyyy-MM-dd."
However, all of my dates in 'ALL DATA'!AK and B1 and B2 are in that format. Is there a way to do this so I can change the dates in B1 and B2 and the query updates for those dates?

replace AK >= date '"&B1&"' and AK <= date '"&B1&"' by
AK>=DATE'"&TEXT(B1;"yyyy-MM-dd")&"' AND AK<=DATE'"&TEXT(B2;"yyyy-MM-dd")&"'

use:
=QUERY('ALL DATA'!$A:$AW,
"select C,F,K,AA,AK,AM
where C=1
and AK >= date '"&TEXT(B1, "e-m-d")&"'
and AK <= date '"&TEXT(B2, "e-m-d")&"'")

Related

Datetime comparison query

I have an importrange array to get data from alot of sheets. I organise zhem into one list and then make calculations from this list.
WORKBOOK
https://docs.google.com/spreadsheets/d/1OH_LF9r04rRb1ZMuc26CwIq3NQ-qWVlb8mXJwuTechk/edit#gid=28668687
SOURCE WORKBOOK (Tracker)
https://docs.google.com/spreadsheets/d/1huVGusrmhZ60zy9pg59PKN_yfL1XulwvLW5EWwHguvA/edit#gid=2007038591
sheet MODTANEWBIE_QUERY
formula #1 (add UID to DATE):
=IF($B$2="No","",ARRAYFORMULA({MODTANEWBIE_PER!A$3&"#"&QUERY(IMPORTRANGE(MODTANEWBIE_PER!$C$3,"Tracker!"&B$3&":"&B$4&MODTANEWBIE_PER!$D$3),"select Col1 WHERE Col1 = "&A2)}))
formula #2 (get data from specific column in #Tracker based on date)
=IF($B$2="No","",ARRAYFORMULA({QUERY(IMPORTRANGE(MODTANEWBIE_PER!$C$3,"Tracker!"&B$3&":"&B$4&MODTANEWBIE_PER!$D$3),"select Col1 WHERE Col1 = "&A2)}))
For some reason I an unable to query the importrange's Date that corresponds to the cell A2 in _QUERY sheet.
Please am I missing something silly? Is there a format problem?
Thanks alot for any help!
formula should be:
=IF($B$2="No",,ARRAYFORMULA(MODTANEWBIE_PER!A$3&"#"&QUERY(IMPORTRANGE(MODTANEWBIE_PER!$C$3,"Tracker!"&B$3&":"&B$4&MODTANEWBIE_PER!$D$3),"select Col1 WHERE Col2 = date '"&TEXT(A2, "e-m-d")&"'")))
however:
tracker sheet does not contain today's date so A2 needs to be a valid date
dates on tracker sheet are in column B so B4 cant be A
query will then look for where Col2 where dates are
and query is picky on dates if they are not in this format: yyyy-mm-dd

Total by day in google sheets for a given month and year

I have the following table:
What I'm trying to do is to write a formula that totals by each unique day in the month and year given in B1 and C1 thus the output should be:
I'm not sure how to start with this, googling seems to suggest the use of SUMIF.
Sheet is here
try:
=QUERY(FILTER({A5:B, TIMEVALUE(C5:C)}, MONTH(A5:A)=MONTH(B1&1), YEAR(A5:A)=D1),
"select Col1,sum(Col3)
where Col1 is not null
group by Col1
label sum(Col3)''
format sum(Col3)'[hh]:mm'", 0)

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)

Query Subset of two tabs only returns #N/A

I am trying to combine two tabs of data to plan a daily routine. One tab is for recurring tasks and one for adhoc tasks.
The data on the tabs are mostly similar except on Sheet1 Col4 is a day of the week, whereas on Sheet2 Col4 is a date
What I would like is to choose the current day of the week and have the query return all recurring tasks for that day, as well as any adhoc tasks for that specific date
I can return both sets of data individually however I can't get the query to return both subsets together
Sheet1 Data
ID,TASK NAME,LOCATION,TASK DAY
REC1,Task 1,Office,Monday
REC2,Task 2,Office,Tuesday
REC3,Task 3,Field,Wednesday
REC4,Task 4,Office,Thursday
REC5,Task 5,Field,Friday
REC6,Task 6,Field,Monday
REC7,Task 7,Field,Tuesday
Sheet2 Data
ID,TASK NAME,LOCATION,TASK DATE
ADH1,AdHoc 1,Office,25/Jun/2019
ADH2,AdHoc 2,Office,26/Jun/2019
ADH3,AdHoc 3,Field,27/Jun/2019
ADH4,AdHoc 4,Office,28/Jun/2019
ADH5,AdHoc 5,Field,29/Jun/2019
ADH6,AdHoc 6,Field,30/Jun/2019
ADH7,AdHoc 7,Field,1/Jul/2019
This pulls the data from sheet 1 based on the day of the week in cell J1
=query({Sheet1!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = """&J1&""" ",0)
This pulls the data from sheet 2 based on today()
=query({Sheet2!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = date '" & text(today(),"yyyy-mm-dd") & "' ",0)
Thought this would work but merely returns an empty set
=query({Sheet2!A:D;Sheet1!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = date '" & text(today(),"yyyy-mm-dd") & "' OR Col4="""&J1&""" and Col1<>'' ",0)
When J1 = Tuesday and date = 25/Jun/2019 I should get
ID,TASK NAME,LOCATION,TASK DAY
ADH1,AdHoc 1,Office,25/Jun/2019
REC2,Task 2,Office,Tuesday
REC7,Task 7,Field,Tuesday
but instead, merely get #N/A
You almost had it.
Rather than put the array inside the queries; put the queries inside the array.
={QUERY(Sheet1!A:D, "SELECT A, B, C, D WHERE D = """&J1&""" ");
QUERY(Sheet2!A2:D, "SELECT A, B, C, D WHERE D = date '" & text(today(),"yyyy-mm-dd") & "'")}
Credit - Stacking multiple query output in one sheet:
Might be too late but issue was in date column after converting it to text query works as well :
=query({Sheet2!A:D; Sheet1!A:D}, "select Col2,Col1,Col3,Col4 where (Col4='"&Sheet1!J1&"' OR Col4 = '" & to_text(today()) & "')",0)

Resources