Select to view content in your preferred language

GrouBy Data Expression

999
11
Jump to solution
03-04-2024 08:57 AM
Labels (1)
JanelleShank
Emerging Contributor

Good Afternoon, All!

I have a dataset of voting results and I can trying to create a data expression that returns the MAX value of registered voters within each district and then add the total of each MAX value to find the total number of those registered. I have written a data expression using the GoupBy function, however, it is returning a null value. I have provided examples of my data expression and data below. 

JanelleShank_0-1709571392930.png

 

var fs = FeaturesetByPortalItem(Portal("https://bedfordplanning.maps.arcgis.com"), "78eb292ce436450b85dc249bd3069741", 0, ["Precinct_name", "Reg_voters"], false)
var out_dict = {
  fields: [
    {name: "Precinct_Name", type: "esriFieldTypeString"},
    {name: "Reg_voters", type: "esriFieldTypeString"}
  ],
  features: [],
  geometryType: ""
}
var maxvotes = GroupBy("out_dict", 'Precinct_Name', {name: 'Reg_voters', expression: 'Reg_Voters', statistic: "MAX"})
 
Thank you,
Janelle
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Since your data is private, I can't test it. However, I was able to test using a public service and it worked as expected, returning two items in the table with the maximum value for each aggregationMethod

var fs =  FeatureSetByPortalItem(
    Portal("https://www.arcgis.com"),
    // portal item id
    "7b1fb95ab77f40bf8aa09c8b59045449",
    0, // layer id
    ["*"], // fields to include
    false // include or exclude geometry
  );

GroupBy(fs, 'aggregationMethod', {name: 'TOTPOP_CY', expression: 'TOTPOP_CY', statistic: "MAX"})

 

 

View solution in original post

0 Kudos
11 Replies
KenBuja
MVP Esteemed Contributor

You're using out_dict in the GroupBy, but you haven't assigned any records to it. Why aren't you using fs in the GroupBy?

0 Kudos
JanelleShank
Emerging Contributor

I thought by using "out_dict", I was assigning a new variable.

0 Kudos
KenBuja
MVP Esteemed Contributor

The first parameter of the GroupBy function is the FeatureSet that contains the data, which should be fs. The out_dict variable is just an empty FeatureSet you've created.

0 Kudos
JanelleShank
Emerging Contributor

Thank you, I'll give that try. 

0 Kudos
JanelleShank
Emerging Contributor

When I replaced "out_dict" with fs, I got an error that it could not execute the expression. Do you happen to have any idea of where I am going wrong?

 

 

0 Kudos
KenBuja
MVP Esteemed Contributor

When using variables, don't encase them in quotes.

var maxvotes = GroupBy(fs, 'Precinct_Name', {name: 'Reg_voters', expression: 'Reg_Voters', statistic: "MAX"})

 

JanelleShank
Emerging Contributor

Thank you. I removed the quotes from the variable and the expression was able to run this time. However it returned a "null" value when it should return a table, like the one below. 

JanelleShank_0-1709574418615.png

Do I need to return to the empty FeatureSet?

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Since your data is private, I can't test it. However, I was able to test using a public service and it worked as expected, returning two items in the table with the maximum value for each aggregationMethod

var fs =  FeatureSetByPortalItem(
    Portal("https://www.arcgis.com"),
    // portal item id
    "7b1fb95ab77f40bf8aa09c8b59045449",
    0, // layer id
    ["*"], // fields to include
    false // include or exclude geometry
  );

GroupBy(fs, 'aggregationMethod', {name: 'TOTPOP_CY', expression: 'TOTPOP_CY', statistic: "MAX"})

 

 

0 Kudos
JanelleShank
Emerging Contributor

Thank you for your help on this! Your code helped me refine my expression and I also noticed that I had my Item number incorrect in my expression and once that was fixed I was able to apply the correct expression. 

Thank you, again!

 

Best,

Janelle

0 Kudos