You are on page 1of 49

SQL Server Reporting Services

PRASHANT

TIPS & TRICKS

Contents
Introduction to ssrs......................................................................................................................................................................... 3
Section A......................................................................................................................................................................................... 3
How to install ssrs........................................................................................................................................................................... 4
Built in Expressions or functions used in ssrs............................................................................................................................... 10
Types of Expressions..................................................................................................................................................................... 10
GLOBALS....................................................................................................................................................................................... 11
OPERATORS............................................................................................................................................................ 11
COMMON FUNCTIONS................................................................................................................................................................... 12
SECTION B.................................................................................................................................................................................... 14
EMBEDDED CODE FOR CELL COLORING (CONDITIONAL FORMATTING)...................................................................................... 15
SHOW PERCENTAGES ON PIE CHART............................................................................................................................................ 17
T-SQL ISNULL FUNCTION EQUIVALENT........................................................................................................................................ 20
SHOW TICK/CROSS BOX INSTEAD OF TRUE/FALSE OR 1/0......................................................................................................... 20
LINK POSTCODE OR ADDRESS TO GOOGLE MAPS....................................................................................................................... 22
HANDLING DIVIDE BY ZERO ERROR........................................................................................................................................... 24
PARAMETER TO CONTROL ROW/COLUMN/TABLE VISIBILITY....................................................................................................... 25
ALTERNATING ROW COLORS IN A TABLE...................................................................................................................................... 28
HAVE TEXT ON MULTIPLE LINES IN A TEXTBOX........................................................................................................................... 28
HOW TO HAVE INDENTED TEXT IN A REPORT.............................................................................................................................. 29
DEFINE A REPORT CONSTANT....................................................................................................................................................... 30
DISPLAY A MESSAGE WHEN REPORT RESULT SET IS EMPTY........................................................................................................ 31
SETTING PARAMETERS DEFAULT VALUE ISSUE............................................................................................................................ 33
1 | Page

CREATE A DRILL DOWN TABLE...................................................................................................................................................... 34


DISPLAY ALL RECORDS ON ONE PAGE.......................................................................................................................................... 38
HANDLING PAGING AND NO DATA ISSUE IN A SUB-REPORT....................................................................................................... 41
DISPLAYING DATA IN SSRS TOOLTIPS.......................................................................................................................................... 41
HOW TO ADD AN ELSE TO SWITCH STATEMENT........................................................................................................................... 43
CALCULATING RUNNING TOTALS.................................................................................................................................................. 44
REPEAT TABLE HEADER ON EACH PAGE IN SSRS......................................................................................................................... 46

2 | Page

INTRODUCTION TO SSRS
SSRS or SQL server Reporting Services which is also called Microsoft SQL reporting, is a component of the Microsoft BI

stack.

The Microsoft Business Intelligence Stack mainly consists of SQL Database Engine, SSRS, SSAS, SSIS and other tools

SSRS is a set of readymade tools, which help you to create, deploy and manage reports.

You can extend reporting functionality using C# and VB as programming languages.

Microsoft SSRS or Business Intelligence SSRS, lets you create very rich reports (Tabular/Graphical/Interactive/free-form)
from various data sources with rich data visualization (Charts, Maps, sparklines).

All these reports can be viewed via a web browsers.

SSRS allows are reports to be exported in various formats (Excel, PDF, word etc.)

SSRS allows reports to be delivered via emails or dropped to a share location in an automated fashion.

SSRS provides a host of security features, in order to control, who sees which reports etc.

3 | Page

SECTION A
HOW TO INSTALL SSRS
Double-click the SQL Server 2012 installation media. Click the Installation link on the left to view the Installation options. Click the top
link New SQL Server stand-alone installation or add features to an existing installation.

Follow the SQL Server Setup wizard until you get to the Installation Type screen. At that screen, select Add features to an
existing instance

of

SQL

Server 2012.

Click Next to

move

to

the

Feature

Selection page.

Select Reporting

Native and SQL Server Data Tools. If the Management Tools have not been installed, go ahead and choose them as well.

4 | Page

Services

Continue through the wizard and reboot the computer at the end of the installation if instructed to do so.

