Get all Rows from QUERY() into one cell in a Google Spreadsheet - google-sheets

I have the following formula in a spreadsheet:
=IFERROR( JOIN( CHAR(10), QUERY(Schedule!$A$2:$E, "SELECT C, D, E Where A = '" & B$2 & "' AND B = timeofday '" & text($A4, "HH:MM:SS") & "' AND C = '" & $C$1 & "'", -1)),)
It works great when it returns one row. It does not work if it returns multiple rows. How can I get it to use all rows returned with a CHAR(10) included between rows as well as columns. Basically I want all the returned data separated by line breaks. So when the QUERY returns
Item1 Item2 Item3
Item1A Item2A Item3A
it is changed to
Item1
Item2
Item3
Item1A
Item2A
Item3A
My goal is to put all the returned data in 1 cell with a line break between each item. Any way I can make this happen?
Here is a copy of the file. Cells B3 to F4 are good sample cells where the formula reside.

The solution is imperfect, it adds extra spaces before each line:
Item1 Item2 Item3
Item1A Item2A Item3A
^ spaces
Step1
Get origilal formula + extra column in query:
=QUERY(data, "SELECT C, D, E, '##' Where" & ... & "' label '##' ''", -1)
The result:
Item1 Item2 Item3 ##
Item1A Item2A Item3A ##
Step2
Replace "##" with char(10)
=ARRAYFORMULA(SUBSTITUTE(step1Formula,"##",char(10)))
Item1 Item2 Item3
Item1A Item2A Item3A
^ new lines
Step3
Use concatenate:
=ArrayFormula(" "&TRIM(CONCATENATE(" "&B16:E17)))
Final formula
=ArrayFormula(" "&TRIM(CONCATENATE(" "&SUBSTITUTE(QUERY(Schedule!$A$2:$E, "SELECT C, D, E, '##' Where A = '" & B$2 & "' AND B = timeofday '" & text($A3, "HH:MM:SS") & IF(OR($C$1 = "*",ISBLANK($C$1)), "", "' AND C = '" & $C$1) & IF(OR($F$1 = "*",ISBLANK($F$1)), "", "' AND E = '" & $F$1) & "' label '##' ''", -1),"##",char(10)))))
Shared file:
https://docs.google.com/spreadsheets/d/1otcqvkXb5H3WTSi_exKw9UUcRqN8MKBZ5JYCmTLAWdQ/edit?usp=sharing

I know this is not an elegant solution, but it will help you in your case. Enter this formula at B3 and then copy and paste it onto other cells.
=iferror(join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 0")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 1")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 2")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 3")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 4")),"")
Basically you cannot concat result strings with google query(), so instead I'd fetch one row at a time using limit 1 offset 0, limit 1 offset 1, limit 1 offset 2, and so on. Maximum is 5 in this case. Sample sheet is here.

Related

Index and Concat / Concatenate from another sheet

I have a data sheet which i want to index pull into another sheet and also concanate or concat 2 or more columns with array formula.
sheet link here
https://docs.google.com/spreadsheets/d/1GUI-Gl7HDNGg5V2wM4ossUn46cp7VHRBZ_t4la2gDH0/edit?usp=sharing
data sheet https://docs.google.com/spreadsheets/d/1GUI-Gl7HDNGg5V2wM4ossUn46cp7VHRBZ_t4la2gDH0/edit#gid=0
result required sample sheet https://docs.google.com/spreadsheets/d/1GUI-Gl7HDNGg5V2wM4ossUn46cp7VHRBZ_t4la2gDH0/edit#gid=1226812843
you can see in result sheet column B and F which are merged from "data sheet column b c d and h i simultaneously"
Use an { array expression }. Insert > Sheet and put this formula in cell A1 of the new sheet:
=arrayformula(
{
DATA!A1:A,
trim(
DATA!B1:B & " " &
if( iserror(search(DATA!C1:C, DATA!B1:B)), DATA!C1:C, "" ) & " " &
if( iserror(search(DATA!D1:D, DATA!B1:B)), DATA!D1:D, "" )
),
DATA!E1:G,
trim(DATA!H1:H & if(len(DATA!I1:I), " / " & DATA!I1:I, "")),
DATA!J1:U
}
)
The formula will only append the values in columns C and D to the value in column B when they do not not already appear in the value in column B.
the prior answer is ok but did you know...
so your issue can be solved as:
=BYROW(DATA!A2:INDEX(DATA!A:A, COUNTA(DATA!A:A)),
LAMBDA(a, {a, TEXTJOIN(" ", 1, OFFSET(a,,1,,3)), OFFSET(a,,4,,3),
TEXTJOIN(" / ", 1, OFFSET(a,,7,,2)), OFFSET(a,,9,,11)}))

Query with multiple cell reference for multiple conditions with a select all option in Google Sheets

