Filter a Query forumla by SUM() - google-sheets

I'm building a sales Dashboard for my company.
I use the following QUERY to list all my employees and their revenue:
=QUERY(Master!A2:O, "
SELECT F, SUM(O), SUM(G)
WHERE A >= date '" & text(D3,"yyyy-MM-dd") & "'
and A <= date '" & text(D5,"yyyy-MM-dd") & "'
AND UPPER(F) contains '"&$L3&"'
AND UPPER(D) contains '"&$L5&"'
GROUP BY F
ORDER BY SUM(G) DESC
LABEL SUM(G) 'Revenue', SUM(O) 'Work Hours', F 'Employee'
")
Some of the data are not attached to a person and their work hours, so I'd like to get rid of these results. To only List my Employees by Work Hours and Revenue.
Here is what I get out of this formula and on the right what I'd like to have:
I would like to cut out the irelevant Online Sales and the Employees who just worked bellow 10 Work Hours.
I tried bellow "GROUP BY F":
AND SUM(O) > 10
HAVING SUM(O) > 10
WHERE SUM(O) > 10
Somehow if I try to filter a SUM() it get's rejacted..
How can I fix that?

try:
=QUERY(QUERY(Master!A2:O,
"select F,sum(O),sum(G)
where A >= date '" &TEXT(D3, "e-m-d")&"'
and A <= date '" &TEXT(D5, "e-m-d")&"'
and upper(F) contains '"&$L3&"'
and upper(D) contains '"&$L5&"'
group by F
order by sum(G) desc
label sum(G)'Revenue',sum(O)'Work Hours',F'Employee'"),
"where Col2 > 10
and not Col1 contains 'Online Sales'", 1)

Related

How to use Query Function to list all Salesman according to the Date Range and Total Sales Range?

For example:
From 4/14 to 5/1, List the saleman whose total sales range is within $30 ~ $50
The Result should be:
Salesman 0196
(Because his total sales is $47.95 from From 4/14 to 5/1 )
I sincerely need your help to write a Query Function to list all Salesman according to the Date Range and Total Sales Range ? Thank you so much
https://docs.google.com/spreadsheets/d/13pIdJOtshK2mv4B2II8gAjKvL9uIjg87LHzREZNEtbE/edit?usp=sharing
In the spreadsheet you shared I entered this formula
=query(query(A:E, "Select B, sum(E) where A >= date '"&text(I1, "yyyy-mm-dd")&"' and A <= date '"&text(K1, "yyyy-mm-dd")&"' group by B", 0), "where Col2 > "&I2&" and Col2 < "&K2&" order by Col2 desc label Col1 'Salesman', Col2 'Total Sales'", 0)
See if that works for you?

QUERY Function: How to count with multiple counting criterias

[Goal]
I'm trying to count the number of tickets per employee in one column that has a status with either "Finished," "Finished (Scope)," or "Routed (Sales)" for a specific week. In another column I also want to count the number of tickets for a specific week without criteria. The data that I'm pulling from to count the tickets has the following column names.
Column A: Date,
Column B: Ticket ID,
Column E: Employee,
Column H: Finished Week
Column K: Week
In the formula, you'll notice that it's referring to cell H1, which the cell contains the current week which is this formula: =TODAY()-MOD(TODAY()-2,7)-1
[Current Formula]
=QUERY('Data'!$A$3:$J,"Select E,
COUNT(B) where D matches 'Finished|Finished \(Scope\)|Routed \(Sales\)'
AND H = "&H1&" GROUP BY E LABEL COUNT(B) 'Total Finished Tickets'",0)
[What it should look like]
I've created a sample spreadsheet that you can refer to.
Link: https://docs.google.com/spreadsheets/d/1MQLgt_SSbUIKv1rEwx-Y21hooxNOOgcUm_j1rFehHdg/edit?usp=sharing
[Issue]
I was able to create a table that counts the number of tickets per employee with the status as "Finished" OR "Finished (Scope)" OR "Routed (Sales)." Which is the "Current Result" table (Link: https://docs.google.com/spreadsheets/d/1MQLgt_SSbUIKv1rEwx-Y21hooxNOOgcUm_j1rFehHdg/edit#gid=0).
However, as I tried to add another count criteria, it gave me errors and I don't understand how to properly make this work. I wanted to look like the table of the title "Ideal Result" in the shared link. Can someone please help?
You can use the pivot clause to get a breakdown by the Status column like this:
=query(
Data!A3:J,
"select E, count(E)
where H = " & E4 & "
group by E
pivot D
label E 'Employee' ",
0
)
The downside is that the grand total must then be calculated separately, but that can be done with a simple sum() formula.
Alternatively, get the totals first, and then do a lookup to get the number of finished tickets, like this:
=query(
Data!A2:J,
"select E, count(D)
where H = " & E4 & "
group by E
label E 'Employee', count(D) 'Total new tickets' ",
0
)
=arrayformula(
iferror(
vlookup(
E12:E,
query(
Data!A2:J,
"select E, count(D)
where H = " & E4 & "
and (D = 'Finished' or D = 'Finished (Scope)')
group by E
label count(D) 'Finished tickets' ",
1
),
2,
false
)
)
)
Note that this serves just to illustrate how to aggregate the data into a report. Your question leaves it unclear as to which status values should be counted for each type of aggregation. No rows with status Routed (Sales) appear in the data, and I cannot see how the expected results you show could be derived from the data.
See your sample spreadsheet.
H1, which the cell contains the current week which is this formula
=TODAY()-MOD(TODAY()-2,7)-1
You may want to try the weeknum() function.
To get two independent counts, you can't use a Where clause because that would exclude cases from both counts, but you could use the fact that Query does not count empty cells something like this:
=ArrayFormula(query({if(regexmatch(D3:D,"Finished$|Finished \(Scope\)$|Routed \(Sales\)$"),true,),E3:E,if(K3:K>=H1,true,)},"select Col2,count(Col3),count(Col1) where Col2 is not null group by Col2 label count(Col1) 'Finished', count(Col3) 'New'",1))

Google Sheets outer join on 2 tables that get summarised

I have a report I am developing that needs to be in google sheets
Sheet 1 contains all the current cases that are opened and owned by a case owner, 1 row per case, multiple cases per case owner per month (sample sheet has 4 months of data)
Sheet 2 contains all the cases that were closed and who closed them, 1 row per case, multiple cases per case owner per month (sample sheet has 4 months of data)
There could be case owners in the open sheet that don't exist in the closed sheet
There could be case owners in the closed sheet that don't exist in the open sheet
I know how create a query() function that summarises both raw data separately - what I want to be able to do is query a joined table to get open and closed cases in 1 table.
In the sample file there are 2 tables in A5 and F5. These were me looking at how the query() function worked - the actual report I want is in K5
Where I am struggling is bringing this together into one summary report which shows the last 3 months of data
To clarify - what I want to do is have one function that queries from the 2 RAW sheets and creates the summary report
The sample sheet should more clearly explain what I need to do
https://docs.google.com/spreadsheets/d/1QEd9ZauY0YrbRWNu35tDTk6SkL1PaIZLtSOCd1A2fDY/edit?usp=sharing
Output for the OPEN cases:
=query('Raw Data'!A:N,"Select B, count(A) where N >= date '2020-02-01' group by B pivot N")
Output for the CLOSED cases
=query('Raw Data - Closed'!A:N,"Select A, count(B) where H >= date '2020-02-01' AND H <= date '2020-04-01' group by A pivot H")
Any pointers?
try:
=ARRAYFORMULA(SUBSTITUTE(TRANSPOSE(QUERY(TRANSPOSE({{
IF(ISNUMBER(A5:D5*1), A5:D5&" A"&A3, A5:D5); A6:D},
IFNA(VLOOKUP(A5:A, {IF(ISNUMBER(F5:I5*1), F5:I5&" "&F3, F5:I5); F6:I},
COLUMN(G5:I5)-(COLUMN(F5)-1), 0))}), "order by Col1", 1)), " AOpen", " Open"))
=ARRAYFORMULA(SUBSTITUTE(TRANSPOSE(QUERY(TRANSPOSE({{
IF(ISNUMBER(
QUERY('Raw Data'!A:N, "select B,count(A) where N >= date '2020-02-01' group by B pivot N limit 0")*1),
QUERY('Raw Data'!A:N, "select B,count(A) where N >= date '2020-02-01' group by B pivot N limit 0")&" AOpen",
QUERY('Raw Data'!A:N, "select B,count(A) where N >= date '2020-02-01' group by B pivot N limit 0")); QUERY(
QUERY('Raw Data'!A:N, "select B,count(A) where N >= date '2020-02-01' group by B pivot N"), "offset 1", 0)},
IFNA(VLOOKUP(INDEX(
QUERY('Raw Data'!A:N, "select B,count(A) where N >= date '2020-02-01' group by B pivot N"),,1), {IF(ISNUMBER(
QUERY('Raw Data - Closed'!A:N, "select A,count(B) where H >= date '2020-02-01' and H <= date '2020-04-01' group by A pivot H limit 0")*1),
QUERY('Raw Data - Closed'!A:N, "select A,count(B) where H >= date '2020-02-01' and H <= date '2020-04-01' group by A pivot H limit 0")&" Closed",
QUERY('Raw Data - Closed'!A:N, "select A,count(B) where H >= date '2020-02-01' and H <= date '2020-04-01' group by A pivot H limit 0")); QUERY(
QUERY('Raw Data - Closed'!A:N, "select A,count(B) where H >= date '2020-02-01' and H <= date '2020-04-01' group by A pivot H"), "offset 1", 0)},
SEQUENCE(1, COLUMNS(
QUERY('Raw Data - Closed'!A:N, "select A,count(B) where H >= date '2020-02-01' and H <= date '2020-04-01' group by A pivot H"))-1, 2, 1), 0))}), "order by Col1", 1)), " AOpen", " Open"))

Query Subset of two tabs only returns #N/A

I am trying to combine two tabs of data to plan a daily routine. One tab is for recurring tasks and one for adhoc tasks.
The data on the tabs are mostly similar except on Sheet1 Col4 is a day of the week, whereas on Sheet2 Col4 is a date
What I would like is to choose the current day of the week and have the query return all recurring tasks for that day, as well as any adhoc tasks for that specific date
I can return both sets of data individually however I can't get the query to return both subsets together
Sheet1 Data
ID,TASK NAME,LOCATION,TASK DAY
REC1,Task 1,Office,Monday
REC2,Task 2,Office,Tuesday
REC3,Task 3,Field,Wednesday
REC4,Task 4,Office,Thursday
REC5,Task 5,Field,Friday
REC6,Task 6,Field,Monday
REC7,Task 7,Field,Tuesday
Sheet2 Data
ID,TASK NAME,LOCATION,TASK DATE
ADH1,AdHoc 1,Office,25/Jun/2019
ADH2,AdHoc 2,Office,26/Jun/2019
ADH3,AdHoc 3,Field,27/Jun/2019
ADH4,AdHoc 4,Office,28/Jun/2019
ADH5,AdHoc 5,Field,29/Jun/2019
ADH6,AdHoc 6,Field,30/Jun/2019
ADH7,AdHoc 7,Field,1/Jul/2019
This pulls the data from sheet 1 based on the day of the week in cell J1
=query({Sheet1!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = """&J1&""" ",0)
This pulls the data from sheet 2 based on today()
=query({Sheet2!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = date '" & text(today(),"yyyy-mm-dd") & "' ",0)
Thought this would work but merely returns an empty set
=query({Sheet2!A:D;Sheet1!A:D}, "select Col1,Col2,Col3,Col4 where Col4 = date '" & text(today(),"yyyy-mm-dd") & "' OR Col4="""&J1&""" and Col1<>'' ",0)
When J1 = Tuesday and date = 25/Jun/2019 I should get
ID,TASK NAME,LOCATION,TASK DAY
ADH1,AdHoc 1,Office,25/Jun/2019
REC2,Task 2,Office,Tuesday
REC7,Task 7,Field,Tuesday
but instead, merely get #N/A
You almost had it.
Rather than put the array inside the queries; put the queries inside the array.
={QUERY(Sheet1!A:D, "SELECT A, B, C, D WHERE D = """&J1&""" ");
QUERY(Sheet2!A2:D, "SELECT A, B, C, D WHERE D = date '" & text(today(),"yyyy-mm-dd") & "'")}
Credit - Stacking multiple query output in one sheet:
Might be too late but issue was in date column after converting it to text query works as well :
=query({Sheet2!A:D; Sheet1!A:D}, "select Col2,Col1,Col3,Col4 where (Col4='"&Sheet1!J1&"' OR Col4 = '" & to_text(today()) & "')",0)

Can't query using cell reference from other functions in Google Sheets

I'm trying to get an integer value from another cell, A1, into my Google Sheets query:
=QUERY(B:F; "select B where F > "& A1 &"limit 1")
I get this error:
PARSE_ERROR: Encountered " "," ", "" at line 1, column 27. Was expecting one of: "group" ...
A1 contains a function =B1+C1 and that returns an integer. Though this query works if A1 is a hard-coded value like 100. I am not sure what is causing this error could be a parsing error or query error. Does anyone know how to fix it?
NOTES:
The referred cell, A1, has currency formatting in swedish krona(kr) that's not something custom. The regional settings are set to Sweden.
try it like this:
=QUERY(B:F; "select B where F > '"& A1 &"' limit 1"; 0)
or with INDIRECT:
=QUERY(B:F; "select B where F > '"& INDIRECT("A1") &"' limit 1"; 0)
There is a missing space before limit (it should be " limit 1" instad of "limit 1")
Instead of
=QUERY(B:F; "select B where F > "& A1 &"limit 1")
use
=QUERY(B:F; "select B where F > "& A1 &" limit 1")
Explanation
If the of A1 is an integer like 100 the resulting argument is
select B where F > 100limit 1
but that isn't a syntax supported for Google Query Language
It works when you set the reference cell inside an Value(S13). It didn't work because of a bug in Google Sheets. Did try the query in another sheet, without Value(S13) and it worked, that confirms the bug.
This is the workaround query:
=QUERY(B:F; "select B where F > "& Value(S13) &"limit 1")
The pseudo sheet for testing

Resources