SSRS Parent-Child Report

Source: Internet
Author: User
Working with a Parent-Child Hierarchy in SQL Server Reporting Services (SSRS)
Written By: Ray Barley

Problem
I'm trying to create a report that uses a parent-child hierarchy in a dimension of a SQL Server Analysis Services cube. I 've done this before where I had a parent-child hierarchy in a SQL Server relational database, but the cube has me completely baffled. can you provide an example of how to do this?

Solution
Your situation is understandable. Working with a parent-child hierarchy in a relational database is fairly straight forward, while doing the same thing with a dimension in a cube is not.

In this tip I'm going to covering the main points you need to be able to report on a parent-child hierarchy from a relational database then show the one subtlety you need to report on parent-child dimension hierarchy in a SQL Server Analysis Services cube.

Before we get started I wowould like to provide an example of what we mean by a parent-child hierarchy in a relational database. using the AdventureWorksDW database, let's take a look at the dimEmployee table and the FactResellerSales table:

The following are the main points about the above schema digoal:

  • FactResellerSales has a foreign key relationship with DimEmployee.

  • DimEmployee has a primary key of EmployeeKey.

  • DimEmployee also has a foreign key relationship with itself; this is the essence of the parent-child hierarchy. an Employee has a ParentEmployeeKey column which is a foreign key to the EmployeeKey (I. e. the employee's supervisor ).

Reporting on a Parent-Child Hierarchy in a Relational Data Source

In this section we will review a sample report based on the AdventureWorksDW database that came with SQL Server 2005. we will use the following query that leverages a recursive common table expression to report on reseller sales using the Employee parent-child hierarchy:

;WITH cte AS ( SELECT  EmployeeKey , FirstName + ' ' + LastName EmployeeName , ParentEmployeeKey , 1 EmployeeLevel FROM dbo.DimEmployee WHERE ParentEmployeeKey IS NULL UNION ALL SELECT  c.EmployeeKey , c.FirstName + ' ' + c.LastName , c.ParentEmployeeKey , p.EmployeeLevel + 1 FROM dbo.DimEmployee c JOIN cte p ON p.EmployeeKey = c.ParentEmployeeKey)SELECT cte.*, ISNULL(f.SalesAmount, 0) SalesAmountFROM cteLEFT JOIN dbo.FactResellerSales f   ON f.EmployeeKey = cte.EmployeeKeyORDER BY EmployeeLevel, EmployeeName

A partial result set from the above query is shown below:

The following are the main points about the result set:

  • Ken Sanchez is at the top of the hierarchy; I. e. his ParentEmployeeKey is null

  • The group of employees with a ParentEmployeeKey of 112 report to Ken

  • The group of employees with a ParentEmployeeKey of 277 report to Brian

  • The EmployeeLevel shows the place of the employee in the hierarchy

Our sample report layout is shown below:

Right click on the left-most column of the selected detail line then select Edit Group from the popup menu; you will see the following settings:

The key point from the above dialog is that we specify the EmployeeKey column as the Group on expression and the ParentEmployeeKey column as the Parent group.

In order to toggle visibility (I. e. enable expand and collapse), the Visibility tab on the Grouping and Sorting Properties dialog has the following settings as shown below:

Referring back to our report layout, the Sales Amount expression is cutoff in the screen shot; the full text of the expression is:

= SUM (Fields! SalesAmount. Value,

 

 

"ParentChildGroup"

, Recursive)

 

 

The above expression uses the SUM function that accepts three parameters: the expression to aggregate, the scope (I. e. the Name in the Grouping and Sorting Properties General tab), and the value Recursive (I. e. calculate the sum of the row's sales and its child rows 'sales ).

Our final step to render a simple report is to add a little bit of an indent for our hierarchy. to do that select the Employee Name field in the detail line of the report layout and set its Padding Left property as shown below:

Level () is a built-in function in Reporting Services that returns the level in the hierarchy of the item. as we descend the hierarchy, Level () simply increments by 1. this gives a little more of an indent for each successive level in the hierarchy.

We can now preview the report as shown below:

We have covered the main points for creating a report using a parent-child hierarchy in a SQL Server relational database. let's move on to show how to do it when the parent-child hierarchy is in a dimension in a SQL Server Analysis Services cube.

Reporting on a Parent-Child Hierarchy in a Cube

We will leverage the AdventureWorksDW standard cube that came with SQL Server 2005 for our report. the Employee dimension contains the same parent-child hierarchy that we saw in our relational example. if we browse the Employee dimension in the cube we will see the following:

When reporting on a parent-child hierarchy in a dimension of a cube, we need to specify the Group on and Parent group expressions differently than we did in our relational report. the Grouping and Sorting Properties dialog General tab is shown below for our report based on the SSAS cube:

Note that we are using UniqueName and ParentUniqueName instead of EmployeeKey and ParentEmployeeKey as we did in our relational report. uniqueName and ParentUniqueName are both member properties that an SSAS cube makes available to us. this is a very subtle but important difference and I wowould like to give credit to Robert Bruckner who pointed this out in his blog posting Using Analysis Services Parent-Child Hierarchies in Reports.

You might be wondering how did UniqueName and ParentUniqueName become available to us. The answer is in the generated query in our report dataset as shown below:

SELECT   NON EMPTY {     [Measures].[Reseller Sales Amount]   } ON COLUMNS, NON EMPTY {     (DESCENDANTS      ([Employee].[Employees].[Employee Level 02].ALLMEMBERS) *        [Employee].[Phone].[Phone].ALLMEMBERS ) }   DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME, PARENT_UNIQUE_NAME, LEVEL_NUMBER ON ROWS FROM [Adventure Works] 

Note that MEMBER_UNIQUE_NAME and PARENT_UNIQUE_NAME are specified in the list of dimension properties; this is precisely how those properties are made available.

After making the subtle change on the Grouping and Sorting Properties dialog General tab as shown above, we can preview our report that uses the SSAS cube as its data source:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.