How to use Averageifs or Sumifs in an Arrayformula - google-sheets

I'll post a link to the spreadsheet at the bottom of this write-up.
I'm trying to work out the average of a column of numbers that fall between a date range. The formula below works but I have to drag it down the column, I want it to be an array so it auto updates.
=iferror(averageifs(B$2:B,A$2:A,">="&C2,A$2:A,"<="&D2),1)
So I created it as an array as follows:
=iferror(ArrayFormula(averageifs(B$2:B,A$2:A,">="&C2:C,A$2:A,"<="&D2:D)),1
But it stops after the first cell. So I broke up the average function into sum & count in order to divide.
The count array works using this formula:
=iferror(ArrayFormula(countifs(A$2:A,">="&C2:C,A$2:A,"<="&D2:D)),1)
However the sum array does not even go past the first cell:
=iferror(ArrayFormula(SUMIFS(B$2:B,A$2:A,">="&C2:C,A$2:A,"<="&D2:D)),1)
And the combination gets past the first cell but the calculations all come to zero:
=ArrayFormula(iferror(SUMIFS(B$2:B,A$2:A,">="&C2:C,A$2:A,"<="&D2:D)/countifs(A$2:A,">="&C2:C,A$2:A,"<="&D2:D),1))
What I'm trying to achieve is an average for the previous month.
If anyone can help me I'd be very grateful!
https://docs.google.com/spreadsheets/d/1XhoLl5hB-MpXFz9VS2aLqJOWbXk1d_7apnwKWFdDUlg/edit?usp=sharing

you are trying to apply a bit weird structural logic with lots of repeating values with no reason. basically, it looks like you need this:
=ARRAYFORMULA(QUERY({TEXT(A2:A, "yyyy-mmm"), B2:B},
"select Col1,avg(Col2)
where Col2 is not null
group by Col1
order by avg(Col2) desc
label avg(Col2)''", 0))
but if you really really need so:
=ARRAYFORMULA(IF(LEN(A2:A),
IFERROR(VLOOKUP(TEXT(DATE(YEAR(A2:A), MONTH(A2:A)-1, 1), "yyyy-mmm"),
QUERY({TEXT(A2:A, "yyyy-mmm"), B2:B},
"select Col1,avg(Col2)
where Col2 is not null
group by Col1", 0), 2, 0), 0), ))

Related

How to make an arrayformula count values by row?

I have tried many approaches but I can't seem to figure out a solution to a fairly simple problem with an array formula it seems:
I have a sheet where I get results of individual results of runs of a certain scientific experiment by row.
Next, I'd like to count the occurrence of each output value. Output values can be a number from 1 to 10 so I'd like to individually count how many times the output has been per value.
Also, I'd be then summing up how many individual outputs have been generated in total. So I'd be counting the number of unique outputs within a row.
Now my issue is that when using Arrayformula the way I normally use it it doesn't work because it would count and sum up everything for the whole Matrix specified. However, I just want the current row to be counted. I tried using ROW to get there, but i failed.
I also tried some things like =ARRAYFORMULA(SUMIF(IF(COLUMN(B1:F1),ROW(B2:F5)),ROW(B2:F5),B2:F5)) to understand how to apply the logic, but I fail to get it working as COUNTIF version if itself.
Test Sheet
Any help and ideas would be highly appreciated!
in H2 use:
=INDEX(MMULT(IF(INDIRECT("B2:F"&MAX((ROW(A2:A)*(A2:A<>""))))=""; 0; 1);
FLATTEN(COLUMN(B:F)^0)))
in J2 use:
=INDEX(ARRAY_CONSTRAIN(QUERY(QUERY({IFERROR(SPLIT(FLATTEN({ROW(A2:A)&"×"&B2:F}); "×"));
{SEQUENCE(10)*9^9\SEQUENCE(10)}};
"select count(Col1),Col1 where Col2 is not null group by Col1 pivot Col2");
"where Col11 < 100000 offset 1"; 0); 9^9; 10))
update:
H1:
={"Total No. of outcomes"; ARRAYFORMULA(IFNA(VLOOKUP(ROW(A2:A);
QUERY(SPLIT(UNIQUE(FLATTEN(ROW(B2:F)&"×"&B2:F)); "×");
"select Col1,count(Col1) where Col2 is not null group by Col1"); 2; 0)))}
J1:
=INDEX({"Occurence of "&SEQUENCE(1; 10); ARRAY_CONSTRAIN(QUERY(QUERY({IFERROR(
SPLIT(FLATTEN({ROW(A2:A)&"×"&B2:F}); "×")); {SEQUENCE(10)*9^9\SEQUENCE(10)}};
"select count(Col1),Col1 where Col2 is not null group by Col1 pivot Col2");
"where Col11 < 999999 offset 1"; 0); 9^9; 10)})
demo sheet
Just an afterthought to this, I think the accepted answer is optimal but I did wonder if you could solve it using only countifs. The answer is yes, but you have to force the array arguments to be the same size and shape otherwise you get the 'array arguments different sizes' error so in J2:
=ArrayFormula(countifs(B2:F6,mod(sequence(5,10,0),10)+1,row(B2:F6)+B2:F6-B2:F6,int(sequence(5,10,0)/10)+2))
Note the the total number of outcomes column gives the number of different outcomes using the formula below in H2:
=ArrayFormula(mmult(sign(J2:S6),sequence(10,1,1,0)))

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

Google Sheets Column function to get Average of all the columns in a range to the right using current row

I'm not sure why I can't find examples of this.
But I want my column E to have an average of all the columns to the right. In single example it's:
=AVERAGE(F2:O2)
Now I try to do this for the whole column:
=ArrayFormula(IF(ROW(E:E)=1,"Aggregate",AVERAGE(F:O)))
The problem is it doesn't do it per row, it just does the whole array. Maybe i don't want to use ArrayFormula
I should also put a IF(ISBLANK(F (Row())), "" So I don't calculate blank rows.
How do I put dynamic row on this?
try:
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(F2:O),
"select "&TEXTJOIN(",", 1,
IF(TRIM(TRANSPOSE(QUERY(TRANSPOSE(F2:O),,99^99)))<>"",
"avg(Col"&ROW(F2:F)-ROW(F2)+1&")", ))&"")),
"select Col2"))

Marrying Query and Arrayformula through cell reference in Google Sheets

Trying to understand if it is possible to apply ARRAYFORMULA to situations when QUERY is used in Google Sheets.
For example, I used QUERY for querying and aggregating a set of items, like so:
=QUERY($H$2:$I$17,"select sum(I) where H='"&A2&"' label sum(I) ''",0)
But in order to make that work across the spreadsheet, I will have to drag this formula down. There is also the ARRAYFORMULA thing, which is supposed to help with getting rid of excessive dragging, however it does not seem to work with QUERY, or am I just missing something?
A picture for a quick look:
And a shared file for the longer thinking:
https://docs.google.com/spreadsheets/d/1xOdqeESrFbrBknNYahSeF0ripA5fr2vVFQ-r--lkdA0/edit?usp=sharing
use this formula:
=QUERY(H2:I17, "select H,sum(I) where H is not null group by H label sum(I)''", 0)
and then you can do simple VLOOKUP like:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A, QUERY(H2:I17,
"select H,sum(I) where H is not null group by H label sum(I)''", 0), 2, 0)))
Here two method alternatively:
first ==>
=arrayformula(sumif(H2:H,"=" & unique(filter(H2:H,H2:H<>"")),I2:I))
second ==>
=arrayformula(
query(
filter({vlookup(H2:H,{A2:A,row(A2:A)},2,false),H2:I},H2:H<>"")
,"Select sum(Col3) group by Col1 label Sum(Col3) ''"
)
)

Google Sheets import range returning incorrect data

The full google spread sheet system is used for score keeping and is prone to delays when updating, however I have never run into an issue like this were the same basic function is returning two separate results. The problem is repeatable and occurs on more than one spreadsheet.
I have created a test sheet-
https://docs.google.com/spreadsheets/d/1arh0D9ch5MpQjRh_bHjLfLx5S7TAW8R_pgGLf5tovig/
with the code in question; Can anyone help please?
=QUERY(IMPORTRANGE("***","***"),"select Col1 where Col1 <>5 order by Col9 desc")
in your QUERY formula you are selecting cells that are numeric and comparing it to <>5 but take a notice that A2 is not numeric:
that is the reason why 2 2 is not included in your C column
also it looks like that your QUERY formula smashed first cells into one because you did not specify the 3td query parameter. try:
=QUERY(IMPORTRANGE("1pnowvo6YVj-DZAPCaKE2x9vSIbpAAmlwhRMO2OZNlrE","color!A84:M115"),
"select Col1 where Col1 <>5 order by Col9 desc", 0)

Resources