Google Sheets query not working when referencing a cell - google-sheets

I use the query function to extract data from my tables in google sheets. But one of the conditions must be a reference to another cell. I do it like this:
=QUERY({$A2:$B,ArrayFormula(Month($C$2:$C)),$D2:D},"Select SUM(Col4) Where (Col3 = '"&H2&"' ) label sum(Col4) ''",0)
The above result is #N/A. (The field H2 above contains the number 10).
If I replace the '"&H2&"' with the actual number, like below, I get output:
=QUERY({$A2:$B,ArrayFormula(Month($C$2:$C)),$D2:D},"Select SUM(Col4) Where (Col3 = 10 ) label sum(Col4) ''",0)
Any help is appreciated. Thanks in advance.

Single quotes are unnecessary in case of numbers:
=QUERY(
{
$A2:$B,
ARRAYFORMULA(Month($C$2:$C)),
$D2:D
},
"SELECT SUM(Col4)
WHERE Col3 = " & H2 & "
LABEL SUM(Col4) ''",
)
Or you could use MONTH inside QUERY:
=QUERY(
{A:D},
"SELECT SUM(Col4)
WHERE MONTH(Col3) = " & H2 - 1 & "
LABEL SUM(Col4) ''",
)
MONTH in QUERY is 0-based, so there is H2 - 1.

Related

Arrayformula to replace sumifs based on criteria - grouping data into categories/types

In this spreadsheet
Cols G and H give the total of each Account Type, from the data (A1:D)
If sum Dr - sum Cr > 0, then only this is shown in col G.
=if(sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2)>0, sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2),"")
If sum Cr - sum Dr > 0, then only this is shown in col H
=if(sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2)>0, sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2),"")
I am looking for an array formula which replaces these summifs formula, so that if either new Account Types (e.g. Type E, Type F etc) are added or new rows of data are added, then the formula would automatically calculate the sum Dr or sum Cr, instead of having to copy the formula down
try this in cell G2:
=BYROW(F2:F,LAMBDA(fx,IF(fx="",,LAMBDA(cx,dx,{IF(cx-dx>0,cx-dx,),IF(dx-cx>0,dx-cx,)})(SUMIF(A:A,fx,C:C),SUMIF(A:A,fx,D:D)))))
-
Use query(), like this:
=arrayformula(
lambda(
aggregated,
lambda(
account, balance,
{
"Account Type", "Dr", "Cr";
account,
if( balance >= 0, balance, iferror(1/0) ),
if( balance < 0, -balance, iferror(1/0) )
}
)(
query(aggregated, "select Col1", 0),
query(aggregated, "select Col2", 0)
)
)(
query(
A3:D,
"select A, sum(C) - sum(D)
where A is not null
group by A
label sum(C) - sum(D) '' ",
0
)
)
)
You can set a QUERY that finds both entire columns like this:
=QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) where A is not null group by A",1)
And check with LAMBDA if there are negative values and change them to null:
=LAMBDA(a,INDEX(IF(a<0,,a)))(QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) WHERE A is not null group by A",1))
If the values in your result table are just for display, you can use the QUERY format clause to hide negative values, such that the whole thing can be generated within a single QUERY:
=QUERY(A2:D,"select A,sum(C)-sum(D),sum(D)-sum(C) where A is not null group by A label sum(C)-sum(D) 'Dr',sum(D)-sum(C) 'Cr' format sum(C)-sum(D) '0;',sum(D)-sum(C) '0;'",1)

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)

Joining the columns of a query result/range with a newline using sheets arrayformula

Here is the spreadsheet for testing: Click here to copy OR Click to View
I have a query result from some other source of data.
From this table I further need to merge the B and C columns with new line (char(10)) and that must be expected to look like below
But I have tried with Arrayformula and I am not successful.
=ARRAYFORMULA(query(Sheet1!A2:Z7,"Select A,B,C,D,E,F,G,H") & char(10) & query(Sheet1!A2:Z7,"select B") )
Try with an { array expression } like this:
=arrayformula(
query(
{ Sheet1!A2:H7, Sheet1!B2:B7 & char(10) & Sheet1!C2:C7 },
"select Col1, Col9, Col4, Col5, Col6, Col7, Col8",
1
)
)
...or perhaps more simply, like this:
=arrayformula(
query(
{ Sheet1!A2:A, Sheet1!B2:B & char(10) & Sheet1!C2:C, Sheet1!D2:H },
"where Col1 is not null",
1
)
)

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.

Import function using QUERY + REGEXMATCH doesn't work with excluding & select parameters

I would like to import a sheet by 2 different filter function. The first one need to include every row where the value of column 14 is equal to 1, the second one need to exclude rows where column H contains one of those strings "alc" "alcool" "vin". I've seen it in previous questions here google-sheet-query-matches-function-doesnt-exclude-strings. However when i try to add the first filter into my previous formula it doesn't work, here is my formula :
=FILTER(
QUERY(
IMPORTRANGE("URL"; "Sheet!A:BE");
"SELECT Col1, Col3, Col4, Col26, Col8, Col30, Col40, Col41, Col44, Col45, Col49 WHERE Col14 = "1""
);
NOT(REGEXMATCH(IMPORTRANGE("URL"; "Sheet!H:H"); "(?i)alc|vin|alcool"))
)
I try both using and not using "" as the values of my column are numbers but none of them work
use:
=FILTER(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col1,Col3,Col4,Col26,Col8,Col30,Col40,Col41,Col44,Col45,Col49
where Col14 = 1");
NOT(REGEXMATCH(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col8
where Col14 = 1"); "(?i)alcool|vin|alc")))
or shorter:
=INDEX(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col1,Col3,Col4,Col26,Col8,Col30,Col40,Col41,Col44,Col45,Col49
where Col14 = 1
and not lower(Col8) contains "&JOIN(" and not lower(Col8) contains "; "'"&SPLIT(
"alcool|vin|alc"; "|")&"'")))

Resources