Select to view content in your preferred language

Arcade Expressions & Maths returning wrong values

1441
6
Jump to solution
08-21-2023 06:36 AM
AndrewReynoldsDevon
Regular Contributor

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

1 Solution

Accepted Solutions
AndrewReynoldsDevon
Regular Contributor

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? 

View solution in original post

0 Kudos
6 Replies
ClayDonaldsonSWCA
Frequent Contributor

I would try wrapping each field in Number(). What data type are the two fields?

(Number($feature.PassengersOutward) + Number($feature.Passengerslnward)) / 2

AndrewReynoldsDevon
Regular Contributor

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

 

0 Kudos
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
AndrewReynoldsDevon
Regular Contributor

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? 

0 Kudos
KenBuja
MVP Esteemed Contributor

You should use Float. Take a look at the documentation about field data types.

0 Kudos
JohannesLindner
MVP Alum

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

Have a great day!
Johannes
0 Kudos