Select to view content in your preferred language

Arcade Calculation Rule Help - Average non null values

727
3
Jump to solution
06-21-2022 08:13 AM
Labels (3)
Robswann
Occasional Contributor

Hello, I am trying to get the following rule to work. I have 3 fields that I would like to get the average from, but in some instances not all 3 fields have values and are null. I would like to average the non null values. I have tried Count, and other varies methods but cant seem to get it to work. Any help would be appreciated. Thanks 

VAR LC1 = $feature.Lot_Cost
VAR LC2 = $feature.Lot_Cost_
VAR LC3 = $feature.Lot_Cost__
VAR AVLOT = COUNT([LC1,LC2,LC3])

SUM(LC1,LC2,LC3) / AVLOT

The above is what I am working on, but returns the wrong value. 

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

This is my biggest gripe with Arcade: it treats null values as zero. To circumvent that, you have to manually check each value for null, for example like this:

var values = [$feature.Lot_Cost,
    $feature.Lot_Cost_,
    $feature.Lot_Cost__,
]
var value_sum = 0
var value_count = 0
for(var i in values) {
    if(values[i] != null) {
        value_sum += values[i]
        value_count ++
    }
}
return value_sum / value_count

 

Related idea (though I don't think this will be implemented): Arcade: Don't treat null as zero in mathematical f... - Esri Community


Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

This is my biggest gripe with Arcade: it treats null values as zero. To circumvent that, you have to manually check each value for null, for example like this:

var values = [$feature.Lot_Cost,
    $feature.Lot_Cost_,
    $feature.Lot_Cost__,
]
var value_sum = 0
var value_count = 0
for(var i in values) {
    if(values[i] != null) {
        value_sum += values[i]
        value_count ++
    }
}
return value_sum / value_count

 

Related idea (though I don't think this will be implemented): Arcade: Don't treat null as zero in mathematical f... - Esri Community


Have a great day!
Johannes
0 Kudos
Robswann
Occasional Contributor

This is perfect. Thanks again 

0 Kudos
drWood
by
Regular Contributor

The immediate workaround to this that comes to mind would be to add a string of code to select all values that are "not null" from the desired feature classes and then passing those selected values to your new variables for calculation. Best of luck!

Derek Wood                                                                                                                                                                          Pasco County Public Works

0 Kudos