Array Formula with AverageIF of a row - google-sheets

I have a formula in a Google Sheet that is averaging the values in a row...
=AVERAGEIF(A2:L2,"<>0")
Simple, right?
However, I cannot figure out how to get the ARRAYFORMULA to work for this.
=ARRAYFORMULA(AVERAGEIF(A2:L,"<>0")
That formula only averages all of the cells in the range, instead of returning the average for each row. What am I missing?

you were close. try:
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(A2:L),
"select "&TEXTJOIN(",", 1, IF(LEN(A2:A),
"avg(Col"&ROW(A2:A)-ROW(A2)+1&")", ))&"")),
"select Col2"))
then to add <>0 you need:
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(A2:L),
"select "&TEXTJOIN(",", 1, IF(LEN(A2:A),
"avg(Col"&ROW(A2:A)-ROW(A2)+1&")", ))&
"where "&TEXTJOIN(" and ", 1, IF(LEN(A2:A),
"not Col"&ROW(A2:A)-ROW(A2)+1&"=0", ))&"")),
"select Col2"))

Related

How to convert this FILTER + COUNTA() into an Arrayformula on Google Sheets?

I got this one working fine, but it'd have to be an arrayformula:
=if(A3="","", COUNTA(filter(D:D,regexmatch(LOWER(D:D), LOWER(A3)))))
I have tried it like this, but it gives a total only in the cell the formula sits:
=ARRAYFORMULA(if(A3:A="","", COUNTA(filter(D:D,regexmatch(LOWER(D:D), LOWER(A3:A))))))
Here's the demo file:
Thanks in advance!
try:
=QUERY(D3:D,
"select D,count(D)
where D is not null
group by D
label count(D)''")
update 1:
=ARRAYFORMULA(QUERY({FILTER(D3:D, D3:D<>""),
FLATTEN(INDEX(QUERY(TRANSPOSE(REGEXMATCH(FILTER(D3:D, D3:D<>"")&"", ""&
TRANSPOSE(FILTER(A3:A, A3:A<>"")))*1),
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(COUNTA(D3:D))&")")),2))},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"))
update 2:
=ARRAYFORMULA(FLATTEN(INDEX(QUERY(TRANSPOSE(IF(A3:A="",,
REGEXMATCH(IF(A3:A="",,TRANSPOSE(FILTER(D3:D, D3:D<>""))), A3:A)+0)),
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(COUNTA(A3:A))&")")), 2)))
update 3:
if column A contains empty cells in between
=ARRAYFORMULA(IFERROR(1/(1/FLATTEN(INDEX(QUERY(TRANSPOSE(IF(A3:A="",,
REGEXMATCH(IF(A3:A="",,TRANSPOSE(FILTER(D3:D, D3:D<>""))), A3:A)+0)+0),
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX((A3:A<>"")*ROW(A3:A))-ROW(A3)+1)&")")), 2)))))
update 4:
one more alternative
=INDEX(IFERROR(1/(1/LEN(SUBSTITUTE(TRIM(FLATTEN(QUERY(TRANSPOSE(IFERROR(1/(1/IF(A3:A="",,
REGEXMATCH(IF(A3:A="",,TRANSPOSE(FILTER(D3:D, D3:D<>""))), A3:A)+0)))),,9^9))), " ", )))))

Finding the top 3 values and returning the column ID

Thank you beforehand for your kind response.
Problem: I am trying to do a sheet to sort out the 3 top categories of a given range as shown by the attached screen grab.
enter image description here
I only managed to get the results by using the "large" command. "=large(range,n)". Using this I got the value but what I need is the column index/id rather than the numeric values.
Any ideas on how I would go about achieving this?
Thanks for your reply.
Try the following maybe:
Formula in G2:
=TRANSPOSE(QUERY(TRANSPOSE({A$1:E$1;A2:E2}),"Select Col1 Order by Col2 desc limit 3"))
It's definitely possible in arrayformula.
Using vlookup, a sequence of numbers for the search key:
=arrayformula(sequence(max(if(A:E<>"",row(A:A),))-1,columns(A:E)))
...and a range of columns that are sorted by each row of numbers:
=arrayformula({
sequence((max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1)),
sort({
array_constrain(int((row(A2:A)-2)/(columns(A:E)))+1,(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),1),
array_constrain(iferror(split(flatten(if(A2:E<>"",A1:E1&char(9999),)&A2:E),char(9999)),),(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),2)
},1,1,3,0)
})
The final formula for the solution, in cell G2:
=arrayformula(
array_constrain(
vlookup(
sequence(max(if(A:E<>"",row(A:A),))-1,columns(A:E)),
{sequence((max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1)),sort({array_constrain(int((row(A2:A)-2)/(columns(A:E)))+1,(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),1),array_constrain(iferror(split(flatten(if(A2:E<>"",A1:E1&char(9999),)&A2:E),char(9999)),),(max(if(A:E<>"",row(A:A),))-1)*columns(A1:E1),2)},1,1,3,0)}
,3,0),
max(if(A:E<>"",row(A:A),))-1,3)
)
It will work with blank cells in some of the rows.
The width of the source data is determined by:
=columns(A1:E1)
and the height of the source 'numbers' by:
=max(if(A:E<>"",row(A:A),))-1 (it needs to sit within an arrayformula).
you may use this superior formula:
=ARRAYFORMULA(ARRAY_CONSTRAIN(IFERROR(SPLIT(FLATTEN(SPLIT(QUERY(FILTER(
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col3 where Col2 is not null order by Col1,Col2 desc"), COUNTIFS(
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col1 where Col2 is not null order by Col1,Col2 desc"),
QUERY(SPLIT(FLATTEN(IF(A2:E="",,ROW(A2:A)&"×"&A2:E&"×"&A1:E1)), "×"),
"select Col1 where Col2 is not null order by Col1,Col2 desc"),
SEQUENCE(COUNTA(A2:E), 1, ROW(A2)), "<="&SEQUENCE(COUNTA(A2:E), 1, ROW(A2)))<4)&
FLATTEN(IFERROR(SEQUENCE(MAX((A2:E<>"")*ROW(A2:A)), 3)*
{"a", "a", 0})),,9^9), 0)), " ")), 9^9, 3))

