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

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

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() function helpful, if you want to use more efficient expressions in place of your scripts.

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

example dataset:

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:

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:

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