Need SUMIFS to return multiple rows - google-sheets

The error I'm getting:
Function ARRAY_ROW parameter 2 has mismatched row size. Expected: 3.
Actual: 1.
I think I know why I'm getting this error. As you can see in the formula and table below, there are three months worth of data. This would cause three rows with the first Array. However, the SUMIFS function is only returning one number it seems like. However, I need it to return a row for each month. I suspect 3 rows are expected in the error because there are three months, as returned by the first array. Is my understanding of this error correct?
Formula:
=ArrayFormula({{unique(text(A1:A,"MMM YY"))},
{ABS(SUMIFS(C1:C,B1:B,G1,text(A1:A,"MMM YY"), unique(text(A1:A,"MMM YY"))))}})
Note: I am using entire columns so the function will still work without adjustment when more data is added.
Data:
Date
Category
Payment
01/03/2021
Food
$5
02/03/2021
Food
$5
02/06/2021
Fee
$5
03/03/2021
Food
$5
G1 is Food
Expected output:
Jan 21
$5
Feb 21
$5
Mar 21
$5
How can I get SUMIFS to return a new row for each month? Do I need to use a function like OFFSET?

You can use SUMIF + ARRAYFORMULA instead of SUMIFS
=ArrayFormula(unique(if(A2:A<>"",{text(A2:A,"MMM YY"),sumif(text(A2:A,"MMM YY")&B2:B,text(A2:A,"MMM YY")&"Food",C2:C)},)))

You can aggregate the payments by month and show the result for each category in a separate column with a query() formula that uses a pivot clause, like this:
=arrayformula(
query(
{ A1:C, text(A1:A, "yyyy-mm") },
"select Col4, sum(Col3)
where Col3 is not null
group by Col4
pivot Col2",
1
)
)
To only show the category specified in cell G1, use this:
=arrayformula(
query(
{ A1:C, text(A1:A, "yyyy-mm") },
"select Col4, sum(Col3)
where Col2 = '" & G1 & "'
group by Col4
label sum(Col3) '" & G1 & "' ",
1
)
)

Related

How to get running total with two conditions using Arrayformula in Google Sheet

Can someone help me to build formula to get running total with two conditions brands and date using arrayformula?
exp for G4 i want to get sum of QTY before 11th Nov 2022 (F4) for brand D, DG and DA (G2) and so on until column F is not null.
Thanks.
This formula uses QUERY to sum the QTY and group up the given data by brand and date, than uses BYROW with INDEX and FILTER to get the RUNNING TOTAL of the QUERY.
BRAND to lookup need to be entered in range 'G2' and separated with ','(without space - separator can be changed at the last line of code).
You can edit the range at the bottom line of the code to change the range of reference.
Formula in range 'F4':
=ArrayFormula(LAMBDA(DATARANGE,SELECTEDBRAND,SEPARATOR,
QUERY(SPLIT(
LAMBDA(QUERY,
LAMBDA(COL_DATE,COL_TOTAL,
BYROW(QUERY,LAMBDA(ROW,
LAMBDA(DATE,TOTAL,
JOIN(";",DATE,SUM(FILTER(COL_TOTAL,COL_DATE<=DATE)))
)(INDEX(ROW,,1),INDEX(ROW,,2))
))
)(INDEX(QUERY,,1),INDEX(QUERY,,2))
)(
QUERY(
QUERY({DATARANGE},"SELECT Col3,SUM(Col4) WHERE "
&IF(NOT(ISNUMBER(FIND(SEPARATOR,SELECTEDBRAND))),
"Col2='"&SELECTEDBRAND&"'",
JOIN(" OR ","Col2='"&SPLIT(SELECTEDBRAND,SEPARATOR)&"'")
)
&" GROUP BY Col2,Col3 LABEL Col3 '', SUM(Col4) ''",1),
"SELECT Col1,SUM(Col2) GROUP BY Col1 ORDER BY Col1 ASC LABEL SUM(Col2) ''",0)
),
";"),"ORDER BY Col1 DESC",0)
)($A:$D,$G$2,","))

