I think this should be a straightforward question, but for some reason I can't find a solution anywhere.
I have a lengthy formula in excel that, ultimately, returns an array of four items -- i.e. {1,2,0,0}. I want to count how many of the resulting numbers are greater than zero.
When I use =COUNT({1,2,0,0}) on this result, I get the expected answer of 4. But when I try to use =COUNTIF({1,2,0,0}, ">0") it pops up with an error saying that my formula has a problem.
Is there something I'm doing wrong? Is there an array equivalent for COUNTIF() ?
It appears the COUNTIF function only works on ranges, while the COUNT function can utilize an array.
Give SUMPRODUCT a try. Below is a slightly expanded form of your example which I used to test the formula. It basically checks to see if each value in the array is greater than 0, and if it is, it assigns it a value of 1. Then SUMPRODUCT goes through and adds up all the 1s to give you the total number of values greater than 0.
=SUMPRODUCT(IF({1,0,3,0,5,0,0,6,9,9,0,7,0}>0,1,0))
Probably the most concise way to accomplish this is to just convert the TRUE or FALSE value returned from the validation check into a number with the INT function. TRUE translates to 1 and FALSE translates to 0. Then SUM those 1's and 0's.
=SUM(INT({1,2,0,0}>0))
Or as Barry Houdini points out, you can coerce the boolean to an int with:
=SUM(({1,2,0,0}>0)*1)
Or:
=SUM(({1,2,0,0}>0)+0)
Related
In Google sheets, I just input the following values through Data Validation.
Once the range of values are input in a google sheet, it looks like the following:
From the above figure:
I want to take an average of inputted drop down values in the range between
B6 and B14.
I applied a formula =Average(B6,B14). But, it display error message.
Error!
Evaluation of function AVERAGE caused a divide by zero error.
Anyone knows how to resolve it. Thanks in Advance!
You can use the following formula:
=AVERAGE(ARRAYFORMULA(REGEXEXTRACT(B6:B14, "\d+")*1))
Explanation
The =REGEXTRACT() function will extract the first match of a regular expression for the given string. In this case, the first chain of numbers will be extracted.
Afterwards, the result of the function is multiplied by 1. This is done to transform the type of the result of =REGEXTRACT() from a string to a number, so that we may afterwards calculate the average.
=REGEXTRACT(), by default, operates on single strings - but not on multiple cells at the same time. For that reason, we have to wrap the call with =ARRAYFORMULA() which will allow to call it with a range as a parameter.
Finally, the =AVERAGE() function is called.
Handling empty cells
With the previous response, given a range in which at least a cell is empty/doesn't conform the regex specified in the =REGEXTRACT() call, an error will be returned. We have two main options to handle this situation:
Ignore the cell. The cell will be completely ignored for the average computation:
=AVERAGE(ARRAYFORMULA(IFERROR(REGEXEXTRACT(B6:B14, "\d+")*1, "")))
Treat the cell as a 0 towards the average computation.
=AVERAGE(ARRAYFORMULA(IFERROR(REGEXEXTRACT(B6:B14, "\d+")*1, 0)))
Both the solutions use the =IFERROR() function. In case the call to =REGEXTRACT() returns an error, the value will be replaced respectively for an empty string (which doesn't count in the average computation) or for a 0 instead.
use this fx:
=ARRAYFORMULA(AVERAGE(IFERROR(REGEXEXTRACT(B6:B14; "\d+")*1)))
I am trying to count the number of unique values in a range of values after the values are rounded to the hundredths place.
This is the formula I am using to count the number of unique values in the range:
=SUMPRODUCT(1/COUNTIF($EZ6:$FD6,$EZ6:$FD6&""))
and this is what I've tried to count the number of unique values in the range after rounding:
=SUMPRODUCT(1/COUNTIF(ROUND($EZ6:$FD6,2),ROUND($EZ6:$FD6,2)&""))
This formula produces an error and doesn't evaluate.
The issue isn't adding the "" at the end, since this formula also produces an error and doesn't evaluate:
=SUMPRODUCT(1/COUNTIF(ROUND($EZ6:$FD6,2),ROUND($EZ6:$FD6,2)))
By produce an error, I mean that a message box appears saying "There is a problem with this formula..."
Yes unfortunately Countif only works with ranges, not arrays - as soon as you introduce the Round function, you are implicitly using arrays.
I would suggest using the Frequencies method for getting unique values:
=SUM(--(FREQUENCY(IF(EZ6:FD6<>"",ROUND(EZ6:FD6,2)),ROUND(EZ6:FD6,2))>0))
This is based on a standard formula see documentation- the only issue is that if you apply the Round function to blank cells, you get zero, which could give you an extra unique value, hence the If statement.
Must be entered as an array formula using CtrlShiftEnter
Using a complex series of array combinations using standard Excel functions in a single formula (no VBA or UDF's involved), I've got the following result appearing mid-formula. I now need to add one final step to the formula to pull just one number out of this array:
{0,0,0,4,0,6,7...}
Using Excel formulas, how can I retrieve the first non-zero figure (in this example, 4) without any additional references to the array? Due to the complexity of the calculations it took to produce this array from the source data, I'd rather not do this twice in the same formula.
So I'm looking for functions or operators which can be applied to the array in order to yield the required result - ideally something elegant and simple. MATCH and LOOKUP fail because they require the array to be sorted. MIN fails because the lowest value is 0, not 4.
NB: The value of each non-zero figure also corresponds with its position in the array (the first would be 1, second would be 2, etc), so the first non-zero number will always be the smallest.
Try this
=1/AGGREGATE(14,6,1/{0,0,0,4,0,6,7},1)
It finds the maximum of the reciprocals ignoring the error values so the answer is 0.25. Then it takes the reciprocal of that.
Simple question that I wasn't able to figure out. Trying to calculate counts and medians of arrays with a conditional that depends on numbers in two columns in Excel, as seen below.
=MEDIAN(IF(AND($D$3:$D$1216=1,OR($B$3:$B$1216=3,$B$3:$B$1216=6,$B$3:$B$1216=9,$B$3:$B$1216=12)),$M$3:$M$1216))
Please see above for the function, which I input as an array. All I'd like to do is scan Column D for all 1s and then 3/6/9/12 in Column B.
The function works but only returns zeroes for all relevant values in Column D, which I find strange.
Thank you for your help!
Is 0 in fact the most common element in $M$3:$M$1216? i.e., are a lot of those cells just blank?
When I keep the range large, but only enter a few values, I get the same result as you. When I shrink the range to something more manageable, and fill in the values, the function returns what I'd expect.
Also, here's a somewhat simpler syntax for the same result:
=MEDIAN(($D$3:$D$5=1)*OR($B$3:$B$5=3,$B$3:$B$5=6,$B$3:$B$5=9,$B$3:$B$5=12)*($M$3:$M$5))
I am coming to seek guidance with another question with Excel Array Formulas.
I am using Excel 2003 and trying to understand a new spreadsheet with the following type of formula :
{=IF($B$6,SUM(($C$6:$AM$6=1)*1),)}
I have tried using the Excel formula audit tool to understand the formula but it crashes when I run it on these type of array formula.
As of now I am thinking that the formula does this :
B6 has a number = True / False
If True do SUM of (C6:AM6=1) and multiply by 1
If False do nothing
However, I am not 100% of the second statement. Does it say SUM the number of times 1 is present in C6:M6 then multiply by 1? If so why multiply by 1. My only guess on the latter is that the (C6:M6=1) returns a True or False value and the *1 converts it to 0 or 1. Then if this is correct, what is the purpose of the sum function?
Thanks for any guidance.
You are correct.
($C$6:$AM$6=1) returns an array of TRUE/FALSE values, which SUM would ignore.
Multiplying by 1 creates an array of 1/0 values, which are then added by SUM to create a count of the number of cells in the range that equal 1.
COUNTIF would be simpler as mentioned previously.
I think you do not need an array function. Try this non-array formula:
=IF($B$6,SUMIF($C$6:$AM$6,1,$C$6:$AM$6),"Do Nothing")