Arcade Split comma delimited value into multiple rows, keeping row details

2404
4
Jump to solution
10-07-2021 06:53 AM
VeronicaArambula
New Contributor II

Hello, 

I'm new in working with Arcade expressions in the Dashboard.

What I want is to split the values in the 'split' field which are separated by commas while preserving the original ID of each value. How could I do this? I

 

 

 

// Split values corresponding to Action
// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 'a616dac6c1834fedb0fad1860c14234e' , 1, ['*'], false);

// Empty dictionary to capture each hazard reported as separate rows. 
var choicesDict = {'fields': [{ 'name': 'split', 'type': 'esriFieldTypeString'}, { 'name': 'ID', '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["Action"], ',') 
    var count_arr = Count(split_array) 
    for(var i = 0; i < count_arr; i++ ){ 
        choicesDict.features[index++] = { 
            'attributes': { 'split': Trim(split_array[i])
            }
            }
        }
        for(var i = 0; i < count_arr; i++){
            choicesDict.features[i++] = {
                attributes: {
                    ID: feature["ID_bins"]
                }
            }
            
        }
}

var result = Replace(choicesDict, "_", " ");

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

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

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
// Split values corresponding to Action
// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 'a616dac6c1834fedb0fad1860c14234e' , 1, ['*'], false);

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

// Split comma separated hazard types and store in dictionary.
for (var feature in fs) {
  var ID = feature["ID_bins"]
  var split_array  =  Split(feature["Action"], ',')
  for(var i in split_array) {
    Push(choicesDict.features, {"attributes": {"ID": ID, "split": Trim(split_array[i])}}
  }
}
var result = Replace(choicesDict, "_", " ");

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

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

Have a great day!
Johannes

View solution in original post

4 Replies
JohannesLindner
MVP Frequent Contributor
// Split values corresponding to Action
// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 'a616dac6c1834fedb0fad1860c14234e' , 1, ['*'], false);

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

// Split comma separated hazard types and store in dictionary.
for (var feature in fs) {
  var ID = feature["ID_bins"]
  var split_array  =  Split(feature["Action"], ',')
  for(var i in split_array) {
    Push(choicesDict.features, {"attributes": {"ID": ID, "split": Trim(split_array[i])}}
  }
}
var result = Replace(choicesDict, "_", " ");

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

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

Have a great day!
Johannes
VeronicaArambula
New Contributor II

Thank you very much for your reply! The code works perfectly fine I only had to add 'ID' at the end.

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

 

0 Kudos
BNix_TPA
New Contributor III

Hi Veronica,

I'm trying to do basically the same thing, but add two more fields so I can filter my Series Chart further.  One for data and another attribute field.  I enter the above solution and enter your edit and receive an error on line 17.  Would you be able to chare the code you used?

 

Thanks!

ArielLow2146
Occasional Contributor II

Based on what Johannes provided I was able to get the following to work:

// Split values corresponding to Sub Topic
// Reference layer using the FeatureSetByPortalItem() method. 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), '404e68025b454f829385dfaa326d0486' , 0, ["*"], false);

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

// Split comma separated sub types and store in dictionary.
for (var f in fs) {
  var Topic = f["Topic"]
  var split_array  =  Split(f["Sub_Topic"], ',')
  for(var i in split_array) {
    Push(choicesDict.features, {"attributes": {"Topic": Topic, "split": Trim(split_array[i])}})
  }
}
var result = Replace(choicesDict, "_", " ");

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

// Return featureset after grouping by sub topic 
//return GroupBy(fs_dict, ['split'],
//       [{ name: 'No', expression: 'split', statistic: 'COUNT' }]);