CONFIGURE REPORTING SERVICES


If you installed Reporting Services during the installation of the SQL Server instance, SSRS will be configured automatically for you. If you
install SSRS later, then you will have to go back and configure it as a subsequent step.
Click Start > All

Programs > Microsoft

SQL

Server 2012 > Configuration

Manager > Connect on the Reporting Services Configuration Connection dialog box.

5 | Page

Tools > Reporting

Services

Configuration

On the left-hand side of the Reporting Services Configuration Manager, click Database. Click the Change Database button on the right side
of the screen.

6 | Page

Select Create a new report server database and click Next.

7 | Page

Click through the rest of the wizard accepting the defaults. This wizard creates two databases: ReportServer, used to store report definitions
and security, and ReportServerTempDB which is used as scratch space when preparing reports for user requests.
Now click Web Service URL on the left-hand side of the Reporting Services Configuration Manager. Click the Apply button to accept the
defaults. If the Apply button has been grayed out, move on to the next step. This step sets up the SSRS web service. The web service is the
program that runs in the background that communicates between the web page, which you will set up next, and the databases.

8 | Page

The final configuration step is to select the Report Manager URL link on the left. Accept the default settings and click Apply. If
the Apply button was already grayed out, this means the SSRS was already configured. This step sets up the Report Manager web site where
you will publish reports. You may be wondering if you also must install a web server on your computer. SQL Server does not require that the
Internet Information Server (IIS), the Microsoft web server, be installed to run Report Manager.

9 | Page

Click Exit to dismiss the Reporting Services Configuration Manager dialog box.

BUILT IN EXPRESSIONS OR FUNCTIONS USED IN SSRS


TYPES OF EXPRESSIONS

Globals

Operators - Arithmetic, Comparison, Logical

Common Functions - Text, Date & Time, Math, Inspection, Program Flow, Aggregate, Financial, Conversion, Miscellaneous
10 | P a g e

GLOBALS
Global expressions executes/works in Page Header and Footer parts only.
ExecutionTime shows date and time at when report executes
PageNumber shows page number of each and every page but allowed only in page header and footer
ReportName displays name of the active report what name we have assigned to the active report
UserId shows current user name like saxony/Prashant.Rai
Language displays language like US-English

OPERATORS
Arithmetic
^ power of
* multiplication
/ divides two numbers and returns a floating point result
\ divides two numbers and returns a integer result
Mod divides two numbers and returns remainder only
+ adds two numbers and concatenation for two strings
- subtraction and indicates negative value for numeric values

11 | P a g e

Comparison
Known operators : < <= > >= <>
Like compares two strings and return true if matched or else returns False. Ex: =Fields!Title.Value Like Fields!LoginID.Value
Is compare two object reference variables Ex: = Fields!Title.Value Is Null
Concatenation
+ and & symbols uses for concatenation
Logical
Known: And, Not, Or
Xor SELECT * FROM users where firstname = 'Prashant' XOR lastname = 'Rai'
AndAlso First condition will check first and if it is true only, goes to next or else it won't need to check. Because our execution time is saving
in a logical operation in which more conditions is combined using AndAlso function.
OrElse same like above

COMMON FUNCTIONS
The rest of the functions can be accessed by clicking on Expression on any report item, which looks like following:

12 | P a g e

13 | P a g e

SECTION B

The following section (pg.16-pg.46) will introduce you to some tips and tricks which are based upon,
basic/inbuilt functions and expressions that we have in SSRS and which I have garnered over a
period of time. This section is recommended for anyone who wants more out of standard SSRS
expressions.

14 | P a g e

EMBEDDED CODE FOR CELL COLORING (CONDITIONAL FORMATTING)


Bring up the Report Properties (by right clicking outside the report in Layout view)

Now go to the Code tab and write in this code:


Function GetColour(ByVal Amount As Double) As String
If Amount < 5000000 Then
GetColour = "Red"
ElseIf Amount >= 5000000 And Amount < 7500000 Then
GetColour = "Orange"
Else
GetColour = "Green"
End If
Return GetColour

15 | P a g e

End Function

