Can I use Arcade to average values from many attribute columns?

10-16-2019 09:44 AM
Occasional Contributor

I have a polygon layer that I want to label with the average yield over time. The data table is set up so that there is a polygon ID and then historic yields for each year in its own attribute column. 

For Example:

Polygon Num            yield 2010      yield 2011      yield 2012      yield 2013

1                                       10               26                     29                     15

2                                       35               45                     19                      25

3                                       14                25                     36                     48

How can I label the polygon, or create an average attribute in the pop-up, with the average yield for all years?

Thank you!

Tags (1)
0 Kudos
1 Reply
New Contributor III

Yes. You need to create an expression in the Configure Pop Up menu. Then at the bottom, add an expression. 

In that window, you can use the items on the right side to dive in to fields. 

to get the value of a columns you use $feature.name_of_field

your expression will be something like:

var yields = $feature.Yield2010 + $feature.Yield2011 + $feature.Yield2012 + $feature.Yield2013

var average = yields / 4

return average

I am not sure how you would make it more flexible to do things like not count blank years. 

I bet there is a function in Arcade for that.. but I just wrote this post so use that function builder and see if it has something like AVERAGE(field1, field2)


Average is the function they use for demonstrating Arcade.... i've seen it hundreds of times and never really read it. 

// Write a script to return a value to show in the pop-up.
// For example, get the average of 4 fields:
// Average($feature.SalesQ1, $feature.SalesQ2, $feature.SalesQ3, $feature.SalesQ4)
Average(values, fieldName?)