Making a calculation in arcade GroupBy expression

1085
2
Jump to solution
01-27-2023 01:44 PM
Labels (1)
Ryan_Gould
New Contributor III

I am trying to display the average event closures in a dashboard list using a GroupBy function. I would like to display the average closures per month and per week.  I'm trying to do this using a GroupBy expression that divides the total closed events by the program length. The test results either shows NaN (Not a Number), or no results. 

I've tried using the Number function in the expression to convert the equation to a number, but this produces no results in the Test. Apparently can't use a variable in the expression because this again produces no results. I've tried adding fields for Total Weeks and Total Months. This doesn't work either. 

I'm fairly new to Arcade . This seems like a simple task. Can some one show me where I'm going wrong? 

Thank you

 

var fs = FeatureSetByPortalItem(Portal('xyz'), 'xyz', 0, ['*'], false);

//Filter for closed events
var fs_closed = Filter(fs, 'STATUS > 1');
//Count rourds after filtering
var cnt = count(fs_closed);
//return cnt
var mths = 5
var wks = 22

// Return a featureset with single group by statistics.  
return  GroupBy(fs_closed,	['TMM','FUNCTIONAL_ROAD_CLASS','CAMP'],
    [
		{name: 'Average Reponse Time (hours)', expression: 'RESPONSE_TIME_H/1', statistic: 'AVG'},
		{name: 'Average Reponse Time (days)', expression: 'RESPONSE_TIME_H/24', statistic: 'AVG'},
		{name: 'Average Reponse Time (weeks)', expression: '(RESPONSE_TIME_H/24)/7', statistic: 'AVG'},
        //Count the total records and divide by the program length.
        //		{name: 'Average Closures Per Week', expression: 'cnt/prglnthwks', statistic: 'AVG'},
		{name: 'Average Closures Per Week', expression: 'cnt/wks', statistic: 'MAX'},
		//{name: 'Average Closures Per Week', expression: 'Count(OBJECTID)/prglnthwks', statistic: 'AVG'},
		//{name: 'Average Closures Per Month', expression: 'cnt/prglnthmths', statistic: 'AVG'}
        {name: 'Average Closures Per Month', expression: 'cnt/mths', statistic: 'MAX'}
		//{name: 'Average Closures Per Month', expression: 'Count(OBJECTID)/prglnthmths', statistic: 'AVG'}
]
);

 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

What kind of field is RESPONSE_TIME? If it's an integer, you won't be able to divide it nicely, as your output will also be an integer. You'll have to cast the data to a float before dividing it, if that's the case.

Also, you can use variables in your expression, but it has to be formatted as '@cnt/@wks' or `${cnt}/${wks}`

You can't use aggregation functions in the expression, however, so Count won't work.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

What kind of field is RESPONSE_TIME? If it's an integer, you won't be able to divide it nicely, as your output will also be an integer. You'll have to cast the data to a float before dividing it, if that's the case.

Also, you can use variables in your expression, but it has to be formatted as '@cnt/@wks' or `${cnt}/${wks}`

You can't use aggregation functions in the expression, however, so Count won't work.

- Josh Carlson
Kendall County GIS
0 Kudos
Ryan_Gould
New Contributor III

Hello, thank you for the reply. I should have mentioned that the first 3 expressions seem to work fine. It's the Average Closures Per Week and Month that do not. The RESPONSE_TIME_H field is a double. 

I get results when I use the @ format. Now I think my logic is incorrect. All the records have the same value for the field now. This makes sense as the total count is 198 and there were 22 weeks or 5 months in the program. My goal was to show the total events filtered by another field. This data is collected from highway maintenance crews. For example: I would like to filter by maintenance camp, or highway classification. I think I might need to use an indicator or chart, but I'm not sure. 

Thank you for your help. Even is this doesn't solve my problem, I now know how to use variables in a GroupBy expression. Have a good weekend. 

0 Kudos