Counting over aggregated columns in Google Sheets

I have the yellow table shown below, and I'm trying to get the blue table, which aggregates columns B:F by value, and then counts the number of 'x' symbols for each row value of column A.
Is there some basic SQL/array magic formula to get this, please? There must be.
Use this new functions formula
=BYROW(B2:4, LAMBDA(v, COUNTIF(v, "=x")))
Used:
BYROW, LAMBDA, COUNTIF
v is the array_or_range
Update
={ A2:A4, BYROW(B2:4, LAMBDA(vv, COUNTIF(vv, "=x")))}
For fun
Update 02
=ArrayFormula(TRANSPOSE(QUERY({
QUERY(TRANSPOSE(IF(A1:4<>"x",A1:4,1)),
" Select * Where Col1 is not null ", 1)},
" Select (Col1),sum(Col2),sum(Col3),sum(Col4) Group by Col1 ", 1)))

What Google Sheets formula should I use to average values that are not empty and are unique?

I have the next two tables:
Table 1, on sheet called "top1Data"
Table 2, on sheet called "top1An"
I want to take all the unique champions from Table 1 and insert them into Table 2 (under Total), and then average their stats (kills, solokills).
Note that more champions can be added in the future, so it has to check for non-empty cells.
You can find an editable copy of the Google Sheet here.
Try this in cell B3:
=query(
query(
C2:E,
"select C, sum(D), sum(E)
where C is not null
group by C",
1
),
"select avg(Col2), avg(Col3)
label avg(Col2) '' , avg(Col3) '' ",
1
)

If query is empty return "no data"

I'm using Google Sheets and my database looks like this:
I have a query formula that changes dynamically based on the month:
my query:
=QUERY('database',"SELECT B,C WHERE A = '"&A1&"'")
At the beginning of the months I don't have data, so I want to display something like this:
I want the column userID permanent in all month, but the column name to display "NO DATA"
I already tried with IFNA or IFERROR but that won't work, because if there's no data, the query is still successful because it displays the column names.
You can try this longer combination of several formulas:
=arrayformula(
query({A2:C;split(E1&"_"&unique(filter(B2:B,B2:B<>""))&"_NO DATA", "_")},
"select Col2, max(Col3) where Col1 = '"&E1&"'
group by Col2
order by Col2 label max(Col3) ''", 0)
)
Where:
A2:C is the dictionary
B2:B is the column of userIDs
E1 is the Month
Outputs:

Google Sheets: sum two columns by QUERY with some empty cells

I have try to get sum of two columns using query function in Google Sheets.
Col1 Col2 Col3
-----------------------
12 User1
23 44 creature
55 User1
14 User1
This work fine if there are at least one number in each column:
=QUERY(IMPORTRANGE('SomeURL';"Page!A1:C");
"select (sum(Col1) + sum(Col2)) where Col3 = 'User1'")
However this query cause error QUERY:AVG_SUM_ONLY_NUMERIC if all cells in one column are empty in result set.
Col1 Col2 Col3
-----------------------
12 User1
23 44 creature
User1
14 User1
How can I get sum of columns using query function, if sometimes the cells are empty in one of the column?
=ARRAYFORMULA(SUM(query(IMPORTRANGE("url","page!A1:C6"),"select Col1,Col2 where Col3 = 'User1'")))
This should work for a simple sum. But i don't think there's a way inside QUERY to consider blanks as zero or assume them as numbers. If you could actually import range into sheet(i.e., use them as helper columns), then you can use ARRAYFORMULA(Query ({filter (A1:B6*1,NOT(ISEMAIL(A1:A6))),C1:C6}, "select *.... You should convert blanks outside Query (by *1)or Sum them outside query. Or use a DOUBLE Query and double import range, which would be performance depreciative.
You can SUM the Query, like this:
=sum(query('SomeURL';"Page!A1:C"); "select Col1, Col2 where Col3 ='User1'"))
OR use SUMIF() twice, once for each column. It means 2 importranges, though, so it will probably be slower.

Resources