Google Sheet Querying a Date Value from a cell in another sheet - google-sheets

I would like to ask if anyone knows the exact syntax to get this query working on google sheets.
I currently have 3 Sheets:
1. Sheet A, RAW DATA (where all my Raw Data is)
2. Sheet B, Main Page (where I have my user input on the date)
3. Sheet C, Filtered Data (where I would like to use the Query to pull out filtered data from Sheet A)
So I have set up a generic query goes like this in Sheet C, it works:
=QUERY('RAW DATA'!A:J, "SELECT * WHERE F > date '2018-03-20' AND F < date '2018-03-22' AND F IS NOT NULL ORDER BY F",1)
I would now like to replace the 2 dates, with user input dates, which are B5 and D5 in Sheet B.
I have tried this syntax:
=QUERY('RAW DATA'!A:J, "SELECT * WHERE F > date '"&Main Page!B5&"' AND F < date '"&Main Page!D5&"' AND F IS NOT NULL ORDER BY F",1)
Do note that B5 and D5 have already been set up with data validation rules to ensure that only date values are allowed.
Could anyone point out my mistake?
Thank you!

The date provided to QUERY must be:
a string of
format yyyy-mm-dd
Try changing from
date '"&Main Page!D5&"'
to
date '"&TEXT(Main Page!D5,"yyyy-mm-dd")&"'

Related

Convert date in query function and order by date column

