# Ignition dataset

**URL:** <https://forum.inductiveautomation.com/t/ignition-dataset/43854>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [February 26, 2021, 3:18am UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854 "2021-02-26T03:18:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![epifanio.tigno](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@epifanio.tigno](https://forum.inductiveautomation.com/u/epifanio.tigno)\
**Post date:** [February 26, 2021, 3:18am UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/1 "2021-02-26T03:18:09Z")

</div>

How do i sum the values in a dataset with same row values?.Like for example below  
In column Item code, i want to sum the Weight with the same Item Code.  
Item Code Weight  
20025 1.5  
20008 0.5

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/e/ce08faecab243f187257d4f449e7985ee625bfa3.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 26, 2021, 4:34am UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/2 "2021-02-26T04:34:03Z")

</div>

I suppose that depends on what you’re wanting to do with the sum once you have it and where the data is coming from. Even then there are different ways to arrive at the answer.

For instance, you could loop through each row of the dataset and add together the values if the item code matches search criteria.

Or if the data is in a database, you could query the data summing the weight grouped by item no.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 26, 2021, 1:40pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/3 "2021-02-26T13:40:38Z")

</div>

For putting the sum on a different component, say a label, you can also use the `sum()` expression to enter a dataset and the column you want to sum.

[https://docs.inductiveautomation.com/display/DOC80/sum](https://docs.inductiveautomation.com/display/DOC80/sum)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 26, 2021, 1:44pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/4 "2021-02-26T13:44:51Z")

</div>

> [@bkarabinchak.psi](#):
>
> For putting the sum on a different component, say a label, you can also use the `sum()` expression to enter a dataset and the column you want to sum.

Yes, but note that there are different item codes that the OP wants to sum.

Strictly answering the OP's question, using dictionaries is a very versatile option. Also, using a PyDataSet simplifies row handling in the dataset, IMHO.

This assumes that this will be written to another dataset to be used in a summary table.

```python
if event.propertyName == 'data':
	# Convert to pyDataSet
	pyData = system.dataset.toPyDataSet(event.source.data)

	weightDict = {}
	for row in pyData:
		# Check if item code is already in the dictionary
		if row['Item Code'] not in weightDict.keys():
			weightDict[row['Item Code']] = row['Weight']
		else:
			weightDict[row['Item Code']] += row['Weight']

	# Create output dataset
	headersOut = ['Item Code', 'Weight']
	data = [[itemCode, weightDict[itemCode]] for itemCode in sorted(weightDict.keys())]
	# Write data to summary table	
	path.to.summary.table.data = sytem.dataset.toDataSet(headers, data)

```

Using a defaultdict will automatically detect if there is a missing key, and add the default object (in our case a float) if the key doesn't exist.

```python
 	from collections import defaultdict

	# Convert to pyDataSet
	pyData = system.dataset.toPyDataSet(event.source.data)

	weightDict = defaultdict(float)
	for row in pyData:
		weightDict[row['Item Code']] += row['Weight']
	...

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 26, 2021, 1:53pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/5 "2021-02-26T13:53:35Z")

</div>

Before @pturmel comes through ‘meh-ing’ for not working strictly with the original dataset, this one’s for you. 😉

```python
 	from collections import defaultdict
	
	dataIn = system.event.data
	weightDict = defaultdict(float)
	for i in xrange(dataIn.rowCount):
		weightDict[dataIn.getValueAt(i,'Item Code')] += dataIn.getValueAt(i,'Weight')

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 26, 2021, 2:20pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/6 "2021-02-26T14:20:54Z")

</div>

Tangentially related but a `sumif()` type function similar to the excel version would be a very useful expression.

---

<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:** [February 26, 2021, 2:33pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/7 "2021-02-26T14:33:14Z")

</div>

Nah, I’ll say “Meh” because this problem is trivially solved with my `view()` expression function (from [Simulation Aids](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids)):

```auto
view("Select Item_Code, sum(Weight) As Weight Group By Item_Code", {path/to/source/dataset})

```

Note that I had to use an underscore in Item\_Code since spaces aren’t allowed in python. See my docs on “munged” column names.

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [February 26, 2021, 3:06pm UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/8 "2021-02-26T15:06:45Z")

</div>

I’ll mention that there are probably good reasons to have some of this type of data housed within a database backend. If you want it to “stay with the app” and don’t want/need the hassle of a more traditional external database, just use a SQLite database file located in the `data/projects/` folder of your Ignition install–then it will get picked up by the Gateway Backup! What you’re looking for is very standard fare for SQL.

Like @pturmel’s example above, the SQL query for something like this looks like below:

```sql
SELECT Category, Customer, [Item Code], Description, [No of Crates], SUM(Weight) as [Total Weight] FROM item_detail
	GROUP BY Category, Customer, [Item Code], Description, [No of Crates]

```

That said, going this direction may involve some significant refactoring of your project, depending on what it does. Here is a quick little example that I whipped up, fwiw:

[https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/0/80156b36e6805aae2c561d2dfa4bc74ada5f79ef.mp4](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/0/80156b36e6805aae2c561d2dfa4bc74ada5f79ef.mp4)

Also, the `WHERE` clause there is just a bonus, using short-circuit evaluation to offer an optional parameter to filter by, if desired.

---

<div class="post-metadata">

**Author:** ![epifanio.tigno](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@epifanio.tigno](https://forum.inductiveautomation.com/u/epifanio.tigno)\
**Post date:** [March 1, 2021, 1:47am UTC](https://forum.inductiveautomation.com/t/ignition-dataset/43854/9 "2021-03-01T01:47:38Z")

</div>

Thanks guys for helping  
Thank you all
