Jump to content
Changes to the Jaspersoft community edition download ×

Initialising variables!


mctozzy

Recommended Posts

I seem to be having a problem with initial values for Variables. What I want to do is create a running balance value, so I have a variable with Calc type Sum, Reset type = Report, Increment=none.

 

I have a variable expression such as $F{Amount}.

 

This works correctly, except for one thing. I want to prime the value of the variable, so that it starts off with an "Opening Balance" so to speak.

 

The initialValueExpression doesn't seem to work in the way I would have expected. No matter what I do, the initial value is always zero.

 

Thanks,

Milt.

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,

 

This is because the initValueExpression is used only for obtaining a value to display before the variable is actually incremented the first time.

If we are talking about a SUM and we want it to start with 5, not 0, then you should add the 5 in the expression of the field that displays the sum, or have a separate variable with calculation Nothing that adds the 5 to the sum variable.

 

This is because the initValueExpression of a variable is not considered as part of the series used to increment the variable during iteration. This initial value is only for displaying purposes, before the variable incrementing kicks off.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

Thanks for the explanation. By creating a second system variable, I assume you meant this:

 

(using "pseudodefs")

 

$V{RunningBal} := sum($F{Amount})

 

and

 

$V{Balance} := system($V{RunningBal}+$P{InitialVal})

 

Then using $V{Balance} in the report?

 

I can see that would work, but I don't really understand why there isn't a more natural way to prime a variable, without having to resort to this or use scriptlets.

 

Why not extend the meaning of InitialValueExpression to actually prime the variable?

 

Is this worthy of an RFE?

 

Cheers,

Milt

Link to comment
Share on other sites

Not sure what you mean by "system" variable. You simply need to create a new variable with calculation="Nothing" and $V{RunningBal}+$P{InitialVal} as expression. And in any case you don't need scriptlets to do this.

 

Regarding the "extension" of the logic of initialValueExpression, two notes:

Changing this does not strictly involve "extending" the semantics of initialValueExpression, it actually breaks backwards compatibility. People will witness altered behaviour in certain scenarios. It's never trivial to take a decision in such a case.

Base/starting value makes sense for calculations such as Sum or Count, but it doesn't for others like Average or StandardDeviation. Such a functionanily would be then slightly inconsistent across calculation types.

[/ol]

 

Regards,

Lucian

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...