I'm using Arcade Expressions to add up to fields of data Field A + Field B then divide by 2.
So far the expression works for all data apart from when Field A or Field B contain a 0 then all I get as an output is 0 when actually it should be a value.
I'm following the standard rules of BODMAS so I have (Field A + Field B) /2 which when I verify is a valid formula so don't know why this is happening.
See attached snip to see the formula
Solved! Go to Solution.
The other values are 0 so the answer should be 0.5 so the data type of the field must be the issue. I'm using model builder to add the field before the calculation. What option should I choose from the data type drop down so that the above problem is resolved?
I would try wrapping each field in Number(). What data type are the two fields?
(Number($feature.PassengersOutward) + Number($feature.Passengerslnward)) / 2
I tried the above but I'm still getting calculated values of 0 where one of the data values is 0.
The datatypes are doubles with numeric formatting. The output of the Arcade expression is data type 'Long'. There are 29 records out of 1900 that have a 0 value in one of the fields so the others have data values greater than 1 & the equation is working ok for those.
See the code below
(Number($feature.PassengersOutward) + Number($feature.PassengersInward)) /2
What is the value in the other field for those 29 records? If it's 1, the resulting value would be 0.5, which, in a long integer field, would show as 0.
You should use Float. Take a look at the documentation about field data types.
I can't reproduce the error, but you might want to try these:
// your output field is an integer, try rounding
Round(($feature.PassengersOutward + $feature.Passengerslnward) / 2)
// try using the Mean() function
Mean([$feature.PassengersOutward, $feature.Passengerslnward])
// try both
Round(Mean([$feature.PassengersOutward, $feature.Passengerslnward]))