Now Insert either of these formula into the Cells Colour Format
=Code.GetColour(Fields!TotalDue.Value)
=Code.GetColour(Me.Value)

16 | P a g e

SHOW PERCENTAGES ON PIE CHART


Bring up the Chart Properties (Right Click the Chart object and select properties)

Now click the Edit button for the Values section and go to the Point Labels tab

17 | P a g e

Uncheck Auto to get percentages on outside (the inside square of 9 put labels in Pie Chart, the other are on the outside) and check the Show
point labels box
Now the formula to enter into the Data label text box is:
=Sum(Value) / Sum(Value, "ChartDataset")
In this case the formula is:
=Sum(Fields!TotalDue.Value) / Sum(Fields!TotalDue.Value, "pRepSalesOrdersByMonth")
And the Label Style button opens up the label formatting properties

18 | P a g e

And this is the final product

19 | P a g e

T-SQL ISNULL FUNCTION EQUIVALENT


Using the VB IIF Function use the following expression
=IIF(Field!FieldName.Value Is Nothing, "Value for True", "Value for False")
e.g.

SHOW TICK/CROSS BOX INSTEAD OF TRUE/FALSE OR 1/0


Convert the Boolean (or 1/0) field to "R" for True / 1 and "Q" for False / 0
You can do this either in SQL or using an IIF expression

20 | P a g e

SQL
,CASE
WHEN BooleanField = 1
THEN 'R'
ELSE 'Q'
END AS BooleanField
IIF Expression
=IIF(Fields!BooleanField.Value = True, "R", "Q")
The original field doesn't necessarily have to be Boolean e.g. Does final score pass exam:
=IIF(Fields!FinalScore.Value >= 50, "R", "Q")
Now just format the textbox to Wingdings2

And this is what you get:

21 | P a g e

LINK POSTCODE OR ADDRESS TO GOOGLE MAPS


Open the textbox properties for the field you want to have the link:

22 | P a g e

And select the Action tab:

Open the Go to URL expression builder and insert the following expression:
="javascript:void(window.open('http://maps.google.com/maps?q=" & Fields!PostalCode.Value & "','_blank','scrollbars=auto,
resizable=yes, status=no'))"
This will open google maps in a new IE window using the results of the field as the search criteria
Click here for more info on the javascript open method
Or alternatively you can use the expression without the javascript to open in the current IE
="http://maps.google.com/maps?q=" & Fields!PostalCode.Value

23 | P a g e

This is just one application of the Jump to URL functionality, this can be used in many ways, e.g. Linking to customer or supplier websites
(providing you have the website address as a field in reports dataset)

HANDLING DIVIDE BY ZERO ERROR


Bring up the Report Properties (by right clicking outside the report in Layout view)

Now go to the Code tab and write (or copy and paste) in this code:
Function CalculateFraction(ByVal Numerator As Double, ByVal Denominator As Double) As Double
If Denominator = 0 Then
CalculateFraction = 0
Else
CalculateFraction = Numerator / Denominator
End If
Return CalculateFraction
End Function

24 | P a g e

Now insert the following expression into the desired textbox and format according:
=Code.CalculateFraction(Fields!Numerator.Value, Fields!Denominator.Value)
You could also use the following IIF expression:
=IIF(Fields!Denominator.Value = 0, 0, Fields!Numerator.Value / IIF(Fields!Denominator.value = 0, 1, Fields!
Denominator.Value))
But the best thing about using the Embedded Code it's easier to use and reusable.

PARAMETER TO CONTROL ROW/COLUMN/TABLE VISIBILITY


Add in a new parameter, all properties are fairly arbitrary as long as the parameter has 2 values, 1 for viewing a particular column or groups
of columns

25 | P a g e

Right click on the column head and select column visibility

26 | P a g e

Enter the following expression in show or hide based upon expression.


=IIF(ParameterForColumnVisibilty = Yes, False, True)
Which in the example is:
=IIF(Parameters!ViewIssueCell.Value = 1, False, True)

27 | P a g e

The same can be done to control the visibility for rows and the entire table.

ALTERNATING ROW COLORS IN A TABLE


On the table select the row to bring up the properties

and enter the following expression into the BackgroundColor property


