# Calculation with previous rows

Hello all,

I have a problem which is simple (I guess) but I don't know how to solve it :(

I have the following (current) Data:

 current goal result 750 750 750 -700 -700 50 -375 -50 0 125 125 125 -250 -125 0 750 750 750 -750 -750 0

I need KNIME to calculate from the "current" data the goal and the result column. The result column can be easily calculated from the goal column. It is important, that the values in the result column never reach below 0, therefore I need to have a adjusted goal column. The calculation in the result column is just an addition of the previos row in the result column with the current row in the goal column.

Any solution for that?

Thanks  :)

Ralph

Hi Ralph,

I think the result colum is a sum of all previous rows of the goal?

In this case you can use the moving aggregation node with cumulative computation and sum.

If you need the previous row, you can get is using the lag column node. It will "shift" some of the values in a new column.

If you need full control of the previous values, you need to use a Java Snippet.

Hope this helps,

Best, Iris

2 Likes

Iris' suggestions will work only if the input contains both the current and goal values.

Since the calculation for a row depends on the calculation in the previous row, a recursive loop will do the trick.
In the attached workflow, the upper branch calculates the goal and result values for each row.  The result of each row is fed back for use in the next iteration, along with the unaltered remaining rows of the input table.

There are other ways to do this, but this workflow gives the desired output using only the current values as input.  Hope this helps.

Tim

2 Likes