Select to view content in your preferred language

Arcade Data Expression GroupBy not grouping

351
2
Jump to solution
06-06-2024 11:26 AM
Labels (1)
frasertab
New Contributor II

Hello,

I have been trying to create a data expression for an AGOL dashboard that turns the values from a column ('Year') into their own columns, and then groups by another column ('Type') so that I have the counts for each year for that type.

So, if the original table looks like this:

TypeYear
Apple2022
Apple2022
Banana2023
Pear2024

 

I'm trying to create a table that looks like this:

Type202220232024
Apple200
Banana010
Pear001

 

I'm not new to scripting but I am new to Arcade, so I've been looking around and piecing together samples to get what I need, and I think I've almost got it. The code below structures the table the way I want it with the columns, but it's not grouping by Type the way I want it to. It's outputting the equivalent of this:

Type202220232024
Apple1nullnull
Apple1nullnull
Banananull1null
Pearnullnull1

 

Can anyone let me know where I'm going wrong in my GroupBy expression? Or is there a larger problem with my approach. Thank you!

var p = Portal('url');

var fs = FeatureSetByPortalItem(
    p,
    'id',
    0,
    [
        'Original_Year_Field',
        'Original_Type_Field'
    ],
    false
);

var yearDict = {'fields': [{'name':'Type', 'type':'esriFieldTypeString'},
                           {'name':'2022', 'type':'esriFieldTypeInteger'},
                           {'name':'2023', 'type':'esriFieldTypeInteger'},
                           {'name':'2024', 'type':'esriFieldTypeInteger'}],
                'geometryType': '',
                'features': []};

for (var feature in fs) {
    var cat = feature['Original_Type_Field']
    var yearone = Iif(feature['Original_Year_Field'] == 2022,1,null)
    var yeartwo = Iif(feature['Original_Year_Field'] == 2023,1,null)
    var yearthree = Iif(feature['Original_Year_Field'] == 2024,1,null)
    Push(yearDict.features, {"attributes": {"Type": cat,
                                            "2022": yearone,
                                            "2023": yeartwo,
                                            "2024": yearthree}})
}

var fs_dict = FeatureSet(Text(yearDict));
return fs_dict

return GroupBy(fs_dict, ['Type'],
       [{ name: '2022', expression: '2022', statistic: 'COUNT' },
        { name: '2023', expression: '2023', statistic: 'COUNT' },
        { name: '2024', expression: '2024', statistic: 'COUNT' }]);
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Your return at line 33 means the GroupBy function didn't run.

This sample script gives the expected table. One thing I had to change to make it work properly is to have the fields in the Dictionary start with a string.

var fs = FeatureSet(
  {"fields":[
      {"alias":"Type","name":"Type","type":"esriFieldTypeString"},
      {"alias":"Year","name":"Year","type":"esriFieldTypeInteger"}],
      "spatialReference":{"wkid":4326},
      "geometryType":"",
      "features":[
        {"geometry":'', "attributes":{"Type":'Apple',"Year":2022}},
        {"geometry":'', "attributes":{"Type":'Apple',"Year":2022}},
        {"geometry":'', "attributes":{"Type":'Banana',"Year":2023}},
        {"geometry":'', "attributes":{"Type":'Pear',"Year":2024}},
    ]
  }
);

var yearDict = {'fields': [{'name':'Type', 'type':'esriFieldTypeString'},
                           {'name':'y2022', 'type':'esriFieldTypeInteger'},
                           {'name':'y2023', 'type':'esriFieldTypeInteger'},
                           {'name':'y2024', 'type':'esriFieldTypeInteger'}],
                'geometryType': '',
                'features': []};

for (var feature in fs) {
    var cat = feature['Type']
    var yearone = Iif(feature['Year'] == 2022,1,null)
    var yeartwo = Iif(feature['Year'] == 2023,1,null)
    var yearthree = Iif(feature['Year'] == 2024,1,null)
    Push(yearDict.features, {"attributes": {"Type": cat,
                                            "y2022": yearone,
                                            "y2023": yeartwo,
                                            "y2024": yearthree}})
}

var fs_dict = FeatureSet(Text(yearDict));
//return fs_dict

return GroupBy(fs_dict, ['Type'],
       [{ name: '2022', expression: 'y2022', statistic: 'Count' },
        { name: '2023', expression: 'y2023', statistic: 'Count' },
        { name: '2024', expression: 'y2024', statistic: 'Count' }]);

 

 

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

Your return at line 33 means the GroupBy function didn't run.

This sample script gives the expected table. One thing I had to change to make it work properly is to have the fields in the Dictionary start with a string.

var fs = FeatureSet(
  {"fields":[
      {"alias":"Type","name":"Type","type":"esriFieldTypeString"},
      {"alias":"Year","name":"Year","type":"esriFieldTypeInteger"}],
      "spatialReference":{"wkid":4326},
      "geometryType":"",
      "features":[
        {"geometry":'', "attributes":{"Type":'Apple',"Year":2022}},
        {"geometry":'', "attributes":{"Type":'Apple',"Year":2022}},
        {"geometry":'', "attributes":{"Type":'Banana',"Year":2023}},
        {"geometry":'', "attributes":{"Type":'Pear',"Year":2024}},
    ]
  }
);

var yearDict = {'fields': [{'name':'Type', 'type':'esriFieldTypeString'},
                           {'name':'y2022', 'type':'esriFieldTypeInteger'},
                           {'name':'y2023', 'type':'esriFieldTypeInteger'},
                           {'name':'y2024', 'type':'esriFieldTypeInteger'}],
                'geometryType': '',
                'features': []};

for (var feature in fs) {
    var cat = feature['Type']
    var yearone = Iif(feature['Year'] == 2022,1,null)
    var yeartwo = Iif(feature['Year'] == 2023,1,null)
    var yearthree = Iif(feature['Year'] == 2024,1,null)
    Push(yearDict.features, {"attributes": {"Type": cat,
                                            "y2022": yearone,
                                            "y2023": yeartwo,
                                            "y2024": yearthree}})
}

var fs_dict = FeatureSet(Text(yearDict));
//return fs_dict

return GroupBy(fs_dict, ['Type'],
       [{ name: '2022', expression: 'y2022', statistic: 'Count' },
        { name: '2023', expression: 'y2023', statistic: 'Count' },
        { name: '2024', expression: 'y2024', statistic: 'Count' }]);

 

 

0 Kudos
frasertab
New Contributor II

Thank you! I figured I was probably down to some syntax errors, but you've saved me hours of tinkering with it. I have no idea how long it would have taken me to realize not having a letter in the field name was causing issues. Thanks again!

0 Kudos