<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Data Expression with Groupby &amp;amp; Intersection in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/data-expression-with-groupby-amp-intersection/m-p/1309831#M53472</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1108"&gt;@XanderBakker&lt;/a&gt; or anyone else&lt;/P&gt;&lt;P&gt;I originally created a data expression that intersected a point layer with a polygon layer and returned a count of points per polygon and a sum of a field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Create FeatureSet for polygons
var boundaries = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    'a1795cc14f9744d68787f649a7865715',
    12,
    ['Type', 'DISTRICT'],
    true
    );
    
// Create Featureset for points
var nno = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    '8de09d32c583450584fe901ecaed1f6f',
    0,
    ['Survey_Year', 'Expected_Attendance'],
    true
    );


var outputDict = {'fields': [
    { 'name': 'Type', 'type': 'esriFieldTypeString'},
    { 'name': 'DISTRICT', 'type': 'esriFieldTypeString'},
    { 'name': 'NNO_Count', 'type': 'esriFieldTypeInteger'},
    { 'name': 'NNO_Attendance', 'type': 'esriFieldTypeInteger'}], 
    'geometryType': 'esriGeometryPolygon',
    'features': []
    };

var index = 0;

for (var boundary in boundaries) {
   var nnoint = Intersects(boundary,nno)
    var nnocount = 0
    var nnosum = 0
    for(var nnoi in nnoint) {
        nnocount ++
        nnosum += nnoi.Expected_Attendance
    }
   outputDict.features[index++] = { 
            'attributes': {
                'Type': boundary['Type'],
                'DISTRICT': boundary['DISTRICT'],
                'NNO_Count': nnocount,
                'NNO_Attendance': nnosum
        },
        'geometry': Geometry(boundary)
}}

return FeatureSet(Text(outputDict));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JerrySneary_0-1689805617920.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75812iE66BD537BD5BFFAF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JerrySneary_0-1689805617920.png" alt="JerrySneary_0-1689805617920.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Now I've been asked to split each of the counts and sums for each district by Survey_Year. I lost count of how many iterations I did on the above code but could never figure it out. I am now trying to use this code to accomplish my goal but I can't seem to figure it out, if I'm even going in the right direction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Create FeatureSet for polygons
var boundaries = FeatureSetByPortalItem(
  Portal('https://arcgis.com/'),
  'a1795cc14f9744d68787f649a7865715',
  12,
  ['Type', 'DISTRICT'],
  true
);

// Create Featureset for points
var nno = FeatureSetByPortalItem(
  Portal('https://arcgis.com/'),
  '8de09d32c583450584fe901ecaed1f6f',
  0,
  ['Survey_Year', 'Expected_Attendance'],
  true
);

// Perform spatial join and aggregation
var intersectedFeatures = Intersects(nno, boundaries);

var aggregatedFeatures = GroupBy(
  intersectedFeatures,
  ['Survey_Year', 'DISTRICT'],
  [{ name: 'NNO_Count', expression: 'intersectedFeatures', statistic: 'COUNT' },
   { name: 'Expected_Attendance', expression: 'Expected_Attendance', statistic: 'SUM'}]
);

// Return aggregated features as a FeatureSet
return aggregatedFeatures;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated.&lt;/P&gt;&lt;P&gt;Jerry&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2023 22:56:02 GMT</pubDate>
    <dc:creator>JerrySneary</dc:creator>
    <dc:date>2023-07-19T22:56:02Z</dc:date>
    <item>
      <title>Data Expression with Groupby &amp; Intersection</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/data-expression-with-groupby-amp-intersection/m-p/1309831#M53472</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1108"&gt;@XanderBakker&lt;/a&gt; or anyone else&lt;/P&gt;&lt;P&gt;I originally created a data expression that intersected a point layer with a polygon layer and returned a count of points per polygon and a sum of a field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Create FeatureSet for polygons
var boundaries = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    'a1795cc14f9744d68787f649a7865715',
    12,
    ['Type', 'DISTRICT'],
    true
    );
    
// Create Featureset for points
var nno = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    '8de09d32c583450584fe901ecaed1f6f',
    0,
    ['Survey_Year', 'Expected_Attendance'],
    true
    );


var outputDict = {'fields': [
    { 'name': 'Type', 'type': 'esriFieldTypeString'},
    { 'name': 'DISTRICT', 'type': 'esriFieldTypeString'},
    { 'name': 'NNO_Count', 'type': 'esriFieldTypeInteger'},
    { 'name': 'NNO_Attendance', 'type': 'esriFieldTypeInteger'}], 
    'geometryType': 'esriGeometryPolygon',
    'features': []
    };

var index = 0;

