I am trying to get the percentage calculation on a label for my top most occurred alarm from a dataset that is returning the column 1: "name of the most occurred alarm" column 2:"how many times it occurred".
I have an expression binding configured to get my dataset.
I am struggling to get the total count so I can calculate the percentage from here.
Here is the error:
The error you are getting is because you are attempting to add a string to a float. According to what you have posted, the first column of your dataset is the name of the alarm which is a string. What you really want is row[1], which will be the count.
On top of that, you will only ever get the value of the first row, because you are immediately returning, perhaps you're not yet done with the script, but just returning the sum of a column isn't a percentage, based on what you have explained.
Thanks for your quick response sir.
I got the percentage. Looks like I cannot use number format to limit to two decimal places with '%' sign it throws me an error.
Your numberFormat function doesn't work and if it did it wouldn't do anything because you haven't assigned it to anything (with an = operator).
Try this:
sum = value[0][1] / val
return "{:.2%}".format(sum)
Or simplify it down to one line:
return "{:.2%}".format(value[0][1] / val)
The funky Python notation tells it to format with 2 digits after the decimal point and show as a percentage. It will then automatically multiply by 100 for you.
Tips:
Post code - not pictures of code (unless you need to show the context of the code). See Wiki - how to post code on this forum.
Leave a little air in your expressions. So, compare, sum = (value[0][1]/val)*100 sum = (value[0][1] / val) * 100
It's a personal preference but I consider the mathematical operators as separate words and so should have a space each side.
sum is the name of a python function, you shouldn’t use that as a variable name. Use something like total instead. That’s why it’s formatted differently