google sheet script filter and query based on checkbox and date - google-apps-script

I have 2 sheets.
1 sheet with 3 columns: 1 column with dates, 1 column with data and one with checkboxes
The other sheet I want to apply the query on
If a checkbox is checked that copy a row with specific column based on the date of 2 cells on the target sheet. (one cell contains month (januari, second cell contains year 2023)
The query part works:
=QUERY(Data!A1:D, "select B,A,C where D=TRUE")
But then howto integrate the date filter. I need some guidance, sometimes I cant get my head around it.
=QUERY(filter(Data!A1:D100, "select B,A,C,D where D=TRUE",TEXT(Data!A1:A100,"yyyMMMM") = $D$1 & $B$1))
Latest code I came up with, but does not work.
Suggestions are welcome

Use filter() with two criteria, like this:
=filter(
{ Data!B1:B, Data!A1:A, Data!C1:C },
Data!D1:D,
text(Data!A1:A, "MMMyyyy") = left(B1, 3) & D1
)
...where column Data!A1:A contains numeric date values, cell B1 contains a month name as a text string and D1 contains a year as a number.

Related

Unable to take sum of qty of part number form another sheet

=INDEX(QUERY(Tracker!A:G,"select sum(G) where B=' & Summary!$A$3 & '",1),1,0)
I have two sheets in a spreadsheet. My requirement is to get sum of qty of part number form sheet tracker to summary. Condition is (B column in Tracker sheet is equal to A3 in summary sheet).
Note : Formula is in Summary Sheet
try:
=QUERY(Tracker!A:G, "select sum(G) where B = '"&Summary!$A$3&"' label sum(G)'' ",1)

using "like" with cell to reference the last four digits in google sheets

hardCode formula
dynamic formula
I am using google sheets to make a report related to customers projects,
in my company When we have new project we give the project a 4 digits number then we create account receivable for the customer contain of 14 digits like that :
the first 6 digits is fixed
the second 4 digits represent the year and month
the last 4 digits represent the project number
so i have column with the Project number and i want to make a query that return the count of costumer payment in front of the project number , when i try to use this formula :
=QUERY({'2016DB'!$A$2:$H$18168;'2018DB'!$A$2:$H$13379;'2017DB'!$A$2:$H$17759;'2019DB'!$A$2:$H},"select
count(Col3) where Col1 = 12040118091492 And Col3 > 0 label count(Col3)
'' ",0)
it's work fine
But when i try to make it dynamic by using like statement :
QUERY({'2016DB'!$A$2:$H$18168;'2018DB'!$A$2:$H$13379;'2017DB'!$A$2:$H$17759;'2019DB'!$A$2:$H},"select
count(Col3) where Col1 like 120401____"""&E7&""" And Col3 > 0 label
count(Col3) '' ",0)
Where E7 Cell Contain the Project number it doesn't work !!
Made a simple example with your type of query. Check this, please: https://docs.google.com/spreadsheets/d/1ISG0AjMNhwnXQvlxDJCMHcyx8mDKiCv6zqOfAGxrrDg/edit?usp=sharing (screenshot: https://i.imgur.com/jHppqzr.png)
Main formula is: =QUERY(A1:A5, "select A where A like 'a__"&D1&"'") where D1 is your needed project number. Looked up values are in format: letter a followed by 2 any characters and then project number.
Mind double and single quotes.

COUNTIFS() for non-contiguous range defined as a NAME

I have a spreadsheet that shows the name of attendees to meetings on one tab (e.g. Meeting 1 are columns E:G, Meeting 2 are columns H:J, etc...). Each meeting has the name of the person invited, their team name and then a Yes/No for attendance. The cells with names are assigned a defined name "MeetingAttendees" - and thus are non-contiguous. On a second tab (first tab is labled "POC") I have the unique list of every possible invitee across all of the columns on the other tab (labled "Team Members"). I am trying to create a count to see how many meetings that person has actually attended (not just invited).
MeetingAttendees = "=POCs!$E$10:$E$30,POCs!$H$10:$H$37,POCs!$K$10:$K$42"
AttendeeDisposition = "=POCs!$G$10:$G$30,POCs!$J$10:$J$37,POCs!$M$10:$M$42"
Attempting this formula which doesn't seem to be working (I get "#VALUE!")
=countifs("MeetingAttendees",A2,"AttendeeDisposition","yes")
Joe Smith is a value in A2 and is one of the cells in the MeetingAttendees named range.
Please help.

Google Spreadsheet function Query

I am confused.
I have a spreadsheet in google with 2 tables.
I want to select data from a sheet named "Presseverteiler" where a value in one field equals the value that I enter in a second sheet named "Veröffentlichungen". The formula is inside the sheet "Veröffentlichungen".
My code is this
=IF(not(isblank(D2)) ; QUERY(Presseverteiler!$A$2:AZ;"SELECT F WHERE A="&D2&" LIMIT 1"); "")
D2 is a number in "Veröffentlichungen" that equals a number in "Presseverteiler" in column A.
If the numbers in column A are not sorted from A to Z (1 to 510 in my case), the query yields the result I want.
If the numbers in column A are in order though, the query gives the result I want plus the entry of line 2 which it puts ahead of the result I want.
If I want entry 223 it would give 2 and then 223. To make it more weird: I have many other columns. Depending on if I sort those by A-Z or Z-A some of them give me 1 result in the query, some give me 2 results. Some give me 1 result both ways.
For example if I sort "title" A-Z (contains Mr, Mrs, Dr.) I get 2 results, if I sort the same column Z-A I get one result.
If I order the column "publication" A-Z or Z-A I get one result.
Despite all that, why does the LIMIT 1 not work here? Even with that I get a #REF! error that the results can not be displayed because they would overwrite successive fields in the table.

Using column names in IMPORTRANGE QUERY

I have used a script that allows me to substitute column names for Col# format in QUERY of a sheet in the same Google workbook but when I call the QUERY from a different Google Sheets spreadsheet with IMPORTRANGE, I get this error:
Range not found (line 11, file "SQLfunction")
My calling cell looks like this:
=QUERY(
IMPORTRANGE("long-hairy-key","MASTER!A:BN"),
IMPORTRANGE("long-hairy-key",SQL("MASTER!A:BN",STATS!D33,true)),
1
)
STATS!D33 is this:
select TRACT,PARID,LINE,OWN1,T_STATUS,ExhibitDate,FEET2,NewEase,TWSacres,ExistEase,PrevDistTWS,ATWSacres,ARdistance,StagingArea,Damages,UpdateReason,ACQ_STATUS where Col51 <>'' ORDER BY LINE ASC, TRACT ASC Label OWN1 'Landowner'
and the SQL function is from here.
Can someone recommend how I might adjust the script to accommodate IMPORTRANGE?
The referred SQL() custom function is not able to access an external spreadsheet. You have to adapt the SQL() custom function to make it able to access an external spreadsheet or to redesign your formula.
If you go for redesigning the formula, one approach is to move the import of the external data out of the "main formula". To do this,
First, get the external range. Add a new sheet (assume that it's name is Sheet1) and add the following formula to A1 =IMPORTRANGE("long-hairy-key","MASTER!A:BN")
Second, replace the "main formula" by other like the next one:
=QUERY(
Shee1!A:BN,
SQL("Sheet1!A:BN",STATS!D33,true)),
1
)
You may generate sql statement with formulas.
Step1. Use importrange to get headers only.
Paste this formula in Sheet1 A1:
=transpose(IMPORTRANGE("long-hairy-key","MASTER!A1:BN1"))
Need to transpose the data to make vertical list of field names.
Step2. Add column numbers.
Paste this formula in B1:
=ARRAYFORMULA(ROW(OFFSET(A1,,,counta(A:A))))
In this step you will get table, which looks like this:
A B
1 FieldName1 1
2 FieldName2 2
3 FieldName3 3
4 FieldName4 4
5 FieldName5 5
Step3. Combine the SQL statement.
Use column C to indicate, which columns are to be selected in your query:
A B C
1 FieldName1 1 1
2 FieldName2 2
3 FieldName3 3 1
4 FieldName4 4
5 FieldName5 5 1
And use this formula in free cell (F1):
="select Col"&JOIN(", Col",FILTER(B:B,C:C=1))
In my case, this formula gives string select Col1, Col3, Col5, and this can be used in query formula:
=query(IMPORTRANGE("long-hairy-key","MASTER!A:BN"), F1)
Use additional columns to make your SQL smarter, add where clause, order by, label and so on.
Update
So this step-by-step solution doesn't solve the problem with rearranged columns.
The trick is to use column names list and save it as values:
paste the formula from step1: =transpose(IMPORTRANGE("long-hairy-key","MASTER!A1:BN1"))
copy it → go to paste spacial → values
Then you need to go through steps 1 and 2 in order to make the list of current firld numbers.
And then you'll have actually two tables. The first one will hold actual field list and field numbers, generated by formulas from step1 and step2. Here's Table1:
A B
1 FieldName1 1
2 FieldName2 2
3 FieldName3 3
4 FieldName4 4
5 FieldName5 5
And the second table will have SQL settings. Here's Table2::
A B C
1 FieldName1 1 1
2 FieldName3 3
3 FieldName2 2 1
4 FieldName4 4
5 FieldName5 5 1
Field names in it must be entered as values. Column B will contain vlookup function:
= vlookup(A1, table1!A:B, 2, 0)
or even ArrayFormula:
= arrayformula(vlookup(offset(A1,,,counta(A1:A)), table1!A:B, 2, 0))
That's all you need to start. Then just make your query smarter.