Showing posts with label SSRS Articles. Show all posts
Showing posts with label SSRS Articles. Show all posts

Thursday, 17 April 2014

Custom Code in SSRS

When the customer’s requirement exceeds the capability of Built-in functions of SSRS then we  need to write own Code to meet the requirements. Writing your own code(function) is called Custom Code and this is a very useful feature provided by Microsoft.

In any expression, you can call your own custom code. 
To write your own code go to report menu -->Report Properties-->code
For ex,

Let’s say we have to design a function which returns the Status of the sales of each product. For that lets create a report first which displays Product and Sales Amount as shown in the below pic -
  • Select “Report Properties” from Reports main menu or in design View Right Click the design surface outside the border of the report and select “Report Properties” as shown below.
  • Select Code tab and paste the following code to create custom function
Public Function Test(ByVal Sales As Decimal) As String
Dim status As String
If Sales >= 2000 Then
status = “High Profit”
ElseIf Sales >= 500 Then
status = “Moderate Profit”
Else
status = “Low Profit”
End If
Return status
End Function
  • Click OK and now add a column to the Table used in report and give header as “Status” and right click on the data sell and select Expression as shown below.
  • Write the expression as shown below and select OK.
=Code.Test(Fields!SalesAmount.Value)
 — “Fields!SalesAmount.Value” field based on which you wish to calculate status.
  • Your report with Custom Function to calculate the Status(in other words KPI) is ready and if you click on preview you can see the output of the function you created.


Expressions in SSRS


To write expressions in SSRS Reports Right click on text box of column and select Expression ..
Here are the some important Expression that are used in Real Time Scenarios..

1.Expression for Page Number like page number of total pages for ex 1 of 4,2 of 4 etc

="pages" & globals!PageNumber & "of " & globals!TotalPages

2.Expression for Date column format 

=Format(Fields!column .Value,"mm-dd-yy")

 3.how to display 0(Zero) instead of NULL in ssrs reports


In SQL Server we can write query like select Isnull ("columnname",0) 
=iif(isnothing(Sum(Fields!SalesAmount.Value)),0,Sum(Fields!SalesAmount.Value))
4.expression provides the name of the report and the time it was run. It can be placed in a text box in the report footer 
=Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")
5 When a page break is defined for the group, this expression results in a page break every 25 rows.
=Ceiling(RowNumber(Nothing)/25)
6.when used in the Color property of a text box, changes the color of the text depending on the value of the Profit field
=Iif(Fields!Profit.Value < 0, "Red", "Black")
7. when used in the BackgroundColor property of a report item in a data region, alternates the background color of each row between pale green and white:
=Iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White")

8. when used in the BackgroundColor property of a report item in a data region, alternates the background color of each row between green and Orange and Red(if it is more than  2 condition ,we can use Switch ):
=Switch(ReportItems!Textbox75.Value>0.9, "Green",
ReportItems!Textbox75.Value >0.7, "Orange", ReportItems!Textbox75.Value <=0.7, "Red")