Variable Cell Value Format In Query Function - google-sheets

I know "Format" can be used in a Query function (i.e Query(A1:G2,"Select * Format C 'MM/DD/YYY'")) but I'm stuck on how to use it in my more complicated query. Essentially I want all the selected Columns to be formatted to dates "MM/DD/YYYY", but I can't do it in the cell itself because this query is part of a variable query function that pulls data based on a user input, and only THESE column pulls are in date formats, all the other pulls are plain numbers or text. I tried putting the Format in myself but it never works or the output gives me the IfError False readout of "No Matches Found". Can anyone assist?
IFERROR(QUERY({Training!A3:AP},"select Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25,Col26,Col27,Col28,Col29,Col30,Col31,Col32,Col33,Col34,Col35,Col36,Col37,Col38,Col39,Col40,Col41,Col42 where "&TEXTJOIN(" and ", 1, IF(Lower(B3)<>"", "Lower(Col1) contains '"&Lower(B3)&"'", ),IF(Lower(B4)<>"", "Lower(Col2) contains '"&Lower(B4)&"'", ),If(Lower(B5)<>"", "Lower(Col6) contains '"&Lower(B5)&"'", ),IF(Lower(B7)<>"", "Lower(Col9) contains '"&Lower(B7)&"'", ),If(B6<>"",Vlookup(B6,Classes!G2:H,2,False)&" contains '"&B6&"'",),), 0), "No Matches Found")))

format in QUERY is buggy. use this:
=ARRAYFORMULA(IF(B2="", "Please Select Criteria",
IF(B2="Licenses", IFERROR(QUERY({Licensing!A3:D,
IF(Licensing!E3:E="",,TEXT(Licensing!E3:E, "dd/mm/yyyy")), Licensing!F3:F,
IF(Licensing!G3:G="",,TEXT(Licensing!G3:G, "dd/mm/yyyy")), Licensing!H3:H,
IF((Licensing!I3:AQ<>"")*(MOD(COLUMN(I3:AQ)+1, 4)=0),
TEXT(Licensing!I3:AQ, "dd/mm/yyyy"), Licensing!I3:AQ)},
"select "&JOIN(",", "Col"&ROW(3:7), "Col"&FILTER(ROW(9:43), NOT(MOD(ROW(9:43), 4)=0)))&"
where "&TEXTJOIN(" and ", 1, "1=1",
IF(LOWER(B3)="",,"lower(Col1) contains '"&LOWER(B3)&"'"),
IF(LOWER(B4)="",,"lower(Col2) contains '"&LOWER(B4)&"'"),
IF(LOWER(B5)="",,"lower(Col6) contains '"&LOWER(B5)&"'"),
IF(LOWER(B7)="",,"lower(Col10) contains '"&LOWER(B7)&"'"),
IF(B6="",,"Col"&MATCH(B6, Licensing!2:2, 0)&" is not null")), 0), "No Matches Found"),
IFERROR(QUERY({Training!A3:D,
IF(Training!E3:E="",, TEXT(Training!E3:E, "dd/mm/yyyy")), Training!F3:F,
IF(Training!G3:AP="",,TEXT(Training!G3:AP, "dd/mm/yyyy"))},
"select "&JOIN(",", "Col"&ROW(3:42))&"
where "&TEXTJOIN(" and ", 1, "1=1",
IF(LOWER(B3)="",,"lower(Col1) contains '"&LOWER(B3)&"'"),
IF(LOWER(B4)="",,"lower(Col2) contains '"&LOWER(B4)&"'"),
IF(LOWER(B5)="",,"lower(Col6) contains '"&LOWER(B5)&"'"),
IF(B6="",,"Col"&MATCH(B6, Training!2:2, 0)&" is not null")), 0), "No Matches Found"))))

