Prev

prev

The prev function returns the value of a column from the previous row in the current context.

Spec

prev(column, [ offset ], [ default_value ] )

⚠️ The type of the third argument must be the same as the first argument. If not, this function will fail with a parse error.

Parameters

column - The column to retrieve the previous value from.
offset - The look backawards offset. Must be a positive integer greater than 0. Defaults to 1.
default_value - The default value if there is no previous. Defaults to null. Must be the same type as column

Return Value

The value of the column from the previous row, or null if there is no previous row.

Example

Get the previous value of a column:

datatable(val:int64)[1, 2, 3, 4]
| extend prev_val = prev(val)
valprev_val
1null
21
32
43

This is useful for calculating differences between consecutive rows:

datatable(val:int64)[10, 15, 20]
| extend diff = val - prev(val)
valdiff
10null
155
205

Simple example using the default arguments

range col from 1 to 10 step 1
| project col, prev = prev(col)
colprev
10
21
32
43
54
65
76
87
98
109

Example using a custom offset.

range col from 1 to 10 step 1
| project col, prev = prev(col, 5)
colprev
10
20
30
40
50
61
72
83
94
105

Example using both a custom offset and a default value.

range col from 1 to 10 step 1
| project col, prev = prev(col, 5, -100)
colprev
1-100
2-100
3-100
4-100
5-100
61
72
83
94
105