Using Arcade GroupBy in the Field Calculator

1524
4
Jump to solution
11-15-2021 08:52 AM
MarilynLong1
New Contributor II

I have a column called 'name' with trail names that repeat sometimes and a column called 'sec_length_ft' with the length of each trail segment. I want to use GroupBy to add up the sections of the same trail name to populate the 'trail_ft' column. The code I've come up with is below. It runs without errors, but the table doesn't update.

 

GroupBy(FeatureSetByName($datastore, 'TrailLinesQAQC'),'name',
{name: 'trail_ft', expression: 'sec_length_ft', statistic: 'SUM'})

 

TrailLinesQAQC is the name of the layer

Trail_ft is the column I want to be updated

name is the names of the trails that I want to group the data by

sec_length_ft is the name of the column that contains values of trail segments to be added up for the trail_ft column

 

Thanks for your help.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
// load the feature set
var fs = FeatureSetByName($datastore, 'TrailLinesQAQC')
// filter for name
var name = $feature.name
var filtered_fs = Filter(fs, "name = @name")
// get sum of sec_length_ft
return Sum(filtered_fs, "sec_length_ft")

 

This should work, too:

// do it with GroupBy
var group = GroupBy(FeatureSetByName($datastore, 'TrailLinesQAQC'),'name',
{name: 'trail_ft', expression: 'sec_length_ft', statistic: 'SUM'})

// you still have to filter it
var name = $feature.name
var filtered_group = Filter(group, "name = @name")
return First(filtered_group).trail_ft

Have a great day!
Johannes

View solution in original post

0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor
// load the feature set
var fs = FeatureSetByName($datastore, 'TrailLinesQAQC')
// filter for name
var name = $feature.name
var filtered_fs = Filter(fs, "name = @name")
// get sum of sec_length_ft
return Sum(filtered_fs, "sec_length_ft")

 

This should work, too:

// do it with GroupBy
var group = GroupBy(FeatureSetByName($datastore, 'TrailLinesQAQC'),'name',
{name: 'trail_ft', expression: 'sec_length_ft', statistic: 'SUM'})

// you still have to filter it
var name = $feature.name
var filtered_group = Filter(group, "name = @name")
return First(filtered_group).trail_ft

Have a great day!
Johannes
0 Kudos
MarilynLong1
New Contributor II

Thank you so much Johannes! You saved me so many hours of work!

0 Kudos
JohannesLindner
MVP Frequent Contributor

Glad to have helped. Please accept the answer so that the question is marked as solved.


Have a great day!
Johannes
0 Kudos
JeanineEGS
New Contributor III

is there an example of GroupBy for the calculate field in Arc Pro where the layer and field name  are already  designated ? 

i.e what goes in the expression box? thanks

Tags (1)