Arcade expression for percentage and status

250
2
03-11-2024 05:24 AM
BethTobin
New Contributor

Hi! 

I need help with advanced formatting a list element in my dashboard. I have a data set that contains the number of completed questionnaires per topic and the target number of questionnaires. How do i write an arcade expression to calculate the progress as a percentage for each topic (Questionnaires completed/Target) and state whether the topic is 'outstanding' (highlighted in red) or 'achieved' (highlighted in green) based on the percentage. 

 

Thank you 🙂

TopicQuestionnaires completed Target 
1117
21417
31217
41417
51017
61417
72117
81617
9817
101417
111217
121517
131317
151117
161316
171716
182216
191516
201216
212616
221716
0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

Is your example table the way the data itself is organized? What's the source table look like? Assuming your table is an accurate representation, here's what it could look like:

// calculating your percentage

var q_complete = $datapoint['questionnaires_completed']
var q_target = $datapoint['target']

var percentage = (q_complete / q_target) * 100

// color based on percentage; replace with whatever hex codes you like

var status_color = Iif(
  percentage == 100,
  '#ddffdd', // green
  '#ffdddd' // red
)

// returning the values

return {
  backgroundColor: status_color,
  attributes: {
    percentage
  }
}

 

Then in your list item template, reference {expression/percentage} to pull in the percentage value.

- Josh Carlson
Kendall County GIS
BethTobin
New Contributor
I have another follow on question from the above. I am trying to have a variable called 'status' indicating whether the topic has still got outstanding questionnaires. I am also trying to change the text colour of the percentage value. The below 'status_colour' expression does not work. Neither does my IIF statement for the status variable, only 'outstanding' is returned even if the value is >=100. 
 
var q_complete = $datapoint['questionnaires_completed']
var q_target = $datapoint['target']

var percentage = Round((q_complete / q_target) * 100) +'%'

var status = IIf(percentage >=100, 'Achieved', 'Outstanding')

var status_colour = Iif(
  percentage >= 100,
 '#ddffdd',
'#ffdddd'
)

return {
  textColor: '',
  backgroundColor: '',
  separatorColor:'',
  selectionColor: '#30c5ad',
 
 attributes: {
    percentage,
    status_colour,
    status
  }
}
0 Kudos