# Sum column in a dataset

**URL:** <https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101>\
**Category:** Ignition\
**Created:** [May 20, 2018, 6:38pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101 "2018-05-20T18:38:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [May 20, 2018, 6:38pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101/1 "2018-05-20T18:38:39Z")

</div>

I am wanting to Sum a column in a dataset and get a percentage for each row and add that percentage to a new column.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 21, 2018, 4:52pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101/2 "2018-05-21T16:52:50Z")

</div>

You basically need to use the “Window” aggregate functions of your database, as [described starting here](https://www.postgresql.org/docs/10/static/tutorial-window.html) for PostgreSQL. If you are using a database that’s too lame to support such functions, you’ll probably have to post-process your dataset with a jython script. Or do weird (and slow) joins between your current query and a grouped version of that data.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [May 24, 2018, 2:02pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101/3 "2018-05-24T14:02:41Z")

</div>

the dataset is on a client window and the script is in python.  
this works but I am trying to get it to update the percentage in each row in that same column on an edit. It only updates the current row.

“if colIndex == 1:  
#Percent of Total Column  
sum = 0.0  
for row in range(self.data.getRowCount()):  
sum = sum + self.data.getValueAt(row, 1)  
print 'sum = ', sum  
percent = 0.123  
percent = (newValue/sum)\*100  
print 'newValue = ', newValue, 'percent = ', percent  
self.data = system.dataset.setValue(self.data, rowIndex, 2, percent)  
self.data = system.dataset.sort(self.data, 1, 0)”

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 24, 2018, 4:45pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101/4 "2018-05-24T16:45:05Z")

</div>

First, don’t write back to the same property. Use a custom property to bring in the data, and process that into a new dataset to assign to ‘data’.  
Second, use a temporary variable to hold your dataset changes in progress. Only assign to ‘data’ at the end of the script. You are only getting the first row changed because that assignment is recursing into another execution of your script.  
Finally, please always post code samples with triple-backquotes – ` – on the line before and on the line after, so it’ll be formatted for others to read clearly.

---

<div class="post-metadata">

**Author:** ![Elizabeth\_Engler](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Elizabeth\_Engler](https://forum.inductiveautomation.com/u/Elizabeth_Engler)\
**Post date:** [October 22, 2024, 5:02pm UTC](https://forum.inductiveautomation.com/t/sum-column-in-a-dataset/18101/5 "2024-10-22T17:02:59Z")

</div>

To sum a column in a dataset in scripting you can use:

```python
sum(data.getColumnAsList(colIndex))

```

where "data" is your dataset.  
Manual reference: [https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/python-scripting/variables-datatypes-and-objects/datasets](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/python-scripting/variables-datatypes-and-objects/datasets)  
You could probably use list comprehension to create an array with your new data and then use syste.dataset.toDataSet(headers, rows) to create the final dataset.