for (var boundary in boundaries) {
   var nnoint = Intersects(boundary,nno)
    var nnocount = 0
    var nnosum = 0
    for(var nnoi in nnoint) {
        nnocount ++
        nnosum += nnoi.Expected_Attendance
    }
   outputDict.features[index++] = { 
            'attributes': {
                'Type': boundary['Type'],
                'DISTRICT': boundary['DISTRICT'],
                'NNO_Count': nnocount,
                'NNO_Attendance': nnosum
        },
        'geometry': Geometry(boundary)
}}

return FeatureSet(Text(outputDict));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JerrySneary_0-1689805617920.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75812iE66BD537BD5BFFAF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JerrySneary_0-1689805617920.png" alt="JerrySneary_0-1689805617920.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Now I've been asked to split each of the counts and sums for each district by Survey_Year. I lost count of how many iterations I did on the above code but could never figure it out. I am now trying to use this code to accomplish my goal but I can't seem to figure it out, if I'm even going in the right direction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Create FeatureSet for polygons
var boundaries = FeatureSetByPortalItem(
  Portal('https://arcgis.com/'),
  'a1795cc14f9744d68787f649a7865715',
  12,
  ['Type', 'DISTRICT'],
  true
);

// Create Featureset for points
var nno = FeatureSetByPortalItem(
  Portal('https://arcgis.com/'),
  '8de09d32c583450584fe901ecaed1f6f',
  0,
  ['Survey_Year', 'Expected_Attendance'],
  true
);

// Perform spatial join and aggregation
var intersectedFeatures = Intersects(nno, boundaries);

var aggregatedFeatures = GroupBy(
  intersectedFeatures,
  ['Survey_Year', 'DISTRICT'],
  [{ name: 'NNO_Count', expression: 'intersectedFeatures', statistic: 'COUNT' },
   { name: 'Expected_Attendance', expression: 'Expected_Attendance', statistic: 'SUM'}]
);

// Return aggregated features as a FeatureSet
return aggregatedFeatures;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated.&lt;/P&gt;&lt;P&gt;Jerry&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 22:56:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/data-expression-with-groupby-amp-intersection/m-p/1309831#M53472</guid>
      <dc:creator>JerrySneary</dc:creator>
      <dc:date>2023-07-19T22:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Data Expression with Groupby &amp; Intersection</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/data-expression-with-groupby-amp-intersection/m-p/1310170#M53479</link>
      <description>&lt;P&gt;I figured it out. Just in case it could help someone else I'll paste the code below.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;// Create FeatureSet for polygons
var boundaries = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    'a1795cc14f9744d68787f649a7865715',
    12,
    ['Type', 'DISTRICT'],
    true
);

// Create Featureset for points
var nno = FeatureSetByPortalItem(
    Portal('https://arcgis.com/'),
    '8de09d32c583450584fe901ecaed1f6f',
    0,
    ['Survey_Year', 'Expected_Attendance'],
    true
);

var outputDict = {
    'fields': [
        { 'name': 'Survey_Year', 'type': 'esriFieldTypeString'},
        { 'name': 'Type', 'type': 'esriFieldTypeString'},
        { 'name': 'DISTRICT', 'type': 'esriFieldTypeString'},
        { 'name': 'NNO_Count', 'type': 'esriFieldTypeInteger'},
        { 'name': 'NNO_Attendance', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType': 'esriGeometryPolygon',
    'features': []
};

var index = 0;

for (var boundary in boundaries) {

    var district = boundary['DISTRICT'];
    var nnoint = Intersects(boundary, nno);

    // Create an object to store counts and sums by Survey_Year
    var surveyYearData = Dictionary();

    for (var nnoi in nnoint) {
        var surveyYear = nnoi.Survey_Year;
        var expectedAttendance = nnoi.Expected_Attendance;

        if (!HasKey(surveyYearData, surveyYear)) {
            var dictVars = Dictionary("count", 0, "sum", 0);
            surveyYearData[surveyYear] = dictVars;

        }

        surveyYearData[surveyYear].count++;
        surveyYearData[surveyYear].sum += expectedAttendance;
    }    

    // Add aggregated data to the output feature set
    for (var currYear in surveyYearData) {
        var currCount = surveyYearData[currYear].count;
        var currSum = surveyYearData[currYear].sum;

        outputDict.features[index++] = {
            'attributes': {
                'Survey_Year': currYear,
                'Type': boundary['Type'],
                'DISTRICT': district,
                'NNO_Count': currCount,
                'NNO_Attendance': currSum
            },
            'geometry': Geometry(boundary)
        };
    }
    
}
//return null;
return FeatureSet(Text(outputDict));&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 20 Jul 2023 18:19:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/data-expression-with-groupby-amp-intersection/m-p/1310170#M53479</guid>
      <dc:creator>JerrySneary</dc:creator>
      <dc:date>2023-07-20T18:19:24Z</dc:date>
    </item>
  </channel>
</rss>

