Calculating Percentage In Report Based Off Query Results

I have a named query I’m running and it’s interpreting some data from my database (mostly totals). I’m trying to calculate a percentage based off of those totals and I haven’t been able to do it in the query itself. Is there a way I can calculate this percentage using scripting and have it show up in my report? If so, how do I reference my query data? I want to calculate a percentage of Good Parts to Total Parts.

image

I’m surprised you can’t do that calculation in the query, but never fear, you can use a script datasource.

https://docs.inductiveautomation.com/display/DOC80/Scripting+Data+Source

Just make sure it’s lower down in the list than your query datasource and it will have access to the results of the query.

1 Like

@KathyApplebaum Thank you for your answer. I’m wondering if you could maybe share an example of how I reference a specific column or row of data from the query? I’m new to programming and sometimes struggle with the proper syntax.

Also, my script doesn’t show up as a data source in the report. (I can’t insert my script as a data source in a text box)

The linked docs have examples towards the bottom of the page.

Another option that may be simpler than a script datasource in this limited case would be a keychain expression: https://docs.inductiveautomation.com/display/DOC79/Keychain+Expressions
Something like @TotalParts/GoodParts@ - filling in the correct data keys as needed.

3 Likes

Where do I implement these expressions? In the data tab of the Reporting Module as a script? Or a parameter?

1 Like

Keychain Expressions are used in the Design tab of the Reporting module, so you would write the expression on a Text Shape/cell of your table.

2 Likes

That worked Perfectly! Thank you @PGriffith and @Paul.Scott! It might be something worth adding to the Ignition manual that the Keychain functions are implemented in the design tab of the Reporting Module.

Thank you all for your help!

1 Like

Can you not calculate a percentage of a column value based off its own total?
I keep getting <N/A> results in preview mode when trying to do the following:
image

I would like to have the % column show the percent of each row relative to the total.
image

so for that last row, the value of 7 would correspond to 50% in the second column.

Did you ever get anywhere on this? I am having the exact same problem, and this is the only thread I could find about it.

Couldn’t get it working with the inbuilt total.Count property. But i did get it working by creating a new data source which is just manually calculating the total count from the PT_Reasons query source:

That then seems to work:

image

image

1 Like