Concatenate fields in dashboard data expression

533
4
Jump to solution
05-17-2021 11:21 AM
Labels (1)
SFM_TravisBott
New Contributor III

I'm having trouble with the workflow and syntax for data expressions in a dashboard. I have two fields and I would like to be able to create dashboard elements from a concatenation of those two fields. Both fields are text and have domains. 

I understand the basics to create a pop-up or labels from this concatenation in Arcade, but do not see how to translate that into a feature set/data expression. 

Basic concatenation: 

Concatenate([$feature["Field_1"], $feature["Field_2"]], ' ')

 

To start setting up the data expression...

// Set variables
var portal = Portal('https://MyPortral.maps.arcgis.com/');
var itemId = 'itemId';
var layerId = 0;

var fs = FeatureSetByPortalItem(portal, itemId, layerId, ['*'], false);

 

....and now what is the appropriate workflow?  Attempt to call fields and concatenate/count? Generate a dictionary of concatenated values and count? Documentation seems pretty skinny as this is fairly new. 

0 Kudos
1 Solution

Accepted Solutions
VivekMalleshappa
Esri Contributor

Hi @SFM_TravisBott 

Here's are two options for concatenating strings in data expressions: 

Option 1: In the Arcade GroupBy() function by specifying the SQL CONCAT() function to create a string in the group by field: 

 

 

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'07945c22c9bc497f9489f97e6203de3c',0,['Division', 'Battalion'], false)

var concatExpr = "CONCAT(Division, ', Division ', Battalion)"
return GroupBy(fs, [concatExpr], [ {name: 'Total', expression: 'DispatchTimeSecs', statistic: 'Average' }])

 

Option 2: Using Arcade's Concatenate() function: 

 

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'07945c22c9bc497f9489f97e6203de3c',0,['Address', 'City', 'Problem'], false)

// Dictionary to hold concatenated strings.
var concatDict = {
    'fields': 
        [{ 'name': 'concat_str', 
           'type': 'esriFieldTypeString'
        }, 
        { 'name': 'problem', 
           'type': 'esriFieldTypeString'
        }], 
        'geometryType': '', 'features': []}; 
                   
var index = 0; 

// Concatenate Address and City fields for each feature.   
for (var feature in fs) { 
    concatDict.features[index++] = { 
            'attributes': { 
                'concat_str': Concatenate(feature['Address'],', ', feature['City']), 
                'problem': feature['Problem']
            }
} }

// Return dictionary as a featureSet. 
return FeatureSet(Text(concatDict)); 

 

The first option allows you created group by statistics on concatenated fields. The second options allows you retain feature level data, in case collapsing data by groups is not intended. 

Hope this helps! If there are specific use cases or additional documentation you think could help, please let us know. 

View solution in original post

Tags (1)
4 Replies
DavidPike
MVP Frequent Contributor

I'm no expert on these, but I've only had luck reconstructing a new FeatureSet by creating a dictionary/JSON and iterating over the existing FeatureSet and inserting the old values and newly calculate values.

A few examples here (last one is probably the most relevant) - Create effective data expressions—ArcGIS Dashboards | Documentation

Agree that it's a bit of an opaque utility at the moment.

0 Kudos
SFM_TravisBott
New Contributor III

Thanks, @DavidPike . Perhaps one of the Arcade blog authors (e.g. @VivekMalleshappa) might chime in. 

All of the examples I've been able to find seem to be the same...the code samples presented in the documentation page you linked are the same as those presented in the blog, which are the same as those presented on GitHub as possible examples. I am pretty novice so I often need to do quite a bit of triangulation before I can get code right. 

 

0 Kudos
VivekMalleshappa
Esri Contributor

Hi @SFM_TravisBott 

Here's are two options for concatenating strings in data expressions: 

Option 1: In the Arcade GroupBy() function by specifying the SQL CONCAT() function to create a string in the group by field: 

 

 

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'07945c22c9bc497f9489f97e6203de3c',0,['Division', 'Battalion'], false)

var concatExpr = "CONCAT(Division, ', Division ', Battalion)"
return GroupBy(fs, [concatExpr], [ {name: 'Total', expression: 'DispatchTimeSecs', statistic: 'Average' }])

 

Option 2: Using Arcade's Concatenate() function: 

 

var fs = FeatureSetByPortalItem(Portal('https://arcgis.com'),'07945c22c9bc497f9489f97e6203de3c',0,['Address', 'City', 'Problem'], false)

// Dictionary to hold concatenated strings.
var concatDict = {
    'fields': 
        [{ 'name': 'concat_str', 
           'type': 'esriFieldTypeString'
        }, 
        { 'name': 'problem', 
           'type': 'esriFieldTypeString'
        }], 
        'geometryType': '', 'features': []}; 
                   
var index = 0; 

// Concatenate Address and City fields for each feature.   
for (var feature in fs) { 
    concatDict.features[index++] = { 
            'attributes': { 
                'concat_str': Concatenate(feature['Address'],', ', feature['City']), 
                'problem': feature['Problem']
            }
} }

// Return dictionary as a featureSet. 
return FeatureSet(Text(concatDict)); 

 

The first option allows you created group by statistics on concatenated fields. The second options allows you retain feature level data, in case collapsing data by groups is not intended. 

Hope this helps! If there are specific use cases or additional documentation you think could help, please let us know. 

View solution in original post

Tags (1)
SFM_TravisBott
New Contributor III

Thanks, Vivek!

This worked perfectly. I chose the second option, and it didn't take long for me to adapt it to my dataset. 

Now for whatever reason the chart I'm creating won't show up in color....but we'll wait on that.