Average a list of numbers if greater than 0 - excel

How do I average a list of numbers whose values are greater than 0? I know I can use AVERAGEIF function in Excel
My data is located in A2, A5, A6, A10, A17.
I only want to average it if the data is greater than 0.
Since my data is not an range, I am not able to use AVERAGEIF Function range.
Need some help on this.
EDIT
For example,
I tried with three numbers:
1) 98.068 and 98.954 and 0 so my forumla looked like this:
=AVERAGE(IF(N(OFFSET(A2,{0,5,10},))>0,N(OFFSET(A2,{0,5,10},))))
The answer came out as 99.106. Not sure why.

A few options:
1)=SUM(SUMIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))/SUM(COUNTIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))
2)=AVERAGE(IF(N(INDIRECT({"A2","A5","A6","A10","A17"}))>0,N(INDIRECT({"A2","A5","A6","A10","A17"}))))
3)
=AVERAGE(IF(N(OFFSET(A2,{0,3,4,8,15},))>0,N(OFFSET(A2,{0,3,4,8,15},))))
2) and 3) must be committed as array formulas**
Regards

(0) A simple method
=SUM(A2*(A2>0),A5*(A5>0),A6*(A6>0),A10*(A10>0),A17*(A17>0))/SUM(A2>0,A5>0,A6>0,A10>0,A17>0)
(4) A more general method
=SUM((A1:A20>0)*A1:A20*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((A1:A20>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
The second one is an array formula and must be entered with CtrlShiftEnter
If it's possible to have text in the cells rather than numbers, then this should replace the first formula:-
=SUM(N(A2)*(A2>0),N(A5)*(A5>0),N(A6)*(A6>0),N(A10)*(A10>0),N(A17)*(A17>0))/SUM(N(A2)>0,N(A5)>0,N(A6)>0,N(A10)>0,N(A17)>0)
(I haven't used N in the > brackets in the numerator because I reason that if A2 etc. is text, the product will always be zero)
I can't persuade N to work with arrays in the second formula, so at the moment I have the rather lengthy
=SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*IF(ISNUMBER(A1:A20),A1:A20,0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
but I have tested it on text values and negative numbers and it does seem fine.
The only exception is if one of the cells contains TRUE. In this case the first formula will count it as 1, the second formula will ignore it.

Related

Get Count of Cells used in Excel Formula

I want to get the count of cells used in an excel function.
For example say I have a sum function ='CV'!D11+Farmer!D11+'County'!D11+Rt!D11+WT!D11+'Country'!D11
I need a function that will tell me how many cells were used to get the total sum. In this case it is 6. The tricky part is if one of the cells used is blank I do not want it counted. For instance say cell D11 on the Farmer sheet is blank I do not want it counted in the total. So the total should be 5.
Use COUNT:
=COUNT('CV'!D11,Farmer!D11,'County'!D11,Rt!D11,WT!D11,'Country'!D11)
It will only count the cell if it has a number
You should really try to collate all your data in to a single sheet before running calculations. For the sake of example, I'll assume you have it in the range A1:A5, then you can add handling of the various cases using array formulas:
Get the count of non-empty cells (the ISBLANK function is untrustworthy in my experience): {SUM(IF(LEN(A1:A5)>0,1,0))}
Get the sum of those cells: SUM(A1:A5)
(must use Ctrl+Shift+Enter to enter the formula as an array formula, you will know it worked if the formula shows like {IF(...)} with the curly brackets)
Because blank/missing values are treated implicitly as 0 in the SUM function, this case is simple. If you have other validations then you'd have to write an array formula for the summation as well. For example, only including numbers between a min and max threshold (e.g. if you want to exclude outliers):
{SUM(IF(AND(A1:A5 >= yourMinValue, A1:A5 < yourMaxValue), A1:A5, 0)}.
If I understand your question correctly, you want to literately count the number of cells used in a formula which in your example is summing 6 values from 6 different locations.
I used the following example to demonstrate my solution:
The sum of =A1+B1+C1+D1+E1+F1 is 10 where cell C1 has a 0 value in it but cell E1 is blank.
Using the following array formula I was able to count the number of cells that have a value other than 0:
=SUMPRODUCT(IFERROR(ABS(N(INDIRECT(TRIM(MID(SUBSTITUTE(RIGHT(FORMULATEXT(A3),LEN(FORMULATEXT(A3))-1),"+",REPT(" ",100)),100*ROW(INDIRECT("1:"&LEN(FORMULATEXT(A3))))-99,100)))))>0,0)*1)
Please note you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly.
The logic is to use a combination of TRIM+MID+SUBSTITUTE+RIGHT+FORMULATEXT+REPT+ROW+INDIRECT to extract the cell addresses from the original formula, then use INDIRECT to convert the cell address into the values stored in those cells, then use a combination of IFERROR+ABS+N to find out if any of these values are not 0, and lastly use SUMPRODUCT to add up all the TRUE results.
It is obvious that there are a couple limitations of my solution:
If your actual formula is not strictly in the form of A+B+C+D+E+F, then my SUBSTITUTE part of formula will need further modification;
The formula will treat cells containing 0 as blank and does not include them in the count.
Let me know if you have any questions. Cheers :)

Why is =SUMIF(C5:C19,NOT(ISFORMULA(C5:C19))) returning zero?

I have a spreadsheet that I track my hours. Each cell initially is populated with a formula, i.e. =IF(WORKDAY(B24-1,1,holidays2019)=B24,OFFSET(C24,-1,2),0)
and then as the month progresses I enter my actual time.
In the following excerpt all values through 5/10/2019 are entered.
The formula =SUMIF(C5:C19,NOT(ISFORMULA(C5:C19))) shows zero. I do not understand why this does not work.
I appreciate any help! Column B in my spreadsheet corresponds to the dates shown below and Column C to the time entries.
Expected Result: 48.9
=SUMPRODUCT(J6:J20,--NOT(ISFORMULA(J6:J20)))
The key to this solution is the -- in front of the NOT(). A boolean that is processed by a math operator gets converted to 1 or 0. --, +0, -0, *1, /1 would have all worked to do the conversion. So now you wind up with an array of values you may want to sum being multiplied by an array of 1 and 0 to indicate the ones you want. The 1 are manual entry and the 0 are your formulas entries.
Now SUMPRODUCT performs array like calculations. As a result avoid using full column/row references inside it or you will wind up with a lot of excess calculations. Adjust the ranges in the answer to suit your needs.
Here's the MSDN definition of the Criteria in =SUMIF
criteria Required. The criteria in the form of a number, expression,
a cell reference, text, or a function that defines which cells will be
added. For example, criteria can be expressed as 32, ">32", B5, "32",
"apples", or TODAY().
Important: Any text criteria or any criteria that includes logical or
mathematical symbols must be enclosed in double quotation marks (").
If the criteria is numeric, double quotation marks are not required.
So, the reason, why your SUMIF returns 0 is, because none of the cells match the criteria, as they return a number and meanwhile they expect FALSE
Another issue here being, that the ISFORMULA will return TRUE, even when a range contains a single formula while all the rest has none. So basically you need to drag the formula down for each cell individually and sum them up only when a value is TRUE
Starting from cell D1:
=ISFORMULA(B1)
And then you can simply sum them up with the formula you provided.
=SUMIF(D1:D16,TRUE,C1:C16)
Obviously, you can hide the column D to make it more aesthetically pleasing.
Your formula fails because the criteria you're matching against, is TRUE/FALSE. Obviously the values in C5:C19 don't contain any booleans, so the sum is 0.
To solve this, you can add the correct criteria in cell D5 and below: =ISFORMULA(C5)
Then use =SUMIF(D5:D19,FALSE,C5:C19) to sum the values in column C.

Calculate current streak in Excel row

I have a list of 1s and 0s in excel row ranging from B2:K2, I want to calculate the current streak of 1's in cell M2,
example dataset where streak would be 4
1 0 1 0 1 1 1 1 0
Is there a simple way of doing this? I have tried research but not been able to find anything specific.
Any help would be much appreciated.
Here is a way of doing this with just one formula, no helper columns/rows needed:
The formula used translates to:
{=MAX(FREQUENCY(IF(B1:K1=1,COLUMN(B1:K1)),IF(B1:K1=1,0,COLUMN(B1:K1))))}
Note: It's an array formula and should be entered through CtrlShiftEnter
Assuming your data is layed out horizontally like the image below, the following two formulas should do it for you.
The first cell requires a different formula as the is no cell to the left to refer to. so a simple formula to check if the first cell is one or not is entered in B2.
=--(A1=1)
The part in the bracket will either be true or false. A quirk of excel is that if you send a true or false value through a math operation it will be converted to 1 for true and 0 for false. That is why you see the double - in front. could have also done *1, /1, +0,-0 at the end.
In B2 place the following formula and copy right as needed:
=(A2+1)*(B1=1)
Basically it adds 1 to the series, then check if the number in the sequence is 1 or 0. In the event its one, it keeps the value as it is TRUE sent through the math operator *. If it is false it set the sequence back to zero by multiplying False by the math operator *.
Alternate IF
Now the above while it works and may save a few characters is not necessarily intuitive for most. The go to option would be to use an IF function. The above formulas can be replaced with the following:
A3
=IF(A1=1,1,0)
B3 ->Copied right
=IF(B1=1,A3+1,0)
Longest streak
To get the longest streak, the highest value in your helper row is what you want. You can grab this with the following formula in an empty cell.
=MAX(2:2)
=MAX(A2,I2)
If you have no other numbers in your helper row, you can use the first formula which looks in the entire row. If there are other numbers due to calculations off to the left or right as an example, then you will want to restrict your range to you data as in the second formula.
I've put those values in cells B2 to B8.
In cell C3, I've put this formula:
=IF(AND(B3=1;B2=1);C2+1;1)
Dragging this downto C8, and then take the maximum of the C column.

Excel: Median of even number of values with a condition

When I calculate the median of even numbers for e.g 1,2,3,4,5,6,7,8.. i want the return value to be 5 i.e. the higher value of the two middle values and not the average of 4 & 5. Please help
I don't have Excel, so I can't try it, but I think you should be able to accomplish this with a combination of the LARGE function the COUNT function, and the TRUNC function. For example, if the numbers you are working with are in cells A1 through A8, you should be able to find the answer you want, though technically it's not the median, with the formula
=LARGE(A1:A8,TRUNC(COUNT(A1:A8)/2))
Edit
=LARGE(A1:A8,TRUNC((1+COUNT(A1:A8))/2))
If you know you will always be working with an even number of entries, the call to TRUNC could be omitted.
With data in column A:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),ROUNDUP(MEDIAN(A:A),0))
EDIT#1:
Consider the array formula:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),MIN(IF(A:A>MEDIAN(A:A),A:A)))
If the number of values is odd, return the median. If the number of value is even, return the smallest value greater than the median.
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
This approach does not require the data to be sorted.
EDIT#2:
This array formula appears to handle Ron's case:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),MIN(IF(A:A>=MEDIAN(A:A),A:A)))
It returns the smallest value greater than or equal to the median (for the even case)
But I don't know if this is what the Poster wants.
=LARGE(A1:A9,INT((COUNT(A1:A9)/2)+0.5))
you can use Roundafter the Median
in your case you can type: =ROUND(MEDIAN(B4:I4),0)
lets say that the given range is from B4 to I4 from 1 to 8.

SUMPRODUCT( SUMIF() ) - How does this work?

Part 1:
I was able to construct a formula that does exactly what I want (from some examples), but yet, I'm unable to figure out how exactly it works. I have, starting with cell A1:
Price $
table 20
chair 10
Invoice Quantity
table 17
chair 1
chair 2
table 3
What I want is the final total (430) for the invoice which is computed as Quantity*Price for each item (17*20 + 1*10 + 2*10 + 3*20). the following formula correctly does this:
=SUMPRODUCT(B6:B9,SUMIF(A2:A3,A6:A9,B2:B3))
I understand the basics of SUMPRODUCT and SUMIF. But here, my argument for SUMIF's range is A2:A3, which makes me think the SUMIF would iterate through A2 and A3, and not through A8:A11 (which is the criteria). What gives?
Edit: the unclear part is, what exactly does SUMIF do (what is its iteration pattern) when the first two arguments are of different dimensions (here, the range is 2 cells while the criteria is 4 cells). Also, what is the "output" of SUMIF? An array? Of what dimensions?
Part 2:
In addition, if I ignored the quantity and simply wanted to add 20 whenever I saw a table and 10 whenever I saw a chair, I figured I would do:
=SUMIF(A2:A3,A6:A9,B2:B3)
But that doesn't work, and I have to enclose it with a SUMPRODUCT() for it to work and correctly evaluate to 60. Enclosing it within a SUM doesn't work either (probably because the SUMIF doesn't return an array?) Why?
I've read a bunch of tutorials and still can't understand this, and would be most grateful for a clear, intuitive explanation for both these cases. Thank you.
SUMIF can produce an array of results. If you take my formula
=SUMIF(A6:A9,A2:A3,B6:B9)
it says
For the criteria in A2 (ie table)
- look at A6:A9
- where table is matched, sum the corresponding value in B6:B9
- returns 20 (ie 17 +0 +0 +3)
- this is stored in the first position of the array
Then for the criteria in A3 (ie chair)
- look at A6:A9
- where table is matched, sum the corresponding value in B6:B9
- returns 3 (ie 0 +1 +2 +0)
- this is stored in the second position of the array
So the end array from the SUMIF is {20:3}
You can see the array result by highlighting the SUMIF formula in Excel's formula bar and then pressing F9
Then use SUMPRODUCT to multiple the count in the SUMIF by the $ values in B2:B3 to get total dollars
={20;3}*{20:10}
=20*20 + 3*10
= 430
Part 1
Rather than
SUMIF(A2:A3,A6:A9,B2:B3)
which produces a four element array of
={20;10;10;20}
(corresponding to table;chair;chair;table)
You should use
SUMIF(A6:A9,A2:A3,B6:B9)
which sums the values in B6:B9 against your two criteria in A2:A3 giving the desired result
={20;3}
(corresponding to table;chair)
and then use SUMPRODUCT to weight your array, ie
=SUMPRODUCT(SUMIF(A6:A9,A2:A3,B6:B9),B2:B3)
={20;3}*{20:10}
=430
Part 2
Use COUNTIF to return an array of the number of chairs and tables and then multiply by the vales using SUMPRODUCT
=SUMPRODUCT(B2:B3,COUNTIF(A6:A9,A2:A3))
={20;10} * {2;2}
=60
Well you only have one minor mistake:
probably because the SUMIF doesn't return an array?
SUMIF can work with arrays, thats why you formula SUMPRODUCT( SUMIF() ) works in first place, to SUMIF show an array you have to select a group of cells (like C6:C9) input the formula and use CTRL+SHIFT+ENTER instead of ENTER only. this generate an "array fomula", identified by curly brackets {} (those can only be entered with CTRL+SHIFT+ENTER, no manualy) and show the array formula and results

Resources