How group or aggregate all other respond which come from Survey123 to one bar chart?

202
2
11-04-2021 12:46 PM
anonymous
Occasional Contributor

Hello,

I am trying to aggregate all answers which user choose other and type his/her respond on the box in survey123. I just want to show how many other respond but I don't want to see actual answers.
you can see below a screen shot which i couldn't find the way and I just want to have one bar for all other together.

ARM_1-1636054858366.png

I have this arcade code which group each respond but I don't know how can I add other to.

 

// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 
'itemID' , 0, 
['neighborhood'], false);

// Empty dictionary to capture each hazard reported as separate rows. 
var choicesDict = {'fields': [{ 'name': 'split_choices', 'type': 'esriFieldTypeString'}], 
                    'geometryType': '', 'features': []}; 

var index = 0; 

// Split comma separated hazard types and store in dictionary.  
for (var feature in fs) { 
    var split_array  =  Split(feature["neighborhood"], ',') 
    var count_arr = Count(split_array) 
    for(var i = 0; i < count_arr; i++ ){ 
        choicesDict.features[index++] = { 
            'attributes': { 'split_choices': Trim(split_array[i]),  
            }
}}} 

// Convert dictionary to featureSet. 
var fs_dict = FeatureSet(Text(choicesDict)); 

// Return featureset after grouping by hazard types. 
return GroupBy(fs_dict, ['split_choices'], 
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);  

 



 

0 Kudos
2 Replies
jcarlson
MVP Honored Contributor

I would assume from your post that this is coming from a survey? How are these "other" values getting into the `neighborhood` field? Are you concatenating choices from the survey?

Anyway, we can group all the "other" responses by using a list and matching against it. The Data Expression examples have yet to be updated to include this, but recent updates to the Arcade language enable us to use Push to populate an array, rather than using an index variable.

Try something like this:

// Get layer
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 
'itemID' , 0, 
['neighborhood'], false);

// Define list of expected values
var categories = [
    'Agressive Drivers',
    'etc',
    'etc'
]

// Empty feature array
var features = []

// Split choices, populate feature array
for (var feature in fs) {
    var split_array  =  Split(feature["neighborhood"], ',') 
    var count_arr = Count(split_array) 

    for(var i = 0; i < count_arr; i++ ){ 
        var cat = Trim(split_array[i])

        if(Includes(categories, cat)){
            var feat = {'attributes': {'split_choices': cat}}
        } else {
            var feat = {'attributes': {'split_choices': 'Other'}}
        }

        Push(features, feat)
    }
}

// EDictionary w/ features
var choicesDict = {'fields': [{ 'name': 'split_choices', 'type': 'esriFieldTypeString'}], 
                    'geometryType': '', 'features': features}; 

// Convert dictionary to featureSet. 
var fs_dict = FeatureSet(Text(choicesDict)); 

// Return featureset after grouping by hazard types. 
return GroupBy(fs_dict, ['split_choices'], 
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);

 

Do note, however, that the final "GroupBy" isn't necessary if the chart is all you need this for; the chart can handle the summing for you.

- Josh Carlson
Kendall County GIS
anonymous
Occasional Contributor

Hey @jcarlson Thanks for this
this is great but I know some arcade code doesn't allow you to create actions. is it normal or I need to change the code to enable Action.

0 Kudos