Came across this Question : http://community.jaspersoft.com/questions/844022/variable-initial-value-and-re-use-same-variable in Jaspersoft Community and worked on it for one day to get the result.
The requirement here is to add or subtract the values from 2 different columns based on whether they are from input column or output column and then the total has to be a running total. This is surely a challenge and the answer for that is here below.
1) I created my own dataset (CSV Format) as per the requirement of the person who asked the question and created a simple report as below.
2) Then I created a variable which will display the no of row for each record. I call it as Increment variable. To know how to cretae an Increment Variable. Please check my blog http://www.rajeshsirsikar.com/increment-a-variable-in-jaspersoft-studio/
Output preview and the Variable definition is as follows.
3) Now I Created a Variable which will Display the Input and Output Column Values in One Column. Trick here is to Display teh Value with “-” sign for output column. So the Variable defn and potput preview is as follows.
4) Now the Tricky Part. The questioner has a condition that the first Value of Total Column has to be either added or subtracted (depending on the column Input or Output) with a constant Value 43. So I created a Variable with the condition as follows.
2 Important Points to Note Here
a) The Variable expression displays the Col Var Values as is for the rest of the rows.
b) We are adding the Values not subtracting (as per the requirement) is because there are -ve values already present in the column.
5) Now the final Part where we create the variable to display running total. The Variable defn and Preview is as follows.
So the Final Column will display the totals as the requester wanted it to be.
People who wants to create a Running total, Please refer to step 5 Only.
Hope this helps!!!