Arcade expression to calculate a division

1639
1
Jump to solution
10-20-2022 05:02 PM
Labels (3)
SantiagoAvendaño
New Contributor III

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)

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

I'm not quite sure what you're trying to achieve, because

  • sum_ano and sum_perimeter aren't defined in your code
  • assuming Area_ha and Area_km2 area the area values of the same polygon, the result will be 100, as that's the conversion factor between ha and km²

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

 

JohannesLindner_0-1666331620354.png

 


Have a great day!
Johannes

View solution in original post

1 Reply
JohannesLindner
MVP Frequent Contributor

I'm not quite sure what you're trying to achieve, because

  • sum_ano and sum_perimeter aren't defined in your code
  • assuming Area_ha and Area_km2 area the area values of the same polygon, the result will be 100, as that's the conversion factor between ha and km²

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

 

JohannesLindner_0-1666331620354.png

 


Have a great day!
Johannes