=IIF(RowNumber(Nothing) Mod 2, "LightCyan", "White")
Or whatever colors you prefer.
HAVE TEXT ON MULTIPLE LINES IN A TEXTBOX
Use Chr(10) and Chr(13) when you want to start text on the next line, e.g. use the expression:
="First line of text" & Chr(13) & Chr(10) & "Second line of text"
The problem here is when you export to Excel you get a box for Chr(13)

This is fixed by leaving out the Chr(13)

28 | P a g e

="First line of text" & Chr(10) & "Second line of text"

HOW TO HAVE INDENTED TEXT IN A REPORT

For instance, you want your data to look like this:


Rank
Captain
Lieutenant
Sergeant
Corporal
Private

To handle this we can embed a non truncating space character by using an ASCII key combination code. To embed this ASCII key code we
would hold the ALT key, type 0160 and then let go of the ALT key.
So to do this in our report, in the Reporting Services table, right click on the textbox with the problem, select (fx) Expression. Then in the Edit
Expression editor use the REPLACE function to embed the non-truncating space with the normal one as follows:
=REPLACE(Fields!Rank.Value," "," ") -- (The first value between the double quotes is a space and the second value between the double quotes
is the ALT-0160 combination code.)

29 | P a g e

After we have made this function change and redisplay the report our hierarchy should be displayed properly as needed.

DEFINE A REPORT CONSTANT


Bring up the Report Properties (by right clicking outside the report in Layout view)

30 | P a g e

Now go to the Code tab and write in this code:


Public Const PI = 3.14
Now in any report expression you can use:
=Code.PI

DISPLAY A MESSAGE WHEN REPORT RESULT SET IS EMPTY


Bring up the table properties and select the Visibility tab

Set the Initial visibility to the following expression:

31 | P a g e

=IIF(Count(Fields!AnyFieldintheDataset.Value, "DatasetName") = 0, True, False)


Which in the example above would be:
=IIF(Count(Fields!CountryRegionCode.Value, "pRepNoRowsReturned") = 0, True, False)
Create a textbox and enter the message you want to display when the resultset is empty, now the the visibility of the text box to the same as
the table BUT with the True and False switched around, so in the example above:
=IIF(Count(Fields!CountryRegionCode.Value, "pRepNoRowsReturned") = 0, False, True)
This will also work with other objects e.g. Matrix, Chart, List, and Rectangle
Another way of doing it is selecting the tablix/matrix properties and setting a message as shown below:

32 | P a g e

SETTING PARAMETERS DEFAULT VALUE ISSUE


When setting the parameter default value you can simply add a value into the Default Values - Non-queried expression box like this:

Now when you preview the report in the design environment this parameter will default to 1 and if you deploy this report to Report Manager
and view it from there it will also default to 1.
The problem arises when you want to change it, if in the example you wanted to change the default value to 0 and you just replaced the 1
with a 0 then when you preview in the design environment the report will recognise this change and default to 0 but when you deploy the
report it will still default to 1 (the value it was set to when it was first deployed)
This is easily fixed you just need to include and equals sign first.

Now when you re-deploy the report it will update the RDL on the Report Server.
For text (and dates, although using expression it better for dates) you just need to use marks e.g.

33 | P a g e

="Yes"

CREATE A DRILL DOWN TABLE


Drag the table object on to the report detail area and put the field you want to be at the bottom of the drill down into the first textbox (In this
example the table will drill-down from Continent - (Country/Area) - Year - Date) and then right-click on the detail row selector and select add
group:

Select Fields!Continent.Value as the expression to group on

34 | P a g e

Now bring up the textbox properties for the far left textbox in the newly created Continent group and set the Value to Fields!Continent.Value
and the Name to Continent

Now repeat this process for the next group Fields!Name.Value


If you have values you wish to Total for each group add them in on the same line as the relevant group using the Sum() function on all but
the detail line

If (for example) your dataset when down to individual Invoice level you could add more columns to the right and then put the Invoice details
(Date, Reference Number, Customer etc) on the detail line

35 | P a g e