This may not help, but I believe the issue is that the FORMAT clause in the QUERY function does not override the cell format in the output area. I did some testing, and found that with the following simplified version of your formula, I could get the format of the output to change, for example from 'mm/dd/yyyy' to 'yyyy-mm-dd'.
"select Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11
where "&TEXTJOIN(" and ", 1,
IF(Lower(B3)<>"", "Lower(Col1) contains '"&Lower(B3)&"'", ),
IF(Lower(B4)<>"", "Lower(Col2) contains '"&Lower(B4)&"'", ),
If(Lower(B5)<>"", "Lower(Col6) contains '"&Lower(B5)&"'", ),
IF(Lower(B7)<>"", "Lower(Col9) contains '"&Lower(B7)&"'", ),
If(B6<>"",Vlookup(B6,Classes!G2:H,2,False)&" contains '"&B6&"'",),
) & " Format Col9 'dd-yyyy-mm', Col11 'dd-yyyy-mm' ", 0)
But this only worked if the cell/column was formatted as Automatic. And worse, I can't reliably reproduce the behaviour yet. On another copy of the sheet, it won't change the format.
Anyway, if you want to see it working, here is a link to the cell in my copy of your sheet.
Maybe someone else will have a clearer explanation of the issue and how to address it.

Related

Google Sheet Query outputting value into wrong row

I am trying to consolidate some data and running into a bizarre issue. I am using a query function that is working perfectly in some instances but not in other. When referencing a particular sheet, it keeps outputting the values in the row beneath, which is causing aa #REF error.
Here is a copy of the Google Sheet.
Here is the formula I'm using (which again, works perfectly sometimes, but not when pulling data from certain sheets)
=QUERY(INDIRECT($B125&"!$A$2:$1000"), "SELECT E WHERE A = date '"& text($C125, "yyyy-mm-dd") &"' and B contains '"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($E125, " " , ""),"AM", ""), "PM", "")&"' ")
You need to specify the number of headers otherwise it will try to guess which eventually results in rows being magically merged into the header causing all sorts of unpredictable behavior:
=QUERY(INDIRECT($B125&"!$A$2:$1000"), "SELECT E WHERE A = date '"& text($C125, "yyyy-mm-dd") &"' and B contains '"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($E125, " " , ""),"AM", ""), "PM", "")&"' ",1)
All I did was add a ,1 just before the closing parenthesis so that it specifies always 1 row of headers. The format of your data is really messed up and you need to think about cleaning up your data.

How to return multiple results in the same cell using Index, Match and Search

I have a working Index, Match, Search formula that searches the titles of my products for specific terms that I've determined in a Key, if no result is found then alternatively it searches my product SKU's using a different key.
Currently, the formula is only giving me the first result that it finds within my key but I would like it to return as many results as possible all separated by Pipes or commas.
Here's the working formula that is giving me 1 result.
=IF((ARRAYFORMULA(INDEX('Array Key'!$J:$J,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$I:$I,$B2)),0))))="",(ARRAYFORMULA(INDEX('Array Key'!$L:$L,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$K:$K,$A2)),0)))),(ARRAYFORMULA(INDEX('Array Key'!$J:$J,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$I:$I,$B2)),0)))))
This would be awesome if I could get it working. Here's a link to a copy of my spreadsheet if anyone would like to see it working. Feel free to make any edits.
https://docs.google.com/spreadsheets/d/1APLCVnqmP51UbimyZeY-S0BoSEuFWkPa2LtAxVX9H0E/edit?usp=sharing
I've tried to use the following formula but it doesn't quite work how I'd like it to.
=IF((ARRAYFORMULA(INDEX('Array Key'!$F:$F,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$E:$E,$B2)),0))))="",(ARRAYFORMULA(INDEX('Array Key'!$H:$H,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$G:$G,$A2)),0)))),(ARRAYFORMULA(INDEX('Array Key'!$F:$F,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$E:$E,$B2)),0)))))&" | "&(ARRAYFORMULA(INDEX('Array Key'!$H:$H,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!$G:$G,$A2)),0))))
This formula searches the 2nd key again and concatenates any results. However, I'm getting duplicates and it still only returns the first result found.
if this would be like:
then D2 cell would be:
=ARRAYFORMULA(TEXTJOIN(" | ", 1, UNIQUE({
IFERROR(VLOOKUP(TRANSPOSE(IFERROR(REGEXEXTRACT(SPLIT(B2, " "),
TEXTJOIN("|", 1, 'Array Key'!E$3:E)))), 'Array Key'!E$3:F, 2, 0));
IFERROR(VLOOKUP(TRANSPOSE(IFERROR(REGEXEXTRACT(SPLIT(A2, " "),
TEXTJOIN("|", 1, 'Array Key'!G$3:G)))), 'Array Key'!G$3:H, 2, 0))})))

Google Sheets: Joining multiple cell values from source

