You are on page 1of 27

Drill down Techniques in SQL Server Reporting Services with the

Visibility Property

Problem
Reporting Services offers many standard solutions out-of-the-box. However, controlling how data is
displayed can be frustrating without understanding some of the subtleties of the Reporting Services report
designer. Users can get confused and turned off, if the presentation of data is less than perfect. You can
create a fairly nice drilldown using the Report Wizard, but how about conditionally opening some
groupings and not others? What about starting a drill down report with all the groups expanded? How do
you change the behavior of the +/- signs (which is not always intuitive)?

Solution
The solution to these challenges is to understand some subtle techniques when using the Reporting
Services report designer. This discussion will demonstration three specific techniques. All of the materials
needed for this demonstration are included so you can try this yourself. The techniques are the following:

• Technique #1 – The Standard “Drilldown”


• Technique #2 – The Reverse “Drilldown”
• Technique #3 - Conditional Initial Visibility

Follow along and you'll be a drill down Pro in no time at all.

Sample Data

The following examples use a fictitious table about the availability of fruit from different suppliers. If you want to follow along
your own system, create a database called zVisibilityLab. Once created use this script to create the table.

Technique #1 – The Standard “Drilldown”

Setup a new report to use the VISIBILITY_EXAMPLE_TABLE as a datasource. Use the following query:

SELECT ProductCode, ProductName, ProductColor, QuantityAvailable, SupplierName, SupplierRegion


FROM VISIBILITY_EXAMPLE_TABLE

Setup a standard table. When you are done it should look something like this:

The preview will look like:


Next add a grouping for Region.

Select the Supplier Region field from the dropdown.


I have also merged all cells in the group header, deleted the group footer and added border styles of
"solid" for everything. When you are done it may look something like this:

Now for the Drilldown


Check the properties of the group header textbox. Note the "Name" property. It should be something like
"SupplierRegion_1" (or something close to that).

Now we have to get to the Details Grouping properties. (There's something about that that doesn't make
sense, but go with me here.) There are two ways (that I know of) to get there. 1) Right click on the details
line and select Edit Group (even though it's a details line), or 2) select the entire table, right-click,
select Properties, then the Groups tab, then Details Grouping. Both these methods bring you to the
same place.
Then click on the Visibility tab. Click Hidden, check the Visibility can be toggled... checkbox and select
theSupplierRegion_1 textbox.

When you are done your preview should look like this:
Expand some of the times:

Technique #2 – The Reverse “Drilldown”

But let's say this is a small table and you want display the records expanded to open by default. There are
two additional things to add to the above technique to make this work.
1) On your Visibility tab (of the Details Grouping), change the Initial Visibility to Visible.
After this, try out a preview. You will notice that the +/- signs do not work the way you would expect or
want.
2) To reverse this, right click on the SupplierRegion_1 (group header, not the group details anymore)
textbox properties. Select the Visibility tab, then change the Initial appearance of the
toggle... to Expanded (-).

This will correct the +/- sign and you are all set to provide all details by default with the ability to collapse
groupings for readability.

Technique #3 - Conditional Initial Visibility

Now let's say you want to open some groups but not others based on a certain condition. (How about by
the number of records?). For the purposes of demonstration, I have added the following expression to the
last cell in the group heading line.
=CountRows("table1_Group1")
This will display the number of rows for each group.
For the sake of this discussion, let's say that we want to display the records of a group only if the number
is 4 or less.

In the Visibility tab of the Group Details section, under Initial Visibility instead of selecting "Visible" or
"Hidden", select Expression and enter the following in the Expression field (or use the Expression Editor
button).

=IIF(CountRows("table1_Group1")<=4,False,True)

*NOTE: Important - Make sure you use the "False" and "True" keywords. In many other cases, the IIF
statement uses the keywords from the normal options (i.e. 'solid' & 'none' for BorderStyle). In this case,
you cannot use "hidden" & "visible".

Once done, the report operates fine, but we have to enter a similar (but opposite) expression to fix the +/-
signs again. To fix this, right click on the SupplierRegion_1 (group header, not the group details
anymore) textbox properties. Select the Visibility tab, then change the Initial appearance of the
toggle... to Expression and enter the following:

=IIF(CountRows("table1_Group1")<=4,True,False)
You see, the table conditionally expands where row counts are <=4. You have to click the "+" sign to
expand groups of 5 or more.

Next Steps