Note: You can only use Aggregate functions on the Groupings lines (other than the Field its been grouped on)
Now to give the table the drill-down effect you need to set each rows visibility to Hidden and the Toggle Item to the textbox above, so for the
detail row we set the Toggle Item to the OrderYear textbox (OrderYear row to Name, Name row to Continent) click on the Detail row to get
the properties

If you want to have totals (at the bottom) for each grouping these would go in the groups below the detail line and these would also need to
have their visibilities set, I usual just delete them.
One effect I like the look of is to indent each group (more than the other) by change the textbox Left property on the Format tab:

36 | P a g e

Once these have all been set you ready to deploy and it should look something like:

And something like this from Report Manager

37 | P a g e

DISPLAY ALL RECORDS ON ONE PAGE


The default Interactive size is set to 11 inches and whatever records can fit into it that is what SSRS display to us.

Let's change the Height to 0 Under InteractiveSize Property under Report properties and deploy our report and see if it is showing all records
on one page.

38 | P a g e

Report display

As we can see that the next page button is not available as all the records are shown on one page.

39 | P a g e

Note:
Displaying all the records on one page can take long time to display. You want to make sure you do not have millions of records to shown on a
page. In those cases the explorer can crash or can take forever to display. Instead of displaying all the records on one page you might want to
increase Height from 11 inches to another number to display more records on each page.
HANDLING PAGING AND NO DATA ISSUE IN A SUB-REPORT
1.

Paging
When creating a report with sub reports paging should be added to the top level report. The paging inside the sub report looks fine on screen
but is ignored when exporting to PDF.
2.
No Data
When there is no data in a sub report the report will not show. If you have a report where you have paging between sub-reports you will end
up with a page break, but then no report, which means you get an empty page in your report. The way to force you report to render is to
create an empty dataset, with
1

select 1

as the SQL. This will always execute and force the sub-report to always render

DISPLAYING DATA IN SSRS TOOLTIPS


Tooltips are often used to help the viewer gain a better understanding of what's displayed. This entry discusses the use of data within
tooltips.
At a glance, the viewer sees the approximate percentage for each school based on the X axis:

40 | P a g e

In addition to the overall comparison of totals between schools, the exact percentage for each school is also useful information. To keep this
chart uncluttered, let's choose to provide the exact percentage using a tooltip.
Right-click the chart, select Series Properties, and open the tooltip expression (fx) window. This expression uses the Fields Collection (i.e.,
the data) as well as text:

The above expression results in the viewer seeing the following:

Regarding the FormatNumber function: source data is stored as decimal(10,2) which translates perfectly for display on the X axis when the
axis is formatted as a percentage. However, that data type doesn't look very nice in a label. To handle this, first multiply it by 100 (which
took .81 to 81.00), then used the FormatNumber function with 0 decimal places (which took my 81.00 to 81), and finally concatenated the %
symbol along with some descriptive text.

41 | P a g e

HOW TO ADD AN ELSE TO SWITCH STATEMENT


Two commonly-used functions in Reporting Services are the IIF() and the Switch(). These are two functions of the Program Flow type, or
Decision Functions.
The IIF() works in the following way. If <expression> evaluates to true then <some_code> gets executed, else <other_code> gets
executed.
Heres a simple example:
=IIf(Fields!YearlyIncome.Value >= 600,"High","Low")
Using this expression, the "High" string is returned when the value of the YearlyIncome field is equal to or above 600, while the string "Low"
is returned when the value is below 600.
Now have a look at the following example. It has been nicely structured with indentation and line breaks to make reading easier.
=IIF
(
Sum(Fields!LineTotal.Value) >= 100,
"Violet",
IIF()
(
Sum(Fields!LineTotal.Value) < 25,
"Transparent",
"Cornsilk"
)
)
As you see, it shows a nested IIF inside another one. Imagine that there were several more nestings and that line breaks were not used by
the coder. Would be a nightmare to read, right?
Thats why the Switch() was invented. The description for the Switch function reads:Evaluates a list of expressions and returns an Object
value corresponding to the first expression in the list that is True.

42 | P a g e

Heres a simple example:


