Sum column in google excel using 2 conditions - google-sheets

I have a table with 4 columns in a google spreadsheet [timestamp, item, amount, category] and would like to get all rows that belong to a specific month and a specific category and sum the amount column.
So far I have tried
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 1 AND D = 'some text' GROUP BY A",1)
and
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
Where B17 is a text that I want a comparison to be made.

In Google spreadsheets, try
=sumproduct(Sheet1!D2:D=B17,MONTH(Sheet1!A2:A)=1, Sheet1!C2:C)
and see if that works?
If you'd prefer query, you can try
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 0 AND D = '"&B17&"' label SUM(C) ''",1)
Note that MONTH() in query is zero-indexed: so January would be month 0.

You can change this one:
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
with
=sum(filter(Sheet1!C2:C,(Sheet1!D2:D=B17)*(MONTH(Sheet1!A2:A)=1)))

Related

How to Query in google sheets, sort by a column and not include that column in the output

I would like to query in google sheets and sort the query by a specific column in accending order, and have a secondary sort that is also in ascending order. I already know how to do this by
=QUERY(A:C,"select * where month(A)+1 = 1 order by A,B ",0)
Here i queried 3 columns month, unique ID, and name. I selected the data with the necessary month, and sorted it by month, followed by a secondary sort of unique ID. But this query outputs 3 columns. How would i change the formula so the output does not include the month column anymore.
Your question is:
How would i change the formula so the output does not include the month column anymore.
Try wrapping your QUERY formula within another QUERY. Like:
=QUERY(QUERY(your_query_here),"select Col2, Col3")
For your given example it would be:
=QUERY(QUERY(A1:C22,"select * where month(A)+1 = 1 order by A,B ",0),
"select Col1, Col3")

Query/Pivot Function: Header Title Descending Order & Month Format mmmm yyyy

[Goal]
I want to create a table with the Query Function where it counts the number of 'Drivers' for each month in a dynamic manner. Meaning that when the data (example sheet is called 'Data') is updated, it'll be updated automatically as well.
[What I was able to do so far]
I was able to create a table with the Query Function, however, it only displays 1 column worth of Months when I want to show up to 4 months. And I also want to show the recent months from the left and the older months on the right.
[Formula that I have so far]
=QUERY(Data!$A:$B,"
SELECT B,
Count(B)
Where B != '' AND MONTH(A)=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
Group By B
Pivot A
Order By B asc
Label B 'Drivers', Count(B) '"&TEXT(A2,"MMMM YYYY")&"'",1)
[Issue that I'm facing]
I've tried specifying the date range like the below, however there are 2 problems.
The date format is not mmmm yyyy (Example: May 2022) and it'd show as: 2022-2-1 May 2022
The months are ordered in an ascending manner (Example: 2022-2-1, 2022-3-1, 2022-4-1) instead of descending (Example: 2022-4-1, 2022-3-1, 2022-2-1)
So I'm not sure what I need to do to fix this. Hopefully I can have support.
Where B != '' AND MONTH(A)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(A)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
[Sample Sheet]
https://docs.google.com/spreadsheets/d/1AJYTRga9-dXbj64nl4RfpDKs5JYSFwP2SiR7v7gAhMI/edit#gid=1297239620
=ARRAYFORMULA(REGEXREPLACE(""&TRANSPOSE(QUERY(TRANSPOSE(
QUERY({Data!A:B, TEXT(Data!A:A, "yyyymmdd×MMMM yyyy")},
"select Col2,count(Col2)
where Col2 != ''
AND MONTH(Col1)<=MONTH(DATE'"&TEXT(A2,"YYYY-MM-DD")&"')
AND MONTH(Col1)>=MONTH(DATE'"&TEXT(EDATE(A2,-3),"YYYY-MM-DD")&"')
group by Col2
pivot Col3", 1)),"order by Col1 desc")),"^(.*×)", ))
Reference:
How do I change the date format in a Google Sheets query pivot table with date filters?
Sort Query Pivot Table - Google Sheets

Google Sheet Countifs with Multiple Criteria

I'm planning to create a dynamic cell which can count total items or only certain items.
I used the following formula to get the total items. But this formula doesn't work when I filter by item.
=ARRAYFORMULA(SUM(COUNTIFS(A:A;{“D1”;{“APPLE”;”ORANGE”;”POMEGRANATES”}};B:B;”1/6/2022″)))
*D1 = Dropdown for Apple/Orange/Pineapple/Pomegranates
The result I expect is:
If I select Apple then the only value that appears is Apple (4/10).
Apple = 4
Orange = 3
Pineapple = 1
Pomegranates = 2
And
If I don’t select Apple then the values that appear are all values (10/10)
All Fruit = 10
I would be very grateful if you could tell me where the error is and provide a solution.
Demo Sheets
Try this out
=QUERY(
{A:B};
"select Count(Col1)
where
Col1 matches '"&IF(ISBLANK(D1);".*";D1)&"' and
Col2 = date '"&TEXT(DATE(2022;6;1);"yyyy-mm-dd")&"'
label Count(Col1) ''")
If D1 is blank, it'll return everything (.* / the wildcard) -- otherwise it'll pull the word in D1.
If you wanted to use a cell reference for the date, you can replace DATE(2022;6;1) with that cell reference.
Try
=sumproduct(regexmatch(A:A; IF(LEN(D1); D1; "Apple|Orange|Pomegranates|Pineapple")); B:B= date(2022; 6; 1))
and see if that works?
Note that regexmatch is case-sensitive.

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))

How to SUM rows filtered by multiple criteria

Google Sheets question.
I have the following sheet (named "x") containing expenses:
Date Sum Category
1/Jan/2017 100 red
2/Jan/2017 200 blue
3/Jan/2017 10 red
4/Jan/2017 20 blue
1/Feb/17 1 red
2/Feb/17 2 blue
I need to compute monthly totals, per category:
Month Red Blue
Jan/17 110 220
Feb/17 1 2
My current solution is to place in each result cell something like:
=SUM(IFERROR(FILTER(x!$B:$B, MONTH(x!$A:$A)=MONTH($A2), x!$C:$C="red")))
I am asking if there is a better way. I want to have a single result formula working over an array (maybe an ArrayFormula?!) instead of placing and customizing my formula in each cell.
Any ideas?! Thanks!
Well this is the basic idea of using pivot tables but not quite there because I can only get the month as a number so far
query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'")
EDIT
The month number can be changed to a month name using an array formula and VLOOKUP but the formula is getting a bit long (my table of month numbers and names is in columns I & J)
=arrayformula({vlookup(query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col1"),I:J,2,false)})
and you've still got to pick up the other two columns - this is the whole thing
=arrayformula({vlookup(query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col1"),I:J,2,false),query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col2,Col3")})

Resources