Select to view content in your preferred language

How to use the GroupBy in Arcade?

3250
3
10-08-2019 02:55 PM
David-Semitekol
Occasional Contributor

I'm struggling a little with the GroupBy function in Arcade.  I have a polygon layer of census blocks in AGOL with a population, color-code, and location field.  Like this:

Population: 1,540

Color: LT Purple

Location: Jacksonville

The color field has 3 variables: LT Purple; DK Purple; and White. Because the census blocks are in different areas around the country I added the Location field to differentiate them.  (This is one layer that has all of the census blocks for each study area, about 10 areas in total.)

My Goal: I want to use the GroupBy function in my popup to total each population by color and by location each time a user clicks on one of the census blocks.  Is there a way to wrap a GroupBy inside a GroupBy or is my only choice going to be to add a larger boundary layer to utilize the Intersect function to differentiate the locations?  I'm a little afraid of the Intersect function because it is a geometry function that might slow down the pop-up, plus I also need to add a layer and then hide it from my end users.

Thanks!

0 Kudos
3 Replies
XanderBakker
Esri Esteemed Contributor

Hi David Semitekol ,

I wonder if a nested GroupBy will get you to the result you want. The first group by will generate an aggregated result that might not allow you to configure the second GroupBy. Although it should be possible to nest GroupBy functions, I don't think it is the way to go in your case. 

This week I helped a client to configure a pop-up that first intersects pipes with hydraulic zones and then calculates the average age, number of pipes and for each material (using GroupBy) lists the material, number of pipes and total length. 

On average each zone intersects about 500 - 1500 pipe features and performance was really good. So the Intersects function could be an option.

In your case, I am wondering if you already know the location, you could use Filter to query only the records for that location and then apply the GroupBy to get the stats. Would that be an option for you?

David-Semitekol
Occasional Contributor

Hi Xander

Thanks for the suggestion.  Yes, you are correct that I know the locations from the attribute table, so I think I will try the Filter query.  I wonder if nesting the GroupBy function will work, I'm just not sure how advanced the Arcade language is yet.  That seems to be the struggle with this new language, thinking about the logic seems much easier than applying the correct code.  I like where Arcade is going, and the opportunities it is giving us, especially with pop-ups.

Thanks again for the help.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi David Semitekol ,

Thinking a little more about nesting a GroupBy leads me to the conclusion that it would not work. This would require to do something like first or last as a statistical option on a string field and as far as I can see, only SUM | COUNT | MIN | MAX | AVG | STDEV | VAR are supported. 

Filter followed by GroupBy is your best option. If you have any problems constructing the expression, just post back what you have and I will see how I can help you.

0 Kudos