Google Sheets Two Lists as Criteria for Query - google-sheets

I have the following query which is working fine. However I'd like to adde some additional logic if possible where if the there is a value in K3 then ideally the query would not use WHERE R = '"&$K2&"' in the query below but rather use WHERE D = '"&$K3&"'.
Here is a sample sheet. https://docs.google.com/spreadsheets/d/1Lt0RzwtuqIrxWYmWnYK4nh0JN2WdM0icpYF9oXGRfB8/edit?usp=sharing
QUERY(Transactions,"SELECT C,D,B,E WHERE R = '"&$K2&"' AND B > date '"&TEXT($C$2,"yyyy-mm-dd")&"' AND B <= date '"&TEXT($C$3,"yyyy-mm-dd")&"'
",1)

Try
=QUERY(Transactions,"SELECT C,D,B,E WHERE "&if(K3="",L2,L3)&" = '"&if(K3="",K2,K3)&"' AND B > date '"&TEXT($C$2,"yyyy-mm-dd")&"' AND B <= date '"&TEXT($C$3,"yyyy-mm-dd")&"' ",1)
with R and D in L2/L3

Related

Google sheet text formula not working; query by date and text() not working

[![enter image description here][1]][1]
So I am trying to pull a query by date, but when I do exactly like the tutorials I found on the internet it just doesn't work,
i.e. :
=QUERY(Penjualan!$A$1:$H$136,"select A , B , D , H where B > date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'",1)
or
=QUERY(Penjualan!A1:H; "select A , B , D , H where B = date '"&text(D1, "yyyy-mm-dd")&"'" )
does anyone know what is wrong?
try like this:
=TEXT(TODAY(); "yyyy-mm-dd")
=QUERY(Penjualan!A1:H;
"select A,B,D,H where B = date '"&TEXT(D1; "yyyy-mm-dd")&"'")
=QUERY(Penjualan!A1:H;
"select A,B,D,H where B > date '"&TEXT(TODAY(); "yyyy-mm-dd")&"'"; 1)

Pass a Date value from a cell in Google Sheet formula

How do I re write this query for Google sheet, so that the dates in column B can be taken from a cell say X1 and X2
=QUERY(master!A:N,"select B,D,H where B>=date '2021-10-01' and B<=date '2021-10-31' and (E='3-MonoYarnSupply' or E='4-MultiYarnSupply') label B 'Date',D 'Particulars',H 'Quantity Supplied'")
Expected formula
=QUERY(master!A:N,"select B,D,H where B>X1 and B<=X2 and (E='3-MonoYarnSupply' or E='4-MultiYarnSupply') label B 'Date',D 'Particulars',H 'Quantity Supplied'")
Where X1 = 01-10-2021 and X2 = 31-10-2021
You can split the formula and use string concatenation logic to refer to other cells like this:
=QUERY(A:N,"select B,D,H where B>=date '" & TEXT(X1, "YYY-MM-DD") & "' and B<=date '" & TEXT(X2, "YYY-MM-DD") & "' and (E='3-MonoYarnSupply' or E='4-MultiYarnSupply') label B 'Date',D 'Particulars',H 'Quantity Supplied'")
B >= date '"&TEXT(X1,"yyyy-mm-dd")&"' and B <= date '"&TEXT(X3,"yyyy-mm-dd")&"'
Thank you #Gangula. Qualifying the date values as above in the query helped to achieve what I required. Thank you for your time.

Google Sheets: Count number of rows in a column that do not match corresponding row in another column?

Say we have the following spreadsheet in google sheets:
a a
b b
c
d e
e d
How would I build a formula that counts the number of rows in column B that do not match the corresponding row in column A, and are not blank? In other words I want to get the number of rows that changed to a new letter in column B. So in this example the formula should return 2.
Thank you for your help.
UPDATE:
Now suppose I have this spreadsheet:
a a
b b b
c a
d e e
e d e
How would I build on the last formula for the third column, where the value returned is:
(the number of rows in column 3 that don't match the corresponding row in column 2) + (if column 2 is blank, the number of rows in column 3 that do not match the corresponding row in column 1)
and I also don't want to count blanks in the third column.
The value returned in this case should be 2 (rows 3 and 5).
To me it sounds like you could use:
=SUMPRODUCT((B:B<>"")*(B:B<>A:A))
=IFNA(ROWS(FILTER(A:B,
(A:A<>B:B)*
(B:B<>"")
)),0)
FILTER by matching conditions * for AND + for OR.
ROWS counts rows
IFNA returns 0 if nothing was found.
or with QUERY
=INDEX(QUERY(A:B,"select count(B) where B<>A"),2)
Try this:
=ARRAYFORMULA(COUNTA($B$1:$B)-SUM(COUNTIFS($A$1:$A, $B$1:$B,$B$1:$B,"<>")))
I see 2 ways to complete this.
First you could add a function to each row to return 1 or 0 if the value changed and was not blank and then sum results. This unfortunately adds a messy column in your spreadsheet.
=if(A1<>IF(ISBLANK(B1),A1,B1),1,0)
Second you could create a function where you would pass the range as a string.
The call from the worksheet would look like this:
=myFunction("A1:B5")
Then create a script by opening Tools -> Script editor and use something like this
function myFunction(r) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(r);
var numRows = range.getNumRows();
var areDifferent = 0;
for (let i=1; i<= numRows; i++) {
let currentValue = range.getCell(i,1).getValue();
let cmpValue = range.getCell(i,2).getValue();
if ((currentValue != cmpValue) && (cmpValue != "")) {
areDifferent++;
}
}
return areDifferent;
}

Creating a query that will filter based on multiple criteria from dropdown menus in Google Sheets

I am trying to create a dashboard that will filter data based on multiple criteria.
Here is a copy of the spreadsheet: https://docs.google.com/spreadsheets/d/1sJcsu0VpnLBi7wh4l-NEFdYfkxWgC6vpX8AeN9u-KlQ/edit?usp=sharing
I am having a hard time getting the query to work as I want.
I have created data validation cells for several search criteria (Name, reporter, incident type, location, start date, end date).
There are four columns for student names, based on the way the form is set up. I am able to get it to filter based on the student name using this formula: =QUERY('Form Data'!$A$2:M,"Select * where B = '"&E1&"' OR C = '"&E1&"' OR D = '"&E1&"' OR E = '"&E1&"'",0)
When I try to add other criteria, the formula breaks down. I've tried:
Select * where B = '"&E1&"' OR C = '"&E1&"' OR D = '"&E1&"' OR E = '"&E1&"' AND D >= Date '"&B11&"' AND A <= Date '"&B12&"' to try and filter date, but I'm getting an error. It says the date format isn't correct, but it's formatted yyyy/MM/dd as it says it should be.
I also tried: =QUERY('Form Data'!$A$2:M,"Select * where B = '"&E1&"' OR C = '"&E1&"' OR D = '"&E1&"' OR E = '"&E1&"'" &if(B5="All",,"'"&B5&"'"),0); this works when "All" is selected, but not when I pick another name from the drop down list.
Any help would be enormously appreciated
try:
=QUERY('Form Data'!A2:M,
"where (B = '"&E1&"'
or C = '"&E1&"'
or D = '"&E1&"'
or E = '"&E1&"')
and A >= date '"&TEXT(B11, "yyyy-mm-dd")&"'
and A <= date '"&TEXT(B12, "yyyy-mm-dd")&"'", 0)

Select 'x' returns "x"()

I want to return 3 columns, A(description), 'D'(hard coded value of D), Q(date)
=query('Detailed Plan'!$A$2:$Q, "select A,'D',Q where D = date)
It returns the following results. Rows 2 and greater are exactly what I want and would be perfect if I didn't get the first row. How do I get a hard coded value into a column without "D"() showing up in the first row?
blank, "D"(), blank
Description, D, date
Description, D, date
Description, D, date
Thanks so much for any help that is provided.
You can use 'label' in the query string.
=query('Detailed Plan'!$A$2:$Q, "select A, D, Q where D = date label A 'Description', D 'Some value', Q 'Date' ", 0)
EDIT: if you don't need headers at all, try
=query('Detailed Plan'!$A$2:$Q, "select A, D, Q where D = date ", 0)

Resources