Select to view content in your preferred language

Calculate feature count based on a certain field in arcade

492
2
06-09-2023 08:26 AM
wizgis
by
Occasional Contributor II

I have a hosted feature layer in my portal in which I have a field named district and it has records like A, A, A, B, C, C. I essentially want to get total count of A, B, C and display it as label. 

I understand this can be achieved using Count & Filter function of Arcade however, am on ArcGIS Enterprise 10.9.1 in which Filter function is not present.

As an alternate I have tried the following code: 

 

 

var countf = 0
var test = $feature.districts
//var test1 = Split(test, ",")

for (var i = 0; i < test; i++){
    if (test[i] == $feature.districts[i]){
        countf = countf + 1
    }
};

 

 

The code is not giving me any error however, am not getting correct result. I believe I am missing out on the syntax. 

Any insights would be helpful.

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

Something like this?

// split the field into single districts
var districts = Split($feature.districts, ", ")

// get a count for each district
var counts = Dictionary()
for(var i in districts) {
  var d = districts[i]
  counts[d] = IIf(HasKey(counts, d), counts[d], 0) + 1
}

// combine districts and counts
var output = []
for(var d in counts) {
    Push(output, counts[d] + " * " + d)
}

// return
return Concatenate(output, ", ")

 

JohannesLindner_0-1686346459822.png

 


Have a great day!
Johannes
wizgis
by
Occasional Contributor II

Thank you @JohannesLindner for sharing this. I'll take a look at it.

0 Kudos