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

710
2
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 Regular 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

2 Replies
JohannesLindner
MVP Regular 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