SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Expressions


Expressions

Author
Message
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)

Group: General Forum Members
Points: 598 Visits: 1265
I am trying to find out the sum of a filed named "PercentColumn" which is calculated column based on PreviousYear and Currentyear.

The expression of the "PercentColumn "is :
=IIF(Fields!PreviousYear.value = 0,0,
(Fields!Currentyear.value - Fields!PreviousYear.value)
/IIF(Fields!PreviousYear.value = 0,1,Fields!PreviousYear.value))

I am taking care of "Zero" in the denominator and finding the % increase and decrease. Up until here the results are populating fine. But,My next column should have a sum of this "Percent Column". So I am using sum of the above expression but its returning an error ...

Can anyone pls advise.
Mark Fitzgerald-331224
Mark Fitzgerald-331224
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3884 Visits: 1538
sharonsql2013 (10/31/2013)
I am trying to find out the sum of a filed named "PercentColumn" which is calculated column based on PreviousYear and Currentyear.

The expression of the "PercentColumn "is :
=IIF(Fields!PreviousYear.value = 0,0,
(Fields!Currentyear.value - Fields!PreviousYear.value)
/IIF(Fields!PreviousYear.value = 0,1,Fields!PreviousYear.value))

I am taking care of "Zero" in the denominator and finding the % increase and decrease. Up until here the results are populating fine. But,My next column should have a sum of this "Percent Column". So I am using sum of the above expression but its returning an error ...

Can anyone pls advise.



Your original expression is checking for PreviousYear value twice. The second time is not required as the expression will only calculate if the PreviousYear is not 0 (by the first if).

=IIF(Fields!PreviousYear.value = 0,0,
(Fields!Currentyear.value - Fields!PreviousYear.value) / Fields!PreviousYear.value)

Now if you are writing a report then you could get the "sum" in two ways. Usually you would not want to sum the percentages or to sum the PreviousYear and CurrentYear then calculate the percentage of the whole.

Case 1 :
= sum(IIF(Fields!PreviousYear.value = 0,0,
(Fields!Currentyear.value - Fields!PreviousYear.value) / Fields!PreviousYear.value))

Case 2:
=IIF(sum(Fields!PreviousYear.value) = 0,0,
(sum(Fields!Currentyear.value) - sum(Fields!PreviousYear.value)) / sum(Fields!PreviousYear.value))

Taking as test values :
PreviousYear | CurrentYear | Percentage
0 100 0 (0%)
100 100 0 (0%)
200 100 -0.5 (-50%)
100 200 1 (100%)

Case 1 : 0 + 0 -0.5 + 1 = 0.5 (50%)
Case 2 : sum(PreviousYear) = 400, sum(CurrentYear) = 500
(500-400) / 400 = 0.25 (25%)

Case 2 shows how the total sales etc has grown correctly.

Fitz
sharonsql2013
sharonsql2013
Mr or Mrs. 500
Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)Mr or Mrs. 500 (598 reputation)

Group: General Forum Members
Points: 598 Visits: 1265
The reason I have another IIf in the denominator is because we have Previous year again in denominator and this denominator can be zero too. If I do not use this IIF again , I get " #Error as output wherever the denominator is zero.
Mark Fitzgerald-331224
Mark Fitzgerald-331224
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3884 Visits: 1538
sharonsql2013 (11/1/2013)
The reason I have another IIf in the denominator is because we have Previous year again in denominator and this denominator can be zero too. If I do not use this IIF again , I get " #Error as output wherever the denominator is zero.


Seems strange as the expression in bold below will stop the second part of the iif from running. I have reproduced the code in a report without the second iif without issue.

=IIF(Fields!PreviousYear.value = 0, 0,
(Fields!Currentyear.value - Fields!PreviousYear.value)
/IIF(Fields!PreviousYear.value = 0,1,Fields!PreviousYear.value))

Whether you continue with the double iif or single iif the cases in the previous post will still work.

Fitz
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search