Hi,
I am looking for an arcade expression that groups (Groupby) by a field (ano/year) the sum of two fields (area_ha and area_km2) and that returns a field calculating the division of those sums.
In summary I want to combine two columns of a layer, dividing the sum of each one with each othe.
Fields: Ano (years), Area_ha and Area_km2
Code:
var p = 'https://www.arcgis.com'
var itemId = 'e5f830354f5a4542b5ef6ad63da94d80'
var fs = FeatureSetByPortalItem(Portal(p), itemId, 0, ['*'], false)
var GroupBy(fs, ['Ano'],
[ { name: 'sum_area_ha',
expression: 'Area_ha',
statistic: 'SUM' },
{ name: 'sum_area_km2',
expression: 'Area_km2',
statistic: 'SUM' },
])
Return GroupBy (sum_ano / sum Perimeter)
Solved! Go to Solution.
I'm not quite sure what you're trying to achieve, because
But, assuming you want to get the sums of two fields grouped by a third field, and then divide those sums for each group:
// load your data
var d = {
geometryType:"",
fields:[
{name:"GroupField", type:"esriFieldTypeInteger"},
{name:"Field1", type:"esriFieldTypeDouble"},
{name:"Field2", type:"esriFieldTypeDouble"},
],
features:[]
}
for(var i = 0; i < 20; i++) {
var f = {attributes: {GroupField: Round(Random()*10, 0), Field1: Random(), Field2: Random()}}
Push(d.features, f)
}
var fs = Featureset(Text(d))
//return fs
// get the sums grouped by a field
var fs_sums = GroupBy(fs, ["GroupField"], [
{name: "sum_field_1", expression: "Field1", statistic: "SUM"},
{name: "sum_field_2", expression: "Field2", statistic: "SUM"},
])
// use group by again, grouping by the same field, but using another expression
var fs_quotient = GroupBy(fs_sums, ["GroupField"], [
{name: "sum_field_1", expression: "sum_field_1", statistic: "SUM"},
{name: "sum_field_2", expression: "sum_field_2", statistic: "SUM"},
{name: "quotient", expression: "sum_field_1 / sum_field_2", statistic: "SUM"},
])
return fs_quotient
I'm not quite sure what you're trying to achieve, because
But, assuming you want to get the sums of two fields grouped by a third field, and then divide those sums for each group:
// load your data
var d = {
geometryType:"",
fields:[
{name:"GroupField", type:"esriFieldTypeInteger"},
{name:"Field1", type:"esriFieldTypeDouble"},
{name:"Field2", type:"esriFieldTypeDouble"},
],
features:[]
}
for(var i = 0; i < 20; i++) {
var f = {attributes: {GroupField: Round(Random()*10, 0), Field1: Random(), Field2: Random()}}
Push(d.features, f)
}
var fs = Featureset(Text(d))
//return fs
// get the sums grouped by a field
var fs_sums = GroupBy(fs, ["GroupField"], [
{name: "sum_field_1", expression: "Field1", statistic: "SUM"},
{name: "sum_field_2", expression: "Field2", statistic: "SUM"},
])
// use group by again, grouping by the same field, but using another expression
var fs_quotient = GroupBy(fs_sums, ["GroupField"], [
{name: "sum_field_1", expression: "sum_field_1", statistic: "SUM"},
{name: "sum_field_2", expression: "sum_field_2", statistic: "SUM"},
{name: "quotient", expression: "sum_field_1 / sum_field_2", statistic: "SUM"},
])
return fs_quotient