=Switch
(
Fields!State.Value = "OR", "Oregon",
Fields!State.Value = "WA", "Washington"
)
This expression says that if the value for the State field is "OR" then the Switch function will return "Oregon", and so on
Now, to get to the point of this section, the Switch function does not contain an ELSE part like the IIF does.
If you read the Switchs description closely, it says that it will return the first expression in the list that is true. So each expression is
evaluated in the order that they are passed to the function. To get ELSE-like behavior we would need an expression that evaluates to True
but only when all other expressions are False. So, why not use True as expression? Its the simplest expression that I can think of and it
does the works!
Have a look at the following, its a rewrite of the last IIF example mentioned earlier.
=Switch
(
Sum(Fields!LineTotal.Value) >= 100, "Violet",
Sum(Fields!LineTotal.Value) < 25, "Transparent",
True, "Cornsilk"
)
CALCULATING RUNNING TOTALS
The syntax for RunningValue function goes like this
RunningValue(expression, function, scope)

43 | P a g e

expression : The expression on which to perform the aggregation, for example,[Quantity].

function : The name of the aggregate function to apply to the expression, for example- Sum.

scope :The name of a dataset, data region, group or Nothing.

See the table below and RunningTotal column which is Cumulative SUM of Quantity column.

ShipId OrderId ShipDate

Quantity RunningTotal

2009-02-01 12:06:16.820

10

10

2009-02-02 16:16:16.820

15

25

2009-02-01 13:26:16.820

20

45

2009-02-03 17:40:16.820

12

57

2009-02-04 13:05:16.820

15

72

2009-02-03 14:25:16.820

25

97

2009-02-04 15:50:16.820

50

147

2009-02-05 19:40:16.820

30

177

2009-02-06 15:30:16.820

20

197

10

2009-02-07 16:20:16.820

10

207

11

2009-02-05 17:05:16.820

35

242

For achieving that in SSRS, add extra column in your Reports Layout Page and put following expression in it=RunningValue(Fields!Quantity.Value, Sum, Nothing)

44 | P a g e

But what about when you want the RunningTotals for group of records? Like for above example you want the Running Totals for each orderId
separately. See the table below :

ShipId OrderId ShipDate

Quantity RunningTotal

2009-02-01 12:06:16.820

10

10

2009-02-02 16:16:16.820

15

25

2009-02-01 13:26:16.820

20

20

2009-02-03 17:40:16.820

12

32

2009-02-04 13:05:16.820

15

47

2009-02-03 14:25:16.820

25

25

2009-02-04 15:50:16.820

50

75

2009-02-05 19:40:16.820

30

105

2009-02-06 15:30:16.820

20

20

10

2009-02-07 16:20:16.820

10

30

11

2009-02-05 17:05:16.820

35

35

Now for achieving that you need do followings :

First go to Report Layout page and add a new Group.

Give some name to the group. If you dont gave any name the default name

would be something like table1_Group1. Just note that. And add OrderId to Group On expression.

Now change the expression at RunningTotal column as


=RunningValue(Fields!Quantity.Value,SUM,"table1_Group1")
Now if you have noticed I have replaced the scope Nothing to Group name table1_Group1 .

45 | P a g e

Well after adding the extra group in the report you might see blank spaces after every OrderId change. If you dont want those blanks and
want continuous report, then go to the property page of the new group added and set Visible to false.
REPEAT TABLE HEADER ON EACH PAGE IN SSRS
In this scenario, we have to set the some properties of static member of the row groups.
To see the static members of the row groups of the tablix, select tablix and click on the small drop down arrow on the right side of the Row
Groups/Columns Groups header. Then select Advanced Mode.
Please see below screenshot.

Now you can see the static members of row groups. Now select 1st static member. After selecting 1st member, top left cell of the tablix will
be selected in the report designer.

Now press F4 or manually open property page. In that, set propertyRepeatOnNewPage to True. Also set KeepWithGroup property
to After.

46 | P a g e

Now run your report. You will see that your table header will repeat on every page of report.
If still issue is not solved then set FixedData property to True in property page of first static member in row group. It will reproduce table
header on every page of report.

Sometimes you dont need to set all three properties, but it doesnt do any harm so lets set all three properties.

47 | P a g e

48 | P a g e

You might also like