As far as I know you can't get to the values of any thing but the current row in the functions and formula. Your going to need to find another way. Maybe some way of getting the values you need from the other rows into the current row.
A change to the query to return this as well or I think there is an analytic query step in kettle which lets you get the nth row before or after the current row. Kettle can then be used as an input for prd
SQL: as much of a standard as the English language