How to create an expression that returns count of fields with a greater than 0 value?

518
2
Jump to solution
05-19-2021 07:57 PM
Labels (3)
JamesPreston1
New Contributor II

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_1field_2field_3field_4field_5field_6custom_field
0044013
1124045

 

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.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
Regular Contributor
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 }

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
Regular Contributor
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 }

Have a great day!
Johannes

View solution in original post

JamesPreston1
New Contributor II

It works! Thank you so much, @JohannesLindner.

 

0 Kudos