Google Sheets AVERAGE function in ARRAYFORMULA - google-sheets

I want to use the ARRAYFORMULA version of =IFERROR(AVERAGE(B29:H29),""). This is to calculate a 7 column rolling average that will automatically drag across using the array formula. I am aware the AVERAGE cannot be used with ARRAYFORMULA but I have struggled to find an alternative.

As discussed in the comments you can modify the previous answer.
Another approach to get the running average of the current number and the next 6 numbers is to take the difference of two running sums and divide by the count like this:
=ArrayFormula(if(C1:1="","",
(sumif(column(C1:1),"<"&column(C1:1)+7,C1:1)-sumif(column(C1:1),"<"&column(C1:1),C1:1))/
countifs(column(C1:1),"<"&column(C1:1)+7,column(C1:1),">="&column(C1:1),C1:1,"<>")))
(you can't use AVERAGEIFS or SUMIFS because they don't work with array formulas).
For completeness, the more usual running average starting with the first number, then the average of the first two numbers etc. would be given by:
=ArrayFormula(if(C1:1="","",
(sumif(column(C1:1),"<="&column(C1:1),C1:1)-sumif(column(C1:1),"<="&column(C1:1)-7,C1:1))/
countifs(column(C1:1),"<="&column(C1:1),column(C1:1),">"&column(C1:1)-7,C1:1,"<>")))

Related

ARRAYFORMULA should be used only once in each formula or should it be used multiple times once for each need?

Sum the VLOOKUP results:
=ARRAYFORMULA(SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE))))
Sum two cells:
=(Z1+Z2)
Sum two specific values from VLOOKUP:
=ARRAYFORMULA(SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE))))
Now I need to come up with an average of the three results:
=ARRAYFORMULA(
SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE)))+
(Z1+Z2)+
SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE)))
)/3
But the faithful form would be:
=(
ARRAYFORMULA(SUM(IFERROR(VLOOKUP(A1:A,B1:C,2,FALSE))))+
(Z1+Z2)+
ARRAYFORMULA(SUM(IFERROR(VLOOKUP(G1:G2,H1:I,2,FALSE))))
)/3
Both will reach the same result, my question is, what is the most correct and safe way from the standards of those who work professionally with Google Sheet?
1 → Use only one ARRAYFORMULA call for the all the formula.
2 → Use multiple ARRAYFORMULA calls, one for each specific need.
Question reason:
I still haven't found risks of using a single ARRAYFORMULA in the beginning and doing everything else within it like =ARRAYFORMULA((...)+(...)) rather than =ARRAYFORMULA(...)+ARRAYFORMULA(...), but I not finding risks doesn't mean they don't exist.
One instance of ArrayFormula on the outside is both sufficient and the professional standard.
However, I must say that I don't understand your formula usage or intention from your posted example. You've got + between each element, which is redundant to SUM. And like ArrayFormula, you only need one outer SUM to sum all elements in your usage.

Is there a google sheets function to count with using arrays of sums as criteria?

I have been struggling for a few days with this problem. Anyone kind enough to show some interested will be highly appreciated.
I have the table shown below.
Suppose columns represent months. I would like to know up to which months' orders have been used up.
I have tried criteria with sums of demand up to that point but I cannot seem to use criteria with the sum of total demand and an array of sums of "total units ordered".
F.e. =COUNTIF(SUM($S$2:($S$2:S$2))<SUM($S$1:S$1) is not possible.
I have tried using an index-match combo but i would have to deduct the previous max sum of "total units ordered" that meets the condition up to the previous cell.
Is that possible without using vba?
Thanks in advance for your interest and time spent.
You can use a standard method of getting running totals using Sumif, combined with Match:
=ArrayFormula(match(sumif(column(S1:Z1),"<="&column(S1:Z1),S1:Z1),sumif(column(S2:Z2),"<"&column(S2:Z2),S2:Z2))-1)
I put rows 3 and 4 in just as a check of my calculations and to show the results of the two Sumifs evaluations - they aren't necessary.
You may wish to specify what should happen if the demands add up to exactly 3000, for example. The above formula would actually go to the next month, so may need some refinement if that is not what you want.

How to calculate an average in Google Sheets excluding outliers which are manually specified in a separate column?

I have a Google Sheet containing a column with values, Price/sqft, which I would like to average, but only including values for which an adjacent column, Outlier? does not contain the word yes.
I took a stab at it following the AVERAGEIF documentation, but I'm getting a 'divide-by-zero' error:
Another way to do it would be to create a separate column which contains the conditional value and compute a regular AVERAGE on that. Is this the way to go? Or is there a way to do this in one go using AVERAGEIF?
You should use the formula =AVERAGEIFS(Q5:Q13,S5:S13,"<>yes")
Here, the syntax is like AVERAGEIFS(average_range, criteria_range1, criterion1)
I hope this will be helpful.
If you do use Averageif, you need to put the criterion range first and the average range last
=averageif(B2:B10,"<>Yes",A2:A10)

Running total with two criteria - seeking arrayformula or app script

I have a google sheet with a list of [people], [dates], and [times]. I need a running total column that gives me a running time total on each line for each combination of name and date, as of that line. I have a working SUMIFS solution. The drawback of this solution is that it requires me to copy the formula down the entire column. Since I ultimately want this to work in a sheet that is fed by a form, I want to use either an ARRAYFORMULA or an APP SCRIPT solution, so that each new row will automatically do the calculation without my needing to copy down the formula.
You can see what I mean in this sample sheet:
https://docs.google.com/spreadsheets/d/1G6cgwwcL6LfnbVUhn8PrMYxrUSImkmgOGvPspparhSI/edit?usp=sharing
Thanks for any thoughts you may have.
-DH
SUMIF() does not play nice with ARRAYFORMULA(). Have a look at this MMULT example spreadsheet, last sheet "Cumulative sum down column", column U
https://docs.google.com/spreadsheets/d/1NJPAt5iFQWIyxHx35bpCh6zjVeKon4rdTPk-dfyZDrU/edit#gid=420781522
If you haven't used matrix multiplication before, and have trouble wrapping your head around the solution, try to follow the examples starting from the 1st sheet.

Calculating Distance Away from a Set Number (Google Sheets)

This is for Google Sheets or I could also write a script for it.
I am a teacher and am trying to create a formula or function that will calculate the distance a range of test scores are away from a set number (70 -- a passing grade).
I have my data in a column as a variety of percentages. I would like the formula or function to first check the range for any values that are less than 70. Then, for the values that are less than 70 it would figure out how far each of those values are from 70 and add them all together. Finally, it will take the sum of the added values and divide by the number of values that fit the criteria (less than 70).
Any ideas on how I would accomplish this? Thanks!
There are numerous formulas you can use: sumif, countif, and averageif
But if you want a dashboard-like spreadsheet that gives you quick summaries, filter is very useful.
Here is an examples of using filters
In particular, the formula is =average(arrayformula(filter(B2:B10,B2:B10<70)-70)), where
filter(B2:B10,B2:B10<70) gives you B values that are < 70
filter(…) - 70 finds the difference
arrayformula(…) is needed because you are working with an array
average(arrayformula(…)) finds the average of the array

Resources