I'm trying to get a formula that pulls a value from a sheet based on a date to find all occurrences and join them together.
My original question is here for context.
This works really well, but if a user submits a new order at a later date for a date that already has a submission, I get the first and not any additional.
Here's my current code, which also checks for comma and space, and adds a carriage return.
=regexreplace(IFERROR(VLOOKUP(B1, IMPORTRANGE("source", "B2:C"), 2, 0)), ", ", char(10))
How can I combine multiple cell values based on criteria?
=ARRAYFORMULA(TEXTJOIN(CHAR(10),1,
REGEXREPLACE(IFERROR(QUERY(IMPORTRANGE(
"1JCOLm7ZQgR0qgtzq_98E4vDie7-FM-WlwiSZwgpB22k", "B2:C"),
"select Col2 where Col1=date'"&TEXT(B1,"yyyy-mm-dd")&"'")),
", ", CHAR(10))))

Filtering and comparing data in Google Sheets

So what I want to do is remove the first word of every name in these columns. Example: "CMSgt Cin" needs to say "Cin" on my datasheet1. I have attempted to do that but am I getting errors.
After I have converted the names, I want the cells that do not apply to the conversion to remain blank on my datasheet1. So basically if the filter is looking for a name such as "CMSgt Cin" but only finds "--" in that cell, I want the filter just make the cell blank on my datasheet1.
My main goal is to see if the names on my reference sheet are also on another sheet which they should be on. So I am filtering and comparing two lists of data, and I want to make it so if they are on my reference sheet but notice sheet I'm comparing it to, their name should appear red. I am willing to talk on discord if this is too confusing to understand
so what I want to do is remove the first word of every name in these columns
=INDEX(SPLIT(A1, " ", 0, 1), 1, 2)
I want the cells that do not apply to the conversion to remain blank on my datasheet1
=IF(ISNUMBER(SEARCH(" ", A1)), INDEX(SPLIT(A1, " ", 0, 1), 1, 2), )
goal is to see if the names on my reference sheet are also on another sheet which they should be on
=ARRAYFORMULA(VLOOKUP(A1:A, 'another sheet'!A1:A, 1, 0))
if they are on my reference sheet but notice sheet I'm comparing it to, their name should appear red
=COUNTIFS($A:$A, $A1, INDIRECT("'another sheet'!$A:$A"), INDIRECT("'another sheet'!$A1"))>1
show all the people who are on my reference sheet but not my data sheet
=QUERY(FILTER({E:E;F:F;G:G;H:H;I:I;J:J},
ISNA(MATCH({E:E;F:F;G:G;H:H;I:I;J:J}, Sheet3!A:A, 0))),
"select Col1 where Col1 is not null and Col1 <>'720th STG Flights'")

How to maintain the original header name when performing Google Sheets Query

Hi I have this working query that loops through an arbitrary range of data and produces the results I need:
=arrayformula(QUERY(Crew!A:DY,"SELECT E,A," & join(",", substitute("Count(`1`)", "1",substitute(address(1, column(Crew!F:DY), 4), "1", ""))) & "GROUP BY E,A", 1))
Unfortunately, this produces Columns that are labeled so:
count 18/12/2017 count 25/12/2017 count 01/01/2018
I need to force a display of the original column name, and if possible, the format, e.g. 18/12/2017 as this will allow me to perform further pivot or group by month type functions
I have experimented with different methods of adding a label at the end of the query, reduced the query to tests of the data without using the arrayformula, and searched through the queryLanguage Docs but all references seem to be related to applying a different text string rather than leaving the column header 'raw'
I suspect the main problem is my inexperience, I don't know the correct terms to search for?
How do I achieve this?
Cheers.
You need to use label construction:
"select ... where ... group by ... label ..."
Reference:
https://developers.google.com/chart/interactive/docs/querylanguage#label
You may get labels text with the formula:
="label "&ArrayFormula(join(", ",substitute("count(`1`) ", "1",substitute(address(1, column(F1:G1), 4), "1", ""))&text(F1:G1," 'dd/mm/yyy'")))
change F1:G1 to your range.
The shorter version of the formula with Regex:
="label "&ArrayFormula(join(", ",REGEXREPLACE(ADDRESS(1,COLUMN(F1:G1),4),"(.*)\d+$","count(`$1`) "&TEXT(F1:G1,"'dd/mm/yyy'"))))

Resources