Can i add three diferent rules to a query function? - google-sheets

I have two different QUERY functions, one is using two different rules, and the other uses only one. Is there a way to merge the two?
The first one uses a validation list of names to generate the results:
=QUERY('Respostas Formulário'!A:L;
"where A >= date '"&TEXTO(E2; "yyyy-mm-dd")&"'
and A <= date '"&TEXTO(F2; "yyyy-mm-dd")&"'"&
IF(B2="Todos os Mestres";;" and B = '"&B2&"'"); 1)
The second one uses another validation list to generate a similar result:
=QUERY('Respostas Formulário'!A:L;
"where A >= date '"&TEXTO(E2; "yyyy-mm-dd")&"'
and A <= date '"&TEXTO(F2; "yyyy-mm-dd")&"'"&
IF(C2="Todas as Tasks";;" and D = '"&C2&"'"); 1)
Is there any way to use both the date, the info in B2, and the info in C2 to generate a query?

try:
=QUERY('Respostas Formulário'!A:L;
"where A >= date '"&TEXTO(E2; "yyyy-mm-dd")&"'
and A <= date '"&TEXTO(F2; "yyyy-mm-dd")&"'"&
IF(B2="Todos os Mestres";;" and B = '"&B2&"'")&
IF(C2="Todas as Tasks";; " and D = '"&C2&"'"); 1)

Related

Using date ranges in filtered views (Google Sheets)

