I want to use Arcade to create a pop-up expression field that is a count of some other fields in the dataset that have a non-zero value. For example:
field_1 | field_2 | field_3 | field_4 | field_5 | field_6 | custom_field |
0 | 0 | 4 | 4 | 0 | 1 | 3 |
1 | 1 | 2 | 4 | 0 | 4 | 5 |
This would be similar to what the COUNTIF function does in excel.
I have a small amount of experience in Arcade, and played around with incrementing a variable based on the result of an IIF statement:
var index = 0
iif($feature["field_1"]>=1, ++index, 0);
iif($feature["field_2"]>=1, ++index, 0);
return index
This just ends up giving a value equal to the number of IIF statement in the code, regardless of whether they evaluate to true or false. I have also tried other variations on this, but no luck. I am assuming that the trueValue of an IIF statement can't be used to increment a variable. This would make sense as the expression evaluations are treated as returns.
Any help would be greatly appreciated.
Solved! Go to Solution.
var index = 0
if($feature["field_1"] > 0) { index += 1 }
if($feature["field_2"] > 0) { index += 1 }
return index
// if the fields can be null:
if(!IsEmpty($feature["field_1"]) && $feature["field_1"] > 0) { index += 1 }
var index = 0
if($feature["field_1"] > 0) { index += 1 }
if($feature["field_2"] > 0) { index += 1 }
return index
// if the fields can be null:
if(!IsEmpty($feature["field_1"]) && $feature["field_1"] > 0) { index += 1 }
It works! Thank you so much, @JohannesLindner.