I have a large set of data that's updated every 2 weeks, so I'm looking to simplify the solution.
Imported sheet has data from D6:G. The date is in Column B.
Right now, I insert column H and use
=ARRAYFORMULA(if(isblank(B6:B),"",datevalue(B6:B)))
In a new sheet, I import the data and order by Col H (the converted date) and Col G (the employee's name).
=query(TimeApr10!B6:H,"select B,C,D,E,F,G where H is not null order by H,G asc")
Is there a way to combine the two steps by converting Col B in the query to datevalue? If I don't convert the date, the ordering doesn't work.

Google Sheets date query won't work on specific columns

I have data that I'm importing from Salesforce, and I'm using query functions to find all rows where any of the columns has a date in a given range. Here's an example of the data:
The query that's not working is:
=query('Salesforce Data'!A2:C,"SELECT A,C WHERE C >= date '"&TEXT(DATEVALUE($A$1),"yyyy-mm-dd")&"' AND C < date '"&TEXT(DATEVALUE($B$1),"yyyy-mm-dd")&"'")
I'm using the same query except in one case, it's looking at dates in column B, and in the other, it's looking at the dates in column C. The column B version works, the column C version does not. I have verified that there is at least one date in column C that falls in the range, so it should not be an issue of no data, as the error suggests:
I've looked over data formatting, and there is no difference between columns B and C in that regard. These are the same types of field in Salesforce as well, so I would not expect a difference in formatting. I tried manually changing the first value in column C to a date (that was an obvious difference between the columns), but that also didn't work.
After a lot of trial and error, I found the issue: it seems that Google Sheets classifies the column of data based on what the majority of the cells are. So, even though both columns B & C have some cells with valid dates and some with a - signifying null, column B has more dates than strings, but C has more strings than dates, so date compare queries won't work on column C at all.
My solution for now is to add a formula sheet to transform all of the null values, -, into a date that won't mess with my query, 1/1/1970:
Example formula:
=IF( OR('Salesforce Data'!C2="-",'Salesforce Data'!C2=""), date(1970,1,1), 'Salesforce Data'!C2)
Another solution would be to edit the data source, but this solution will work entirely within sheets.
Also note, I dragged this formula down far below where I needed, just in case, make sure that if you have a text column (like my column A), you replace empty values there with junk text of some sort. At first I replaced with 0 and then my text column wasn't picked up by the query.
try:
=ARRAYFORMULA(QUERY(TO_TEXT('Salesforce Data'!A2:C),
"select Col1,Col3
where Col3 >= date '"&TEXT(A1, "yyyy-mm-dd")&"'
and Col3 < date '"&TEXT(B1, "yyyy-mm-dd")&"'", 0))
Thank you thank you so so much. This thread helped me a lot.
I have used these from this thread. Someone may need in future:
"select A, B, C, G, H, J where I='"&TEXT($A$2, "dd-mmm-yyyy")&"'"
"select B, C WHERE F= date '"&TEXT(DATEVALUE($A$2),"yyyy-mm-dd")&"'"
"select A, B, C, G, H, J where I='"&TEXT($A$2, "dd-mmm-yyyy")&"' or I='"&TEXT($A$2, "d-mmm-yyyy")&"'"

Filter function and ignoring a condition

I used the following formula to filter a date range between the date entered in A3 and date entered in B3 and also the data entered in C3 that matches column D
=Filter('Sort Violations'!P2:P,'Sort Violations'!C2:C>=Int(A3),'Sort Violations'!C2:C<=Int(B3),'Sort Violations'!D2:D=C3)
I'd like the formula to work where if C3 is empty/blank, the filter still pulls all data from the other conditions in the filter (date range). I think it needs a len in the formula but I'm not sure how...
try this if it works for you:
=IF(LEN(C3), FILTER('Sort Violations'!P2:P, 'Sort Violations'!C2:C>=INT(A3),
'Sort Violations'!C2:C<=INT(B3),
'Sort Violations'!D2:D=C3), )

Google Sheets: query to convert column to date and compare to another date [duplicate]

This question already has an answer here:
How to compare dates or date against today with query on google sheets?
(1 answer)
Closed 5 months ago.
I need to compare a text column, which contains a string that represents a date in the format dd/mm/yyyy, to another date.
The range is in another sheet. I cannot edit it to format the column to a date field:
I need something like this:
=query(importrange(...); "select A, B WHERE C >= '2017-01-08'")
Of course this doesn't work because only Aldo row will be visible. Is there any way to convert, in the query function, the column C to a date or to a string with the pattern yyyy-mm-dd to compare it to the right hand side value of the comparison?
Cumbersome but seems to work:
Import (say to Sheet1!A1):
=importrange(" k e y ","Sheet1!A:C")
Add a column with recognisable date formats (say in D1):
=ArrayFormula(datevalue(C1:C))
then apply the query:
=query(Sheet1!A:D,"select A,B,C where D >= date '2017-01-08' ")
I know this is WAY late, but I ran into this same problem a couple of years ago and fixed it by doing the following:
Cell A1 contains: 2/21/2018
Cell A2 contains: =DATEVALUE(B1)
which results in 43152 and I copy into the QUERY statement:
Cell A5 formula: =QUERY(Sheet!Range, "select B, C, E where B=43152")
But, to make it somewhat better by only having to type in the date, I used the CONCATENATE function to create the text "select B, C, E where B=43152" in a cell and then referenced that cell in my QUERY, like so:
Cell B1: =CONCATENATE("select B, C, E where B=", A2)
Cell A5 formula: =QUERY(Sheet!Range, B1)
This works well for me and I hope it helps someone else or at least gives someone an idea.

QUERY with "contains" and "date range"

I am trying to write a formula that would pull data based on a selection I made and within the date range I put in as well. The code I wrote below does not work. What am I writing incorrectly?!
=QUERY(INDIRECT(CONCATENATE(A2,"!A:FB")), "where (C >=date '"&TEXT('Feedback Report'!C3,"yyyy-mm-dd")&"' and C <= date '"&TEXT('Feedback Report'!D3,"yyyy-mm-dd")&"' and E contains '"&B2&"' ",1)
I asked too soon. After trying one more time, I have found a solution:
=QUERY(INDIRECT(CONCATENATE(A2,"!A:FB")), "Select * where (C >=date '"&TEXT('Feedback Report'!C3,"yyyy-mm-dd")&"' and C <= date '"&TEXT('Feedback Report'!D3,"yyyy-mm-dd")&"' and E contains'"&B2&"')")
Here is the breakdown:
Formula is on a sheet that pulls data from another source (raw data)
INDIRECT(CONCATENATE(A2,"!A:FB")) is where the data is stored
A2 is the sheet I choose from a drop down menu to have the data be pulled from
C is the date column of the raw data
C3 is the starting query date
D3 is the ending query date
E is the category column
B2 is the category value I want to search on
Hope this helps people in the future.

Resources