I have created a filtered view but need to filter columns by OR instead of AND e.g. date range in column N is 1st-20th Jan 2021 OR the date range in column T is 1st-20th Jan 2021 but this doesn't appear to be achievable without an OR custom formula. How do I modify the formula below to make it work?
=or( text(N:N, "yyyy-MM") = "2020-01", text(T:T, "yyyy-MM") = "2020-01" )
With a filtered view, you can add a column with this formula (here in Z1)
=arrayformula(--(mmult({--(text(N:N, "yyyy-MM") = "2020-01"),--(text(T:T, "yyyy-MM") = "2020-01")},{1;1})>0))
then filter with row has 1 in column Z
Explanation
this formula {--(condition1),--(condition2)} will generate 2 columns of 1/0 depending on the result of each condition
then mmult ({--(condition1),--(condition2)},{1;1} will add for each row if zero, one or two conditions are retrieved MMULT
finally, test if the result is greater than 0
-- transform boolean into 0/1
Complement
between 1st and 20th of January 2020
=arrayformula(--(mmult({
(text(N:N, "yyyy-MM-dd") >= "2020-01-01")*(text(N:N, "yyyy-MM-dd") <= "2020-01-20"),
(text(T:T, "yyyy-MM-dd") >= "2020-01-01")*(text(T:T, "yyyy-MM-dd") <= "2020-01-20")
},{1;1})>0))
or
=arrayformula(--(mmult({(N:N >= $AB$1)*(N:N <= $AD$1),(T:T >= $AB$1)*(T:T <= $AD$1)},{1;1})>0))

Google Sheets Query by Date and Group missing data

I am trying to query a data set by date and return ID's that are not listed in the queried date range as compared to a master list. I have set up this Google Sheet as an example.
I know and understand how to use the query function to select data by the dates:
=query(Datasheet!$A$2:$B,"Select B where A > date '"&TEXT('Querysheet'!$A$3,"yyyy-mm-dd")&"' and A <= date '"&TEXT('Querysheet'!$B$3,"yyyy-mm-dd")&"' ...")
The part that I am missing is how to compare and return a group of ID's that are missing from the Datasheet compared with the Mastersheet.
use:
=UNIQUE(FILTER(Mastersheet!A2:A, NOT(COUNTIF(UNIQUE(QUERY(Datasheet!A2:B,
"select B
where A > date '"&TEXT(Querysheet!A3, "yyyy-mm-dd")&"'
and A <= date '"&TEXT(Querysheet!B3, "yyyy-mm-dd")&"'")), Mastersheet!A2:A))))

Countifs N Count across multiple criteria (in data validations) but not returning zero if data validated cell is blank

Trying to set up a formula where I can pull N counts based off multiple criteria including a date range. A typical Countif formula could work in theory:
=countifs(D2:D, J2, F2:F, J3, E2:E, J4, G2:G, J5, C2:C, ">="&TODAY()-7, C2:C, "<="&TODAY())
However, the problem I am facing is I want to be able to still pull the N count even if I leave one or more data validated cells the formula is pulling off of blank. In order to pull anything, you have to select one of the 4 data validated options. As you select more, the data gets more granular. Currently if I leave one cell blank, I then get a zero on the return. Need that not to happen
Any thoughts on how I could make this work?
I have done this with queries before, but I am not sure how to pull N count if use query. Happy to go that route as well if its easiest
use:
=COUNTA(IFNA(QUERY(C:G,
"select C
where 1=1 "&
IF(J2="",," and D = '"&J2&"'")&
IF(J3="",," and F = '"&J3&"'")&
IF(J4="",," and E = '"&J4&"'")&
IF(J5="",," and G = '"&J5&"'")&
" and C <= date '"&TEXT(TODAY(), "yyyy-mm-dd")&"'
and C >= date '"&TEXT(TODAY()-7, "yyyy-mm-dd")&"'", 0)))

Moving Calendar Events around Google Sheets

I have a series of Google Calendars I am subscribed to. In my main Spreadsheet that I do most of my work out of, I have a launch page that I want to have the events pulled for today and tomorrow from each Calendar.
The data is pulled from Google Calendar and put into a hidden sheet called Under The Hood. I then use the following query to try and pull the ones that are A) relevant and B) Happening today (or tomorrow, two different queries).
Today's Events
=QUERY('Under The Hood'!M4:O13, "select M, N where M contains 'P/U' or M contains 'D/O' and toDate(O) contains 'toDate(TODAY())'",0)
Tomorrow's Events
=QUERY('Under The Hood'!M4:O13, "select M, N where M contains 'P/U' or M contains 'D/O' and O contains date'" & TEXT(TODAY()+1,"yyyy-mm-dd")&"'",0)
Both of these queries give me the same partial list of the events from tomorrow and the day after tomorrow.
your formula should be:
=QUERY('Under The Hood'!M4:O,
"select M,N
where M matches '.*P/U.*|.*D/O.*'
and O contains date '"&TEXT(TODAY()+1, "yyyy-mm-dd")&"'", 0)
Solution
Your logic statement right now is this:
M contains 'P/U' or (M contains 'D/O' and O contains date'" & TEXT(TODAY()+1,"yyyy-mm-dd")&"')
(Explicit parenthesis are inserted)
This means that it will return true by just checking that M contains 'P/U'.
You should formulate your logic statement in a way that will check:
M = 'P/U' and O = 'date you want' or M = 'D/O' and O = 'date you want'
To do this just wrap the logical statements on the M columns inside the parenthesis as recommended by the documentation:
You can join multiple conditions using the logical operators and, or, and not. Parentheses can be used to define explicit precedence.
=QUERY('Under The Hood'!M4:O13, "select M, N where (M contains 'P/U' or M contains 'D/O') and O contains date '"& TEXT(TODAY(), "yyyy-mm-dd")&"'",0)
Reference
Query Language

Sumifs formula with dynamic date range is not working in google sheet

Here O column is my sum range,
P2 = Date i.e. 2019-05-02
I want to sum all O column range on the bases of the last 30 days. i.e. p2-30.
This formula is working fine in Excel but not working in Google Sheets.
=SUMIFS(O:O,C:C,C2,P:P,"<"&TEXT(P2-30,"yyyy-mm-dd"))
I am really new to this and would appreciate any hints.....
You might want to give the QUERY function a try.
In
F1: =text(today()-30;"yyyy-mm-dd")
H1: =query(O1:P18;"Select sum(O) where P> date '"&F1&"'")
In case you don't want a header
H1: =query(O1:P18;"Select sum(O) where P> date '"&F1&"' label sum(O) ''")
QUERY is a very nice function. Using a date can be a bit tricky, look for examples at https://www.benlcollins.com/spreadsheets/query-dates/
=SUMPRODUCT(QUERY(O1:P, "select O
where P <= date '"&TEXT(C2, "yyyy-MM-dd")&"'
and P >= date '"&TEXT(C2-30, "yyyy-MM-dd")&"'", 0))

Resources