Query in Google Sheets WHERE and OR - google-sheets

I want to write a query to combine data from two sheets with same heading where data in first column is either a date or a specific string.
Example
=query({Sheet1!A:B;, Sheet2!A2:B}, "select * where Col1 > date 'YYYY-MM-DD' OR Col1 = 'example string' ")
The values are reflecting for date but not for the example string.
The example string entered for Col1 in the formula, is selected from a pulldown list in sheet2.

better try:
=FILTER({Sheet1!A:B; Sheet2!A2:B},
({Sheet1!A:A; Sheet2!A2:A}>"01/01/2000")+({Sheet1!A:A; Sheet2!A2:A}="example text"))

Related

Query and Importrange to get the last value of the selected row of spreadsheet | Google Sheets

Right now I am using this query to search for a row based on its Column 1 value. Then it takes the value from the last column. I need a way for it to automatically find the last column in the row since some of the rows have more columns than others.
This is what I had before, which I had manually specified the last column with a value:
=QUERY(IMPORTRANGE("link_redacted","PriceList!A1:AZ100000"), "Select Col10 where Col1 = '5531001'",1)
I have tried using LOOKUP with ARRAYFORMULA I couldn't get it to work:
=QUERY(IMPORTRANGE("link_redacted","PriceList!A1:AZ100000"), "Select (LOOKUP(1, ARRAYFORMULA(1/[Select Col1 where Col1 = '5531006']:[Select Col100 where Col1 = '5531006']<>"")[Select Col1 where Col1 = '5531006']:[Select Col100 where Col1 = '5531006']))",1)
Any ideas for a simpler way to do this?
Since no example is presented, I tested the formulas given but no source data is fetched.
so i created a a minimal, reproducible example
Example is the data on the left
Use this formula to get the last non empty columns values.
=ArrayFormula(IFERROR( REGEXEXTRACT( TRIM(TRANSPOSE(QUERY(TRANSPOSE(C3:E),,ROW(C3:E)))), "[^\s]+$")))

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

Google Sheets - Count Unique Values of Importrange Data between Given Dates

I have a sheet with a Name Column and corresponding Date column. I'm attempting to count the number of times unique Names appear for a specific Date range. The data is in a separate spreadsheet, so I'm using importrange in the formulas.
I created a sample spreadsheet here - we can pretend that the "Data" tab is on a separate spreadsheet file. I have the below formula so far but I'm not sure how to tell it to only count the unique Name values between the Dates in cells B2 and C2.
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8/edit#gid=0","Data!A:B"),"Select Count(Col1) where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2 <= date '"&TEXT(C2,"yyyy-mm-dd")&"' Label Count(Col1) ''")
Thanks for your help!
This formula should do it:
=COUNTUNIQUE(QUERY(IMPORTRANGE("1VHR-T9bo0E1KH4yZp-9wTW6JGjKh4b3qMdm99MP_vB8","Data!A2:B"),"select Col1 where Col2 >= date '"&TEXT(B2,"yyyy-mm-dd")&"' and Col2<= date '"&TEXT(C2,"yyyy-mm-dd")&"'",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)&"'")

ROUNDUP SUM values in a column in Google Sheets' query

I want to ROUNDUP the sum values in a column in my google sheets QUERY. I discovered adding format sum(Col2) '#' will do the trick as long as the sum is >.5. So how do I accomplish the same result if the value is <.5?
Here is my formula:
=QUERY(QUERY(Data!A3:D, "SELECT A,sum(B),C,D where A is not null GROUP BY A,C,D format sum(B) '#'", 0),"OFFSET 1",0)
Currently, if the result is <.5, it will just display a blank space.
Here is a sample sheet
the only possible way would be:
=ARRAYFORMULA(IFERROR(ROUNDUP(
QUERY(QUERY(Data!A3:D,
"SELECT A,sum(B),C,D where A is not null GROUP BY A,C,D", 0),"OFFSET 1",0)),
QUERY(QUERY(Data!A3:D,
"SELECT A,sum(B),C,D where A is not null GROUP BY A,C,D", 0),"OFFSET 1",0)))

Resources