2/16/2009
Implementing Real Analysis Services DrillDown in a Reporting Services Report
Sean Boon recently blogged about an approach to implement drilldown on charts with Reporting Services when Analysis Services is used as the data source, and it got me thinking about ways to implement drilldown in Reporting Services in general. There are two standard methods used to do this that are widely known about:
- The first can be described as "fetch all the data you're ever going to need to display and then hide the stuff that hasn't been drilled down on yet" - this article describes it well, albeit for SQL data sources. It's easy to implement but has has big problem: if the amount of data your report could ever possibly display is massive then the report will be very slow to run, for example if your dataset query returns millions of rows.
- The second is more scalable, in that you have multiple reports for each level of granularity you want to display and when you drill down or drill up you click on a member in the Report and pass it as a parameter to another report. This also works well but has a different problem: you now have multiple copies of what is essentially the same report to maintain and keep in synch. This approach can also only display one level of granularity at a time, and sometimes it's nice to be able to see multiple granularities in the same report.
Wouldn't it be good to have drilldown capabilities in Reporting Services just like you have in any other Analysis Services client? That's to say, you'd see a list of members on rows in your report, you'd click on one and then see all its children, then click again and its children would disappear? Well, it is possible and it's a problem I've tackled numerous times myself. The last time was when I was writing the samples for Intelligencia Query, but I've just come up with an even better approach which I thought I'd blog about. I've implemented it for the standard Analysis Services data source although I'll be honest it took me a few goes to get it to work properly (there would have been much fewer hacky workarounds if I'd been using Intelligencia Query!) and I'm not sure it's 100% robust; hopefully someone will find this useful though.
What I've done is basically a variation on the second approach above, but instead of using multiple reports I've created a single report which calls itself when you drill down on a member. The really tricky part is how you manage the set of members you've drilled up and down on, and this is where I'd struggled in the past - the solution I've got here uses a hidden parameter to manage that set, which is then passed to the main dataset and used with the DrillDownMember function.
Here are the steps to get it working:
- Create a new Reporting Services report with a data source pointing to Adventure Works.
- Create three new report parameters in this order:
- MemberClicked - tick "Allow Blank Values" and set the default value to [Customer].[Customer Geography].[All Customers]. This parameter will hold the unique name of the member the user clicked on to drill down.
- PreviousDrillDowns - again tick "Allow Blank Values" and set the default value to [Customer].[Customer Geography].[All Customers], and tick "Allow Multiple Values". This parameter will hold the list of members the user drilled down on before the last drill down.
- DrillDowns - again tick "Allow Blank Values" and tick "Allow Multiple Values". This parameter will hold the complete list of members drilled down on for the current report.
Create a new Dataset in the report called DrillDowns. Use the following MDX for the query:
WITH
MEMBER MEASURES.CUSTUNAME AS
[Customer].[Customer Geography].CURRENTMEMBER.UNIQUENAME
SET DRILLDOWNS AS
UNION({[Customer].[Customer Geography].[All Customers]},
IIF(
//CLICKED MEMBER HAS NO CHILDREN, SO IGNORE IT
ISLEAF(STRTOMEMBER(@MemberClicked)), STRTOSET(@PreviousDrillDowns),
IIF(
COUNT(INTERSECT(
STRTOSET(@PreviousDrillDowns),STRTOSET(@MemberClicked)
))=0,
//DRILL DOWN
UNION(STRTOSET(@PreviousDrillDowns),STRTOSET(@MemberClicked)),
//DRILL UP
EXCEPT(STRTOSET(@PreviousDrillDowns),STRTOSET(@MemberClicked))))
)
SELECT {MEASURES.CUSTUNAME} ON 0,
DRILLDOWNS ON 1
FROM [Adventure Works]
What this does is take the set of previously drilled down members, and if the member we've just drilled down on is not in there return the set of all previously drilled down members plus the new member (for drilling down); if it is present, return the set of all previously drilled down members except the new member (for drilling up). If the member we've clicked on is a leaf member, we can ignore the click and just return the set of all previously drilled down members.
You'll need to hook up the two parameters @PreviousDrillDowns and @MemberClicked to the report parameters you've previously declared. To do this, first of all in the query designer declare the parameters but just fill in the names and a default, such as [Customer].[Customer Geography].[All Customers] (see here, towards the end, for more detailed steps). Then exit the query designer but stay in the Dataset Properties dialog and create two dataset parameters with the names PreviousDrillDowns and MemberClicked and hook them up to the appropriate report parameters.
Go to the report parameter called DrillDowns and set the default value to be the CUSTUNAME field from the dataset you've just created.
Create a second dataset called DisplayQuery with the following MDX:
WITH
MEMBER MEASURES.CUSTNAME AS
Space([Customer].[Customer Geography].CURRENTMEMBER.LEVEL.ORDINAL) +
[Customer].[Customer Geography].CURRENTMEMBER.NAME
MEMBER MEASURES.CUSTUNAME AS
[Customer].[Customer Geography].CURRENTMEMBER.UNIQUENAME
SELECT {MEASURES.CUSTNAME, MEASURES.CUSTUNAME, [Measures].[Internet Sales Amount] } ON COLUMNS,
DRILLDOWNMEMBER({[Customer].[Customer Geography].[All Customers]},
StrToSet(@DrillDowns, CONSTRAINED), RECURSIVE)
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Adventure Works]
This query simply displays the measure Internet Sales Amount on columns, and on rows uses the DrillDownMember function to drilldown on the All Member on Customer Geography plus any other visible members that are present in the set returned from the DrillDowns parameter.
Once again you'll have to hook up the @DrillDowns parameter to the DrillDowns report parameter.
Now, in the report, create a table and bind it to the DisplayQuery dataset. Only use the CUSTNAME field to display the members for the Customer Geography hierarchy on rows - this means you have a single field that can contain members from all levels of the hierarchy.
Finally, open the Textbox Properties dialog for the cell bound to the CUSTNAME field and set up an Action to jump to the report we're currently building. We also need to pass two parameters: one that sends the value of the CUSTUNAME field (note this is the unique name of the member clicked on, not the CUSTNAME field which is just the member name) to the MemberClicked parameter, and one that send the value of the DrillDowns parameter to the PreviousDrillDowns parameter. It's not actually obvious how to pass the values of a multi-value parameter through an Action, but I found the answer here; the expression you'll need to use for this report is:
=Split(Join(Parameters!DrillDowns.Value, ","),",")
Here's what you'd expect to see when you first run the report:
Click on Australia and then South Australia and you get this:
Click on Australia again and you'd go back to what's shown in the first screenshot.
I realise these steps are pretty complex, so I've created a sample report in SSRS2008 format and uploaded it to my SkyDrive here:
I dream of the day when SSRS will do all this stuff for me automatically...
UPDATE: you can now view the sample report online (minus the indenting for members on different levels, for some reason) here -
http://reportsurfer.com/CustomContentRetrieve.aspx?ID=170467