Friday, February 10, 2012

Calculating % in Text Box

Hi

I'm On the "Layout Tab" and I'm trying to
calculate a percentage in a text box.

This works:

=((Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)

This Does Not work:

=((Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)
/ Fields!PREV_QTD_LBS.Value) * 100

All I get back when I "Preview" the report is "Error"

Why?

It seems like I should also be able to do the following, but cannot:


If Fields!THIS_YTD_LBS.Value < Fields!PREV_YTD_LBS.Value

=((Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)
/ Fields!PREV_QTD_LBS.Value) * 100

ELSE

return zero

END

Thanks

bjboo wrote:

This Does Not work:

=((Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)
/ Fields!PREV_QTD_LBS.Value) * 100

All I get back when I "Preview" the report is "Error"

Why?

First thing, you don't have to worry about multiplying by 100 at the end, you can use the string function FormatPercent(<expr>) instead. This will also handle if you are dividing by zero (which you are not checking for), or if one of your values is null.

bjboo wrote:

It seems like I should also be able to do the following, but cannot:


If Fields!THIS_YTD_LBS.Value < Fields!PREV_YTD_LBS.Value

=((Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)
/ Fields!PREV_QTD_LBS.Value) * 100

ELSE

return zero

END

This is easily done too. You can either do it in custom code and call the custom code as part of the expression, like this:

= Code.MyFunction(<param>, <param>, ...)

Or you can use the IIf function:

=IIf(<expr which evaluates to true/false>, <expr to evaluate if true>, <expr to evaluate if false>) . So your above expression becomes:

= IIf (Fields!THIS_YTD_LBS.Value < Fields!PREV_YTD_LBS.Value,

FormatPercent( (Fields!THIS_QTD_LBS.Value - Fields!PREV_QTD_LBS.Value)
/ Fields!PREV_QTD_LBS.Value, 2, true, false, false),

"0"

)

That was purely of the top of my head, so make sure you test it :)

sluggy

|||

Thanks, so much.

No comments:

Post a Comment