How to use Round() in an array formula? - arrays

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

Related

Evaluation of function AVERAGE caused a divide by zero error

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)))

Excel > Find first first/lowest non-zero value in unsorted array

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.

Excel: Sum The Values Other Than the Top Three in a Range

Using Excel I would like to get the total of the values in a range other than the top three values.
The range has #N/A values I would like to ignore.
The range in horizontal.
In the case where as an example the top 4 values are all 8, I would only want three of the 8s to be excluded from the sum.
I have managed to sum the top three values in the range using the following function:
=SUM(LARGE(IF(NOT(ISNA(AW2:BH2)),AW2:BH2),{1,2,3}))
I assume there is a small adjustment I can make to the above, but my brain's not working.
You can use the AGGREGATE function to ignore the errors:
=AGGREGATE(9,6,AW2:BH2)-SUMPRODUCT(AGGREGATE(14,6,AW2:BH2,{1,2,3}))
Function_num 9 = SUM
Function_num 14 = LARGE
Option 6 = Ignore errors
You will get an error if there are less than three values in the row. You could trap for that with either COUNT or IFERROR if that might be an issue.
Array formula**:
=SUM(IF(ISNUMBER(AW2:BH2),IF(1-ISNUMBER(MATCH(AW2:BH2+COLUMN(AW2:BH2)/10^6,LARGE(IF(ISNUMBER(AW2:BH2),AW2:BH2+COLUMN(AW2:BH2)/10^6),{1,2,3}),0)),AW2:BH2)))
Regards
where the values in the range AW2:BH2 are assumed to of an order greater than 1E-6.
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

Excel COUNT() vs COUNTIF() with arrays

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)

Sum the results of vlookups across multiple columns

I'm trying to avoid repetition. The following formula works:
=IFERROR(VLOOKUP($C3,'Business Goals'!$A$3:$C$8,3),0)+
IFERROR(VLOOKUP($D3,'Business Goals'!$A$3:$C$8,3),0)+
IFERROR(VLOOKUP($E3,'Business Goals'!$A$3:$C$8,3),0)+
IFERROR(VLOOKUP($F3,'Business Goals'!$A$3:$C$8,3),0)+
IFERROR(VLOOKUP($G3,'Business Goals'!$A$3:$C$8,3),0)+
IFERROR(VLOOKUP($H3,'Business Goals'!$A$3:$C$8,3),0)
Essentially I want to sum up columns C:H, and the values I want to sum are all stored within the same lookup table.
For my own sanity, if you're providing an answer and it doesn't REQUIRE INDEX/MATCH please express your answer using VLOOKUP.
You can use array formulas to achieve this (see caveat below):
=SUM(('Business Goals'!$A$3:$A$8=$C3:$H3)*'Business Goals'!$C$3:$C$8)
This uses the fact that in arithmetic operations, (x=y) evaluates to 1 if true or 0 otherwise. ('Business Goals'!$A$3:$A$8=$C3:$H3) creates a rectangular array which is like a lookup table with a 1 where the values in $C3:$H3 match those in 'Business Goals'!$A$3:$A$8. That is then multiplied by the corresponding values in 'Business Goals'!$C$3:$C$8 and finally the whole lot is summed over.
Remember to paste the formula into the formula bar and then press Ctrl+Shift+Enter otherwise it won't be recognised as an array formula.
This works provided you wanted the "exact match" form of VLOOKUP (e.g. VLOOKUP(A1,B2:G30,FALSE)) which returns an error if it can't find the data, rather than the default approximate match which you have. That assumes a sorted list and returns the next largest row if it can't find an exact match. I don't think there is a neat way of doing it if you need the approximate match.

Resources