Select to view content in your preferred language

Groupby Function in Arcade

2790
5
Jump to solution
08-02-2023 06:22 AM
Labels (1)
Mokwena9
New Contributor III

I have a dataset that includes a list of countries with the different studies done in those countries, and I want to auto calculate the number of studies per country in a new field.

I have used the Arcade GroupBY function as follows: GroupBy($layer,'COUNTRY', {name:'NumberS', expression:'Title', statistic:'COUNT'})

And I get this error: Cannot insert Object in Integer field. You can review the error in the script editor and try the calculation again, or cancel this calculation without changing any data.

1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You have a typo. It's "statistic" not "statistics"

GroupBy($layer, 'COUNTRY', { name:'NumberS', expression:'Title', statistic:'COUNT'})

View solution in original post

5 Replies
KenBuja
MVP Esteemed Contributor

You have a typo. It's "statistic" not "statistics"

GroupBy($layer, 'COUNTRY', { name:'NumberS', expression:'Title', statistic:'COUNT'})
Mokwena9
New Contributor III

@KenBuja Thank you I have fixed that, but I still get an error 

0 Kudos
KenBuja
MVP Esteemed Contributor

Are you getting the same error or a different one?

0 Kudos
Mokwena9
New Contributor III

@KenBuja I get this error: Cannot insert Object in Integer field. You can review the error in the script editor and try the calculation again, or cancel this calculation without changing any data.

0 Kudos
KenBuja
MVP Esteemed Contributor

Without seeing your full code, it sounds like you're trying to save the results of the GroupBy function to your field. GroupBy returns a FeatureSet, which is an object. You have to get the count value from that FeatureSet to save to your field. This is an example from the Playground that shows different ways of getting the GroupBy count for either the first record in the FeatureSet or a specific record.

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

//Get the count of how many records for each floor value
var group = GroupBy(fs, 'FLOORCOUNT', {name: 'Floors', expression: '1', statistic: 'Count'})

//return just the first record
//return firstRec = First(group).Floors

//return the count for a specific floor (3)
return First(Filter(group, 'FLOORCOUNT = 3')).Floors