• You can use a similar technique (technique #1) to show and hide columns as needed. For the
toggle item, you can use an existing column header (but it can't be one of the hidden ones), or try
adding an extra textbox or even an image (i.e. like a button) right above the table.
• Take a look at these other Reporting Services tips

Getting Started

In order to create this report, you will need to have SQL Server 2005 with the
AdventureWorks database, SQL Server 2005 Reporting Services and Visual Studio
2005 correctly installed and configured. We are going to use the tables in
AdventureWorks database which is coming as a part of the SQL Server 2005 needs
to be installed.

Now start a new Business Intelligence project in Visual Studio 2005 or using
Business Intelligence Development Studio, which installs with SQL Server 2005.
Select File | New Project | Business Intelligence Projects | Report Server Project.
Name the project as HierarchicalDrillDownProject. Create a new shared data source
(HierarchicalDb.rds) and set the server, database, login and password to point to
your copy of the database. Then create a new report, HierarchicalDrillDown.rdl.

Hierarchical Relationship

In the AdventureWorks database take a deep look into the


HumanResources.Employee table, here in this table you can see the columns
EmployeeId and ManagerId. The EmployeeId column will have a foreign key
relationship with the column ManagerId. This relationship is called as Hierarchical
relationship.

Creating the Drilldown report

Create a new dataset named HierarchicalDS with the below SQL query as the query
string for the dataset and the shared data source HierarchicalDb.

Select C.FirstName + ' ' + C.LastName + ' -


' + E.Title As Name,E.EmployeeId, E.ManagerId From HumanResources.Employee As EJoin
Person.Contact As C On E.ContactId = C.ContactId Order ByE.ManagerId
Then goto the layout pane and add a table from the toolbar. For our report only one
column is enough so remove the additional columns from the table. Also remove
the Detail row and Footer row from the table as we don't require them in our
report. Give a name to the Header of the column like "Employees Hierarchy". Then
insert a group named "HierarchyGroup", in that group select the Group on
expression as EmployeeId and Parent Group as ManagerId. Uncheck the Include
Group Footer check box and click ok. Give name to the textbox in the
HierarchyGroup like "Employees".
Creating the DrillDown

We've now performed a grouping, but we still have to enable the drilldown. Click on
the table to highlight it. Right-click on the nub that appears, just to the left of the
group row. Select Properties. When the Properties window opens, expand the
visibility section. Set the Hidden property to True and set the ToggleItem property
to Employees. The Hidden property determines the state of the row when the report
is first run. If we set it to True then the data is collapsed and hidden. By setting the
ToggleItem property to Employees, when the report is run a little + sign appears
next to it when the report is run and it works like a tree view.

Switch to the Preview tab and run the report. Now only the employee in the top
level hierarchy will appear, but you can see the employees in the next levels by
using the tree-style +/- controls.
Formatting the Report

The report which we have created now appears to be unformatted with the
subsequent employee levels not arranged in a tree like fashion. To have a tree like
look we need to add the below expression in the Padding | Left property

=CStr(Level("HierarchyGroup") * 25) & "pt"

The Level function return integer starting with 0 for first level in the group.

Drilldown Hierarchical Report

Switch to the Preview tab and run the report. Finally we have created the Drilldown
Hierarchical Report
Wrap Up

I hope this article would have provided you the steps about how to create drilldown
hierarchical reports and this would help you to create reports more interactive and
suit to your business needs

Creating a Basic Drilldown Report in SSRS 2005


So from what I've seen a lot of companies tend to need to see hierarchical data in a report,
or see data in a hierarchical structure in a report. Usually the best and most simple way to
display this data is a drill down report. For the purposes of this blog I’m going to act as if
the reader hasn’t used SSRS before but has used Sql Server 2005 and some knowledge in
TSQL.

A drill down report starts with a higher level set of data (overview) and allows you to “drill
down”, or move through, the data into lower levels, getting into more specific details as you
go.
So to start, we’ll need a set of data. Just so that we’re on the same page lets create the
following stored procedure.
TSQL
LINE NUMBER OFF | HIDE | SELECT ALL
1. CREATE PROCEDURE dbo.DrilldownExample
2. -- Add the parameters for the stored procedure here
3.
4. AS
5. BEGIN
6. -- SET NOCOUNT ON added to prevent extra result sets from
7. -- interfering with SELECT statements.
8. SET NOCOUNT ON;
9.
10. -- Insert statements for procedure here
11.
12. DECLARE @TABLE TABLE (ProductType NVARCHAR(20),
ProductName NVARCHAR(50), ProductCost DECIMAL(20,4),
CustomerName NVARCHAR(50), Quantity INT, PurchaseDateDATETIME)
13.
14. INSERT INTO @TABLE VALUES ('Video Game', 'Halo 3', 50, 'Bob
Dylan', 4,'4/1/09')
15. INSERT INTO @TABLE VALUES ('Video Game', 'Halo 3', 50, 'George
Allen',1,'4/1/09')
16. INSERT INTO @TABLE VALUES ('Video Game', 'GTA4', 50, 'George
Allen',1,'4/7/09')
17. INSERT INTO @TABLE VALUES ('Video Game', 'Super Smash
Brothers', 25, 'George Allen',1,'1/28/09')
18. INSERT INTO @TABLE VALUES ('Console Accessorie', 'Xbox 360
Controller', 50, 'Bo Peep',3,'4/27/09')
19. INSERT INTO @TABLE VALUES ('Console Accessorie', 'PS2 Memory
Card', 15, 'Steve Hobs',2,'4/27/09')
20. INSERT INTO @TABLE VALUES ('Music CD', 'Psychostick - We Couldnt
Think of a Title',10,'Dave Westcalf',1,'4/27/09')
21. INSERT INTO @TABLE VALUES ('Music CD', 'Now 1', 5, 'Gill
Bates',1,'3/22/09')
22. INSERT INTO @TABLE VALUES ('Music CD', 'Now 2', 5, 'Gill
Bates',1,'3/22/09')
23. INSERT INTO @TABLE VALUES ('Music CD', 'Now 3', 5, 'Gill
Bates',1,'3/22/09')
24. INSERT INTO @TABLE VALUES ('Music CD', 'Kidz Bop 1', 5, 'Gill
Bates',1,'3/22/09')
25. INSERT INTO @TABLE VALUES ('Network Gear', 'Ethernet Cable -
05ft', 10, 'William Bonk',10,'4/27/09')
26. INSERT INTO @TABLE VALUES ('Network Gear', 'Ethernet Cable -
10ft', 20, 'William Bonk',15,'4/27/09')
27. INSERT INTO @TABLE VALUES ('Network Gear', 'Ethernet Cable -
20ft', 35, 'Bob Dylan',8,'4/1/09')
28. INSERT INTO @TABLE VALUES ('Network Gear', 'Ethernet Cable -
20ft', 35, 'Bob Dylan',8,'2/18/09')
29. INSERT INTO @TABLE VALUES ('Network Gear', 'Ethernet Cable -
05ft', 10, 'Karen Chase',2,'4/27/09')
30. INSERT INTO @TABLE VALUES ('Network Gear', 'Linksys 4 Port
Router', 55, 'Bob Dylan',2,'4/1/09')
31. INSERT INTO @TABLE VALUES ('Network Gear', 'Linksys Wireless
Network Adaptor', 68,'William Bonk',5,'4/27/09')
32. INSERT INTO @TABLE VALUES ('Network Gear', 'NIC
Card', 15, 'William Bonk',7,'4/27/09')
33. INSERT INTO @TABLE VALUES ('Computer Hardware', '80GB
Harddrive', 60, 'Lisa Smith',2,'4/27/09')
34. INSERT INTO @TABLE VALUES ('Computer Hardware', '20in LCD
Monitor', 120, 'Lisa Smith',1,'4/26/09')
35.
36. SELECT
37. ProductType,
38. ProductName,
39. ProductCost,
40. CustomerName,
41. Quantity,
42. PurchaseDate
43. FROM @TABLE
44. ORDER BY
45. ProductType,
46. ProductName,
47. CustomerName,
48. PurchaseDate
49.
50. END
51. GO

I like to run my reports off of stored procedures. To me it’s easier because if I need the
same data for a different report I can just use the same sp.

Alright, now that we know where we’re pulling the data from, let’s open up Visual Studios.
Create a new report project (file – new – project. Business Intelligence Projects, Report
Server Project). You can name it whatever you want, I’m going to name it
BasicDrillDownExample.
Now we need to create our Data Source. The data source is the database and server we’re
going to use to access our data. Right click the Shared Data Sources folder in the Solution
Explorer Window and click Add New Data Source.
Click the Edit Button
In the first dropdown list, select the server you saved the sp on. In the database dropdown
list, select the Database where the sp is. Click ok.

Now at this point I would set up the Credentials so that when I deploy it the users that I
want to use it can. Right now we’ll just deal with creating the report and deal with deploying
reports to SSRS at another time.

Don’t forget to give your data source a meaningful name. I usually put the Server Name
and Database name as the name.

Click OK. Your data source should now show up in the Solution Explorer.
Right click the Reports folder – click add – click new item. We’re doing it this way because
we don’t want to go through the wizard. Click on Report and name the report Drilldown
Example. Click Add.

Image reduced in size to fit your browser, click for original size

The report should now show up in the solution explorer window, and the report should be
open now (if not, double click the report in the solution explorer window)

If not selected, click the Data tab. Click the Dropdown Box next to Dataset and click New
Dataset.
Name the Dataset dsDrilldownData. The Data Source should be the one we just created.
The command type needs to be stored procedure. In the Query string type
dbo.DrilldownExample. Click Ok.

Click the Exclamation mark to execute the query to make sure it works.

Image reduced in size to fit your browser, click for original size
Now we’re ready to get our hands dirty. Click the layout tab. Drag a table control from the
Toolbox. Expand the dataset (dsDrilldownData) in the Dataset window. In the header of the
first column type Product Type. In the header of the second column type Product Name. In
the header of the third column type Customer Name.

From the dataset list, drag ProductType onto the detail box below the column header.
Repeat this for ProductName and CustomerName. These will be are three groups of the
report.

Image reduced in size to fit your browser, click for original size

Click on the table to see the gray bars on the top and side of the table. Right click the gray
box on the top left of the table, and click properties. The table properties box will pop up.
Click the Groups Tab. Click Add.
Name the group grpProductType. In the expression list select =Fields!ProductType.Value.
Click the Include group footer to deselect it. Click OK.

Repeat the same process for ProductName and CustomerName. Once all three are done,
make sure they’re in the same order as the image.
Click ok. Three rows will have been added, one for each group you created. The group
number corresponds with the order of the groups in the group list. Since the Product type
should be the first group, click the box and drag the field to where it’s sitting in the row for
group one. Product name belongs to group two, so drag it up to the second row. Customer
Name is group 3 so drag it to the third row.
Image reduced in size to fit your browser, click for original size

We aren’t going to use the bottom 2 rows, so highlight them by click the gray box next to
them and hitting the delete buttom.

Let’s go ahead and preview button. As you can see we have data, although it’s very ugly
and there’s not a lot there to really tell us anything. Let’s go back to layout view.

Right-click the gray box above the product name column and select Insert Column to Right.
A new column will appear. Now we have room to add some data to the product name group.
Click and drag the ProductCost field onto the group 2 row of the new column. As you can
see, VS automatically adds a sum function to any number data when added to a group. Also
notice that it went ahead and filled in Product Cost as the column header.
Image reduced in size to fit your browser, click for original size

Product Cost doesn’t need to be summed; instead we want to return the first value for it
since it will be the same for the same product. So to change this we will right-click the field
and click Expression. Replace the word Sum with the word First. The First function will
return the first value it finds. Click ok.
Add a column to the right of Customer Name. Add the quantity field to the CustomerName
group. Keep the sum function because this time we want to total the number of products a
customer has bought.

Right now if we preview the report we’d have a decent amount of data, but it would be very
ugly and somewhat. We’re going to make it a bit easier to read. We’re also going to set up
the actual drill down ability.

Click and drag on the gray boxes to the left to highlight all of the rows. In the Properties
Window click Border Style and Click Solid. Select the Header row. In the Properties Window
click BackgroundColor and click Gainsboro.

Click the Group One row and give it a color. Repeat this with the other two rows, so that
you have distinguishing colors for each group.
You should end up with something like this:

Image reduced in size to fit your browser, click for original size

Now to set it up so that we can collapse and expand the groups. Right click the table and go
to properties. Click the groups tab. Select the grpProductName group and click edit. Click
the Visibility tab. Click the hidden radio button. Click the Visibility can be toggled… check
box. In the drop down list click ProductType (the name of the box the product type field is
in, automatically named when we dragged the data onto the table). Click OK.
Repeat the same process for grpCustomerName, but select ProductName in the dropdown
list.

Now our report should be ready! Click preview. The report should load. Next to each product
type you should have a plus sign that you can click to drill down. Congrats, you’ve made
your first drill down report. If the report doesn’t work, go back and make sure you followed
every step.

You might also like