# Calculating Percentage In Report Based Off Query Results

**URL:** <https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155>\
**Category:** Ignition\
**Tags:** namedqueries, database, scripting\
**Created:** [April 15, 2019, 2:59pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155 "2019-04-15T14:59:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 15, 2019, 2:59pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/1 "2019-04-15T14:59:26Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/2X/7/7eb6bdc47f468d90fce476528a5db0f379d2ed54.png)

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [April 15, 2019, 4:34pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/2 "2019-04-15T16:34:22Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 15, 2019, 5:36pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/3 "2019-04-15T17:36:01Z")

</div>

@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)

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [April 15, 2019, 5:51pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/4 "2019-04-15T17:51:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 15, 2019, 5:52pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/5 "2019-04-15T17:52:59Z")

</div>

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](https://docs.inductiveautomation.com/display/DOC79/Keychain+Expressions)  
Something like `@TotalParts/GoodParts@` - filling in the correct data keys as needed.

---

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 16, 2019, 3:00pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/6 "2019-04-16T15:00:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [April 16, 2019, 3:13pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/7 "2019-04-16T15:13:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 16, 2019, 3:18pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/8 "2019-04-16T15:18:12Z")

</div>

That worked Perfectly! Thank you @paul-griffith 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!

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [June 27, 2022, 5:44am UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/9 "2022-06-27T05:44:21Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/f/ff56f606976a5c051e9906fb903138f47d95ae71.png)

I would like to have the % column show the percent of each row relative to the total.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/d/8df62d052963306705480e4a20b3d722728f3ce1.png)

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

---

<div class="post-metadata">

**Author:** ![hirz](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@hirz](https://forum.inductiveautomation.com/u/hirz)\
**Post date:** [September 22, 2022, 4:24pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/10 "2022-09-22T16:24:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [September 22, 2022, 8:18pm UTC](https://forum.inductiveautomation.com/t/calculating-percentage-in-report-based-off-query-results/24155/11 "2022-09-22T20:18:59Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/6/4613c20f74d45f4375eccb989046e58348122d7e.png)

That then seems to work:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/a/6a5ded624cdad38a23b6213da1aa459b433fd220.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/6/061bec831fb6f28979bb0609f1eeeea7c130559a.png)
