Hi,
today I got stuck writing a data expression that calculates the percentage of several categories based on the frequency of their mentioning in a survey.
To illustrate, I have a survey that allows the user to give multiple topics he would like to know more about in future trainings. These are split by the data expression into seperate rows in order to count them.
var fv = FeatureSetByPortalItem(Portal(p),itemID,layerID,
['future_support'],
false );
var fv_f = Filter(fv,'future_support IS NOT NULL')
var dict = {
fields: [
{ name: 'split_choices', type: 'esriFieldTypeString' },
],
geometryType: '',
features: [],
};
var index = 0;
for (var feature in fv_f){
var split_fv_f = Split(feature['future_support'],',')
var count_arr = Count(split_fv_f)
for(var i = 0; i < count_arr; i++ ){
dict.features[index++] = {
'attributes': {
'split_choices': decode(Number(Trim(split_fv_f[i])),
1,'Pruning & Rejuvenation',
2,'Composting',
3,'Integrated Pest & Disease Management',
4,'Weeding ',
5,'Nutrition',
6,'Shade',
7,'Mulching',
8,'Erosion control',
9,'Coffee planting',
10,'Harvesting',
11,'Sustainability',
12,'Business Skills',
99,'None',
'other')
}}
}}
var fs_dict = FeatureSet(Text(dict));
This is all fine, in the next step I would like to group and count per given answer. Easy task, just use the GroupBy() function:
return GroupBy(fs_dict, ['split_choices'],
[{ name: 'split_count',
expression: 'split_choices',
statistic: 'COUNT' }]);
Which gives me the following:
Based on this, I can create a nice graph showing these counts, but I cannot tell the percentage of total per question. The total is easly calcuated as variable by counting the filtered result of fv_f. So I would basically like to perform per each row in the above picture an additional calculation to get the percentage.
I tried to use the GroupBy() FS I get in another for-loop but that didn't seem to work, as I am not able to reference the fields correctly, namely split_count.
Any ideas?
All the best,
merlin
---