Distinct count issue using list element

449
2
Jump to solution
01-04-2022 09:55 PM
SayedWali
New Contributor III

Using the List Element I am taking multiple layers and out of that, I am going to count the number of reportIDs per Province to know how many monitoring visits were carried out in each province. But looks like it doesn't count properly and the province is not listed uniquely. With my very light knowledge of Arcade I can't figure it out, any help will be appreciated:

SayedWali_0-1641361325751.png

 

 

var portal = Portal('https://www.arcgis.com/');
// Group by province
// Count report IDs


var HMAFAR = GroupBy(
FeatureSetByPortalItem(portal,'56db417ebbc541bcbb6ccf9c49524cc9',0,['*'],false),
['province'],
[
{ name: 'FAR_ID', expression: 'rpt_id', statistic: 'COUNT' },
],

);


var HMAPDIA = GroupBy(
FeatureSetByPortalItem(portal,'cd991db8686d4d0b8c9a6c0e910f03ab',0,['*'],false),
['field_8'],
[
{ name: 'PDIA_ID', expression: 'field_1', statistic: 'COUNT' },
]
);

var MRE = GroupBy(
FeatureSetByPortalItem(portal,'3e63815f693a4d0888209d58ebe7fe8b',0,['*'],false),
['field_8'],
[{ name: 'MRE_ID', expression: 'field_1', statistic: 'COUNT' },

]
);

var NTS = GroupBy(
FeatureSetByPortalItem(portal,'30b595fc7ef64bb596bebcbbffb68db6',0,['*'],false),
['province'],
[
{ name: 'NTS_ID', expression: 'repid', statistic: 'COUNT' },

]
);

var FHMAML = GroupBy(
FeatureSetByPortalItem(portal,'4861c1d8d407434193a012b504b69b36',0,['*'],false),
['field_8'],
[
{ name: 'HMAML_ID', expression: 'field_1', statistic: 'COUNT' },

]
);

var HMAML = filter(FHMAML, "ml_type = 'HMA ML'");

var VA = GroupBy(
FeatureSetByPortalItem(portal,'fbaf2ac4f8bc413c8770f7aaf4e77c4d',0,['*'],false),
['province'],
[
{ name: 'VA_ID', expression: 'rpt_id', statistic: 'COUNT' },

]
);

var layersDict = {
fields: [
{ name: 'Reports_Type', type: 'esriFieldTypeString' },

{ name: 'count_of_ids', type: 'esriFieldTypeInteger' },

{ name: 'provinces', type: 'esriFieldTypeString'},
],

geometryType: '',
features: [],
};

// Loop through each of the six FeatureSets and store attributes into a combined dictionary.
var i = 0;
for (var f in HMAFAR) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'HMAFAR',

count_of_ids: sum(f['FAR_ID']),
provinces: f['province'],
},
};
i++;
}

for (var p in HMAPDIA) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'HMAPDIA',

count_of_ids: sum(p['PDIA_ID']),
provinces: p['field_8'],
},
};
i++;
}

for (var m in MRE) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'MRE',

count_of_ids: sum(m['MRE_ID']),
provinces: m['field_8'],
},
};
i++;
}

for (var n in NTS) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'NTS',

count_of_ids: sum(n['NTS_ID']),
provinces: n['province'],
},
};
i++;
}

for (var ml in HMAML) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'HMAML',

count_of_ids: sum(ml['HMAML_ID']),
provinces: ml['field_8'],
},
};
i++;
}

for (var v in VA) {
layersDict.features[i] = {
attributes: {
Reports_Type: 'VA',

count_of_ids: sum(v['VA_ID']),
provinces: v['province'],
},
};
i++;
}

// Return dictionary 
return FeatureSet(Text(layersDict));

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You have to do a final GroupBy on layersDict to get the sum of all counts.

 

// do a final GroupBy to add all reports in each province
var layers_fs = FeatureSet(Text(layersDict))
return GroupBy(
    layers_fs,
    ['provinces'],
    {name: 'TotalReportCount', expression: 'count_of_ids', statistic: 'SUM'}
    )

 

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

You have to do a final GroupBy on layersDict to get the sum of all counts.

 

// do a final GroupBy to add all reports in each province
var layers_fs = FeatureSet(Text(layersDict))
return GroupBy(
    layers_fs,
    ['provinces'],
    {name: 'TotalReportCount', expression: 'count_of_ids', statistic: 'SUM'}
    )

 

 


Have a great day!
Johannes
SayedWali
New Contributor III

Thank you! worked out

0 Kudos