Arcade expression to calculate percentage

2493
4
Jump to solution
09-29-2021 06:34 AM
CMcDonald
Occasional Contributor II

Hello,

I would like to calculate the percentage of properties that are multiple occupancy.

There are 3 Layers required to do this

1) Multiple Occupancy (Points)

2) Addresses (Points)

3) Zones (Polygons)

CMcDonald_0-1632922252402.png

I have created 2 expressions in the Zones Layer so that in the pop-up they 'count' the number of properties in both point Layers

CMcDonald_1-1632922356433.png

Can I create another expression in the Zones Layer that will in effect

Multiple Occupancy Count / Address Count * 100

So that I can get a %

This is my arcade but the % returned although does not error is not the correct figure 

//count multiple occupancy properties
var hmo = FeatureSetByName($map, 'Houses in Multiple Occupation HMO')
var countHMO = Count(Intersects(hmo, $feature))
return countHMO
//count addresses
var address = FeatureSetByName($map,"CAG OldAbdn Sample")
var countAddress = Count(Intersects(address,$feature))
return countAddress
//calculate % of multiple occupancy
($feature["countHMO"]/$feature["countAddress"])*100

Thank you 🙂

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
  1. You need to remove the return statements. If you return from a function, everything after the statement is not executed.
  2. Arcade expressions don't know of each other. You cant call $feature["countHMO"], because that calls a field that does not exist. You have to calculate countHMO & countAddress again and use them for calculating the percentage.
//count multiple occupancy properties
var hmo = FeatureSetByName($map, 'Houses in Multiple Occupation HMO')
var countHMO = Count(Intersects(hmo, $feature))
//return countHMO
//count addresses
var address = FeatureSetByName($map,"CAG OldAbdn Sample")
var countAddress = Count(Intersects(address,$feature))
//return countAddress
//calculate % of multiple occupancy
return (countHMO/countAddress)*100

Have a great day!
Johannes

View solution in original post

0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor
  1. You need to remove the return statements. If you return from a function, everything after the statement is not executed.
  2. Arcade expressions don't know of each other. You cant call $feature["countHMO"], because that calls a field that does not exist. You have to calculate countHMO & countAddress again and use them for calculating the percentage.
//count multiple occupancy properties
var hmo = FeatureSetByName($map, 'Houses in Multiple Occupation HMO')
var countHMO = Count(Intersects(hmo, $feature))
//return countHMO
//count addresses
var address = FeatureSetByName($map,"CAG OldAbdn Sample")
var countAddress = Count(Intersects(address,$feature))
//return countAddress
//calculate % of multiple occupancy
return (countHMO/countAddress)*100

Have a great day!
Johannes
0 Kudos
CMcDonald
Occasional Contributor II

Thank you @JohannesLindner , that worked perfectly 🙂

I wonder (if I may ask), is it possible/is there any value adding a 'round' statement?

When you test the expression it returns many decimal places

CMcDonald_0-1632929694404.png

however I can control the appearance through Configure Pop-up > Configure Attributes. I just wondered if you rounded in advance if that would increase performance?

Thanks

0 Kudos
JohannesLindner
MVP Frequent Contributor

I don't know if it increases the performance (although I guess the effect would be minimal in either way), but  you can round:

return Round((countHMO/countAddress)*100, 2)

Have a great day!
Johannes
0 Kudos
CMcDonald
Occasional Contributor II

Cool.  Thanks again for your help, much appreciated.

0 Kudos