I have a data sheet 'All Data'!A2:AX. I am working on a dashboard where I want to filter the data using cell references by multiple conditions like gender, Nature of disability, Age less and greater than, and educational qualification. I have been able to achieve that through the below formula
These are the way I have tried to use drop downs used in the formula
= query(importrange("URL","'All Data'!A3:AX"),
"Select Col2, Count(Col24)
Where Col3 = '"&'WMR FY'!$B$3&"'
AND
(Col14 = '" &'WMR FY'!T3&"'
OR Col14 = '" &'WMR FY'!U3&"'
OR Col14 = '" &'WMR FY'!V3&"'
OR Col14 = '')
AND
(Col11 = '" &'WMR FY'!T2&"'
OR Col11 = '" &'WMR FY'!U2&"'
OR Col11 = '" &'WMR FY'!V2&"'
OR Col11 = '" &'WMR FY'!W2&"'
OR Col11 = '" &'WMR FY'!X2&"'
OR Col11 = '" &'WMR FY'!Y2&"'
OR Col11 = '')
AND Col24 = '"&'WMR FY'!U4&"'
AND Col12 >= "&'WMR FY'!U5&"
AND Col12 <= "&'WMR FY'!U6&"
Group by Col2
Order by Count(Col24)
Label Col2 'Center Code', Count(Col24) '"&'WMR FY'!U10&"'",0)
Here Col2 is the center code as there are multiple centers
Col24 is the nature of disability column from source data
Col11 is the Qualification
Col14 is the gender
Col24 = Nature of disability
Col12 = Age
'All data'A3:AX is the source data
With the above formula I am able to populate the data based on selections. However
what I was trying to achieve was this - If I keep one section for example cells for gender blank, it should populate all the genders in line with my other selections of qualification, nature of Disability, etc. Instead if I leave Gender blank I am getting blank cells.
I tried to use an if statement but not sure if it will do the job.
FYI : AND Col3 = '"&'WMR FY'!$B$3&"' is just taking reference of a finacial year.
Sorry if I have asked the question properly. This is the first time I am asking a question as normally I have been able to find solutions online. I am looking for a non appscript soltion.
Please let me know if you want me to create a dummy sheet and I will try to create it at the earliest.
I thought I found a solution but was not able to make it work. Heres the link to the solution I found
Google QUERY Formula to include "All" from a dropdown
try:
=QUERY(IMPORTRANGE("URL", "All Data!A3:AX"),
"select Col2,count(Col24)
where 1=1 "&
IF('WMR FY'!$B$3="",," and Col3 = '"&'WMR FY'!$B$3&"'")&
IF('WMR FY'!T3&'WMR FY'!U3&'WMR FY'!V3="",," and Col4 matches '"&TEXTJOIN("|", 1, 'WMR FY'!T3, 'WMR FY'!U3, 'WMR FY'!V3)&"'")&
IF('WMR FY'!T2&'WMR FY'!U2&'WMR FY'!V2&'WMR FY'!W2&'WMR FY'!X2&'WMR FY'!Y2="",," and Col11 matches '"&TEXTJOIN("|", 1, 'WMR FY'!T2, 'WMR FY'!U2, 'WMR FY'!V2, 'WMR FY'!W2, 'WMR FY'!X2, 'WMR FY'!Y2, "^$")&"'")&
IF('WMR FY'!U4="",," and Col24 = '"&'WMR FY'!U4&"'")&
IF('WMR FY'!U5="",," and Col12 >= "&'WMR FY'!U5)&
IF('WMR FY'!U6="",," and Col12 <= "&'WMR FY'!U6)&"
group by Col2
order by count(Col24)
label Col2'Center Code',count(Col24)'"&'WMR FY'!U10&"'", 0)

How would I convert this formula in Google sheets?

How would I combine the contents of both D and C in the same cell?
The formula is:
=QUERY(courtdates,"SELECT D, C, AQ WHERE BM = date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'",0)
Try this:
=QUERY(
{
INDEX(courtdates_sheets!D:D & " " & courtdates_sheets!C:C),
courtdates_sheets!AQ:AQ,
courtdates_sheets!BM:BM
},
"SELECT Col1, Col2
WHERE Col3 = date '" & TEXT(TODAY(), "yyyy-mm-dd") & "'",
)
Where courtdates_sheets is the sheet name where courtdates named range is defined.

QUERY returns 0

I have a main data sheet (Main) with several columns, the relevant ones being column A, a date (displayed as dd.mm.YYYY) and column D, a numeric value. In a second sheet, I'm using the query function to build the sum over all these numeric values for a specific month using the following expression:
=SUM(QUERY(Main!$A$1:$D$2364; "SELECT D WHERE MONTH(A) = 1"; 1))
MONTH(A) returns a number between 0 and 11 and the above expression works perfectly well for January (0) to October (9). However, as soon as I try to compare MONTH(A)
with a number > 9, the sum shows zero despite there being numerous entries in both November and December.
What I get is this:
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 10"; 1)) equals =SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 1"; 1))
and
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = '10'"; 1)) equals 0
Can anyone help me out with this one?
try:
=SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=SUMPRODUCT(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=ARRAYFORMULA(SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)*1)))

Pass range object as parameter to QUERY function in Google Sheet

In a Google Sheet I have this query:
= QUERY( Data!A1:M300, "select A,B,C,D where C <= date '" & TEXT(B1, "yyyy-MM-dd") & "' AND D >= date '" & TEXT(B1, "yyyy-MM-dd") & "'", 0)
What I would like to do now is make the first parameter of the QUERY function dynamic. For this I wrote this small function:
function getDataRange(sheetName) {
if (sheetName == "") {
sheetName = SpreadsheetApp.getActiveSheet().getName()
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
if (sheet != null) {
output = sheet.getDataRange().getValues();
} else {
output = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
}
return output
}
Now if I pass this function as a first parameter to QUERY like this
= QUERY( getDataRange("Data"), "select A,B,C,D where C <= date '" & TEXT(B1, "yyyy-MM-dd") & "' AND D >= date '" & TEXT(B1, "yyyy-MM-dd") & "'", 0)
I get this error
"Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: A"
I have not found a way to pass a dynamic range as the first parameter to the QUERY function and I am very thankful for any hint.
Try changing your select clause to:
"select Col1, Col2, Col3, Col4 where Col3 <= date '" & TEXT(B1, "yyyy-MM-dd") & "' AND Col3 >= date '" & TEXT(B1, "yyyy-MM-dd") & "'"

Resources