including a conditional statement in a =QUERY function - google-apps-script

How can I add a conditional statement (similar to an 'if elseif' in .php) to the query listed below so that it will return columns D and E from Sheet2 if column C in the responses sheet == 1st, or columns F and G from Sheet2 if column C in the responses sheet == 2nd? It must still only return column data from the appropriate row (based on the query).
=QUERY(Sheet2!$A$1:K; CONCATENATE("SELECT B, C WHERE A = ", responses!B1), 0)
I looked into IF(OR), but I don't believe that will work because final project will have more than two possibilities (there will actually be eight, 1st through 8th class period).
Here is a link to the Google Spreadsheet I'm working with
Any suggestions?
Thank you for your time and assistance,
Todd
High School Teacher

That cant be done with query. All rows must select the same columns.
Use a formula post-query (like an arrayformula)

Related

How to count unique values that are present in 2 different sheets only if they exist in both sheets

[Goal]
I want to be able to count unique values that are present in 2 different sheets only if they exist in both sheets.
[Details]
First, there are 2 sample data sheets (Data A, Data B) within the same Spreadsheet and it also has a sample dashboard to do the calculations. One thing to note about the data sheets is that they have different ranges, so they have different number of columns and rows. However, a couple things they have in common are the Month and ID columns.
Next, in the Dashboard sheet, there are 3 cells where B3 is the Month selector, C3 is to count the number of unique IDs that are included in both sheets (Data A & Data B) based on the month. With D3, I would like to count the number of unique IDs that are included in both sheets (Data A & Data B) where the the Month are the same AND Data B sheet's Cumulative column is 1.
[What I tried so far]
I tried using the COUNTUNIQUEIFS function and had the range as an array (by using the curly bracket) in the below way, however, it didn't work.
=COUNTUNIQUEIFS('Data A'!$B:$B,'Data A'!$A:$A,B3,'Data B'!$A:$A,B3)
I also tried without making the range argument an array.
=COUNTUNIQUEIFS('Data A'!$B:$B,'Data B'!$A:$A,B3)
Both attempts results in 1 for February 2023 when it should return 2. The weird thing is that January 2023 should return 3 and it correctly returns 3.
Hope someone can help me out with this. If there's a more elegant solution to achieve this by using Google Apps Scripts, I'd like to see hear about that as well.
You can try filtering both ranges for month, and the FILTER again by comparing them with MATCH and counting the remaining results:
=LAMBDA(ar,br,COUNTA(IFERROR(FILTER(ar,NOT(ISERROR (MATCH(ar,br,0))))))
(FILTER('Data A'!B:B,'Data )A'!A:A=B3),FILTER('Data B'!B:B,'Data B'!A:A=B3))
And just add an additional filter for the next column and the cumulative =1 'Data B'!C:C=1
=LAMBDA(ar,br,COUNTA(IFERROR(FILTER(ar,NOT(ISERROR (MATCH(ar,br,0)))))) (FILTER('Data A'!B:B,'Data )A'!A:A=B3),FILTER('Data B'!B:B,'Data B'!A:A=B3,'Data B'!C:C=1))

How to import data from a sheet where sheetname is dependent on a cell value

I have been trying to make a Google Sheet for some purpose which shall have details of various movies.
Here is the link: https://docs.google.com/spreadsheets/d/10-4BWaK-zmTxmqr2ov7AvjQW-wAFR8NKXkICuCK1dTY/edit?usp=sharing
Now I wish to count the number of movies per year where sheet name is dependent on cell value.
For example, in Sheet: "Data" I wish that B2 shows the count of movies in Sheet named '2019' but I don't want it to be written as '=COUNTA('2019'!A:A). Instead I wish that the sheet name automatically gets picked up from cell A2. So if I change the year in A2, the Count in B2 should change automatically.
I tried using INDIRECT and MATCH function as =COUNTA(INDIRECT(A2,false)) and =MATCH(A2,INDIRECT(A2),1) but I think I made some errors.
Also in the same way I wish to count in C2 but with a condition: to count only those cells from G column where value is watched
In B2 try
=sumproduct(INDIRECT(A2&"!A3:A")<>"")
and see if that works?

Google Sheets Filter with IN clause

Question: In Google Sheets, using only the built-in functions, how do I write a filter to exclude records based on a column in each row not being present in a list of valid values from another range.
Details
I am using Google Sheets to write a finance spreadsheet where all of my expenses and incomes are entered into a single sheet called Transactions. I have a separate sheet called Constants where I keep a list of income categories and a list of expense categories.
Here is some sample data for purposes of the question:
Constants
Transactions
I have a sheet called ByMonth where I want to show all of the expense transactions in one section and all of the income transactions in a separate section.
Goal:
I need to essentially do this sql query in google sheets functions:
select date, category, amount from transactions
where category not in (
select * from expense_categories
)
but I cannot figure out how to get Google Sheets to let me do an IN concept with their functions.
Here is a cell function expression that I am using for filtering the rows based on a date field:
=filter(Transactions!A2:C,
DATEVALUE(Transactions!A2:A) >= date(2015,4,1),
DATEVALUE(Transactions!A2:A) <= date(2015,4,30)
)
I would be adding a third condition to the filter() function, and that third condition would somehow compare the Category column of each row of data against the list of valid expense or income categories and return a boolean.
Here are some other things that I have tried, to no avail, including some variations of MATCH, ARRAYFORMULA, etc:
=filter(Transactions!A2:C, row(Transactions!B2:B) = UNIQUE(Constants!A2:A))
=filter(Transactions!A2:C, Transactions!B2:B = UNIQUE(Constants!A2:A))
=filter(Transactions!A2:C, Transactions!B2:B = Constants!A2:A)
Any suggestions?
The formula you are interested in is:
=filter(Journal!A2:C13,IFERROR(Match(Journal!B2:B13,Categories!A2:A3,0)))
The function match(a, b, 0) searches if a can be found in b. You need 0 at the end for exact match (default is closest match). If a cannot be found, you get an error, so you wrap the function with iferror to skip error.
Here is working example file
As each category use a keyword, expense or income, the following formulas return the desired results
=query(Journal!A2:C13,"Select * where B contains 'expense'")
=query(Journal!A2:C13,"Select * where B contains 'income'")

Delete rows on 2 criteria

I have copied over a spreadsheet from a form response to another sheet. The first row contains headers that are used by my pivot.
The rows from this destination sheet should be deleted unless:
Column c = today's date
AND column e = 'Show - Sale'
I have managed to get criteria workong on column e but not on column c (could be down to the way it's formatted??).
I also can't seem to stop it deleting the header row!
Cheers
One equal sign in JavaScript is an assignment operator. For conditional checks in JavaScript you must use either double equal signs or triple equal signs.

Google Spreadsheet Populating Cells in Other Sheet Based on Value

Am new to Google Docs, but have to create a cumulative report of comments that are flagged as positive or negative. I have 6 worksheets that ideally would populate to a single report, but I could create 6 individual reports for now.
In the source sheet, ColA is a numeric code identifying the category. Col B is the category description; Col C are the notes from one person; Col D is the code to identify it as positive or negative; Cols E and F are the notes from a 2nd person; G/H from a 3rd, etc.
The report sheet needs to transpose the vertical comments by category with the positive comments for all persons for the first category in Col G, the negative comments for the 1st category in Col H, etc for all 6 categories.
I was able to manually create this report using the following formula to extract the Positive comments from column C:
QUERY(EntrySheet1!C5:D15;"select * where D='P'")
But, it's pretty tedious to copy the formula laterally and vertically to accommodate all 6 categories and all 6 note takers.
So, my questions are whether or not there is an easier way to extract the information the way I need to report it. Also, is there a way to use something like Excel's Indirect function where I could use the concatenate function to build the formulas and the Indirect to evaluate that function. My thought here is that I could have an entry cell where I would identify which cumulative report I wanted to view by simply updating the cell. An alternative would be to load the data into an array and use a script to populate a static cumulative report. Real-time updating with formulas would be ideal, but creating a static report that is created from a script is acceptable. My biggest concern is the manual effort to update the formulas since they are sheet specific.
Use Google Spreadsheet INDIRECT function.
See the Google spreadsheets function list:
INDIRECT(reference)
Returns the reference specified by a text string. This function can also be used to
return the area of a corresponding string. Reference is a reference to a cell or an
area (in text form) for which to return the contents.
You might be able to feed the results of indirect into your query.