# How can I reference the value of the previous row in a dataset loop?

**URL:** <https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064>\
**Category:** Ignition\
**Created:** [September 12, 2026, 3:31pm UTC](https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064 "2026-09-12T15:31:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyler.vandewal](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tyler.vandewal](https://forum.inductiveautomation.com/u/tyler.vandewal)\
**Post date:** [September 12, 2026, 3:31pm UTC](https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064/1 "2026-09-12T15:31:26Z")

</div>

Hello all, I'm new to both expression and python scripts but learning a lot recently. I'm currently trying to make a table that shows data, I have most of it down, but the last function is giving me issues.

I have multiple buttons that are scripted to show production values for certain periods of time. This I have working, but the story is a little more complicated. I'm using machine tags that have grand totals of all time, so I scripted the values in my table to begin at zero and show just the amount produced during that time frame. I've done this using a "for" loop on my first dataset and displaying the new dataset in the table.

Now I am trying to script a button that displays how much was produced over the last 30 days, and displaying **not the incremental values, but each day's individual total**. I first thought that this would be easy, just keep the same loop structure and for each row's base value (the grand total), I would subtract the last row's value from it. However, I have no clue how to reference "the previous row" in the loop!

The base value is easy. Tag = dataset.getValueAt(row, 1)

I thought it could be something like PreviousValue = dataset.getValueAt(row - 1, 1) or dataset.getValueAt(dataset.row - 1, 1) but this does not seem to exist. The closest thing I tried was dataset.getValueAt(dataset.rowCount - 1, 1) but this obviously references the value in the second-to-last row rather than the previous row of each iteration of the loop.

How could I do that? Is there a way to reference the previous row in a loop? There has to be, right?

Thanks all

---

<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:** [September 12, 2026, 4:14pm UTC](https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064/2 "2026-09-12T16:14:06Z")

</div>

Not directly, no. But you can initialize a `priorRow` variable ahead of the loop, and assign current to prior right at the end of the loop, and that will give you access to both rows (except for the first pass through the loop).

You might find my Integration Toolkit's [lag()](https://www.automation-pros.com/toolkit/doc/iteration.html#lag) function helpful, if you want to use more efficient expressions in place of your scripts.

---

<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:** [September 12, 2026, 9:57pm UTC](https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064/3 "2026-09-12T21:57:56Z")

</div>

For later versions of 8.1.x and all of 8.3.x, datasets are PyDataSets under the hood.

example dataset:

```plaintext
row | col1 | col2 | status
--------------------------
0 | a | b | 0     
1 | c | d | 1     
2 | e | f | 2     
3 | g | h | 3     
4 | i | j | 4     
5 | k | l | 5     
6 | m | n | 6     
7 | o | p | 7     
8 | q | r | 8     
9 | s | t | 9     

```

In a script:

```python
from itertools import izip

sampleHeaders = ['col1', 'col2', 'status']
sampleData = [['a', 'b', 0],
              ['c', 'd', 1],
              ['e', 'f', 2],
              ['g', 'h', 3],
              ['i', 'j', 4],
              ['k', 'l', 5],
              ['m', 'n', 6],
              ['o', 'p', 7],
              ['q', 'r', 8],
              ['s', 't', 9]]
              
dataIn = system.dataset.toDataSet(sampleHeaders, sampleData)

for prevRow, row in izip(dataIn, dataIn[1:]):
	print prevRow['status'], row['status']

```

Output:

```plaintext
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
>>>

```

---

<div class="post-metadata">

**Author:** ![tyler.vandewal](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tyler.vandewal](https://forum.inductiveautomation.com/u/tyler.vandewal)\
**Post date:** [September 18, 2026, 7:52pm UTC](https://forum.inductiveautomation.com/t/how-can-i-reference-the-value-of-the-previous-row-in-a-dataset-loop/117064/4 "2026-09-18T19:52:41Z")

</div>

Hello everyone, apologies for the late response. I've figured out a way to do as I wished! I ended up making a table with one column as the incremental total value and another column as just the total for that specific period of time. Here's how:

I have my two datasets, the raw value and the one with incremental values. I then took my incremental dataset and used a "for" loop where I was able to define tags (I'm from PLC land, I call them tags. I don't know what we call them here) as "row - 1" and such. Then I called system.dataset.updateRow within the "for" loop to iterate that dataset:

```python
for row in range(newDataSet.rowCount):
     prevRow = row - 1
updates = {"Hourly": originalDataSet.getValueAt(row, 1) - originalDataSet.getValueAt(prevRow, 1)}
newDataSet = system.dataset.updateRow(newDataSet, row, updates)

```

TL;DR: You can reference the previous row in a dataset loop by making another dataset with a tag "tag" defined as "row - 1" and call a getValueAt for the original dataset with that tag. 🙂 thank you for your responses as well!