Combining hlookup and vlookup

I am trying to import some data from one sheet to another using lookups.
I have one sheet with some data including date, name, and type of data(Puschases, Shops).
https://docs.google.com/spreadsheets/d/1sg3fh3giXlEg1SePkwZhO_RTUEPN8WBsmAJLWSKbpkA/edit#gid=0
I then have another sheet for each team member. I want the purchases and shops data(from sample data sheet) to import to their sheet by name, date, and type of data.
https://docs.google.com/spreadsheets/d/1of3wCGMZ_JEZpcxvo98Riluc5OJROMLfUN9S9LIkicE/edit#gid=0
Any thoughts on how to do this? Any help would be appreaciated.
try:
=ARRAYFORMULA(IFNA(VLOOKUP(B2:B, QUERY(
IMPORTRANGE("1sg3fh3giXlEg1SePkwZhO_RTUEPN8WBsmAJLWSKbpkA", "Sheet1!B1:I"),
"select Col8,max(Col3) group by Col8 pivot Col1", 1), {3, 4}, 0)))
UPDATE:
=ARRAYFORMULA(IFNA(VLOOKUP(B2:B, QUERY(
IMPORTRANGE("1sg3fh3giXlEg1SePkwZhO_RTUEPN8WBsmAJLWSKbpkA", "Sheet1!B1:I"),
"select Col8,max(Col"&MATCH(A2,
IMPORTRANGE("1sg3fh3giXlEg1SePkwZhO_RTUEPN8WBsmAJLWSKbpkA", "Sheet1!B1:I1"), 0)&")
group by Col8 pivot Col1", 1), {3, 4}, 0)))

Google Sheets Query sum indeterminate number of columns

select sum(Col1),sum(Col2),sum(Col3) ... up to 500 columns but the number might increase or decrease... what is the best way to go about this with one formula?
it would be really nice if you could just do select sum(*) ... but sadly that does not seem to work.
you can do:
=ARRAYFORMULA(QUERY({A1:SF}, "select "&TEXTJOIN(",", 1, "sum(Col"&ROW(A1:A500)&")")))
or:
=ARRAYFORMULA(QUERY({A1:SF}, "select "&TEXTJOIN(",", 1, "sum(Col"&COLUMN(A1:SF)&")")))
or infinitely expanding:
=ARRAYFORMULA(QUERY({INDIRECT("A1:"&SUBSTITUTE(ADDRESS(1, COLUMNS(1:1), 4), 1, ))},
"select "&TEXTJOIN(",", 1, "sum(Col"&ROW(INDIRECT("A1:A"&COLUMNS(1:1)))&")")))

More Efficient Way to Avoid Multiple Calculations #3?

Looking for a more efficient way, possibly array formula for Min/Max down a column. Not sure if array formulas work with this function as I can't get it to.
=ArrayFormula(MAX(INDIRECT("Data!E"&(K42:K169)&":E"&(K43:K170-1))))
=ARRAYFORMULA(MAX(VLOOKUP(K42:K169, {ROW(Data!A:A), Data!E:E}, 2, 0)&VLOOKUP(K43:K170-1, {ROW(Data!A:A), Data!E:E}, 2, 0)))
Note that I am using ROW(Data!A:A) instead of simply ROW(A:A) because the range in current sheet doesn't fit that of sheet Data!.
Here is my current code copied down columns in my sheet.
=MAX(INDIRECT("Data!E"&(K42-1)&":E"&(K43-1)))
Just need a more efficient way. Am I correct that using INDIRECT results in slow calculation times.
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(
INDIRECT("Data!E"&K42&":E"&MAX(K42:K))&","&IF(MOD(ROW(
INDIRECT("Data!A1:A"&COUNTA(L42:L)*K41)), K41)=0, "♦", ),,999^99), "♦")), ","),
"where Col2 is not null", 0)),
"select "&TEXTJOIN(",", 1, IF(LEN(L42:L),
"max(Col"&ROW(A42:A)-ROW(A42)+1&")", ))&"")),
"select Col2"))

Resources