You are on page 1of 61

SQL SERVER 2008 REPORTING SERVICES

ADVANCED OLAP DATA SOURCES WITH MDX


SetFocus Business Intelligence Master s Program

Table of Contents
2

Chapter
Chapter Objectives Summary of Examples Visual Studio Project File Creating the Shared Data Source Example 1: Basic PIVOT table Example 2: Trend line chart with a 12-month moving average Example 3: a nested MDX TOPCOUNT and an MDX RANK, over a user-defined date range Example 4: showing KPIs Example 5: Cascading Parameters with OLAP data Example 6: (Quarter Parameter, show data for current/prior quarter, annotate both dates) Example 7: Data Mining

Slide #
3 4 5 6

7 16 24 32 39

Chapter Objectives
3

Show how to create reports against OLAP databases without writing any MDX code Show how to override the MDX that SSRS generates, with custom MDX Modify the query parameters that SSRS generates Manually create our own query parameters Create reports against Analysis Services KPIs Show how to implement reports with cascading parameters Show reports against Data Mining Models

Examples in this chapter


4
1. 2.

Creating a Matrix/Pivot table report with drilldown capabilities Creating a trend line chart with a 12-month moving average (using a prewritten MDX calculated member to determine the 12-month moving average) Create a report that generates a nested MDX TOPCOUNT and an MDX RANK, over a user-defined date range Create a report that retrieves KPIs Implementing Cascading Parameters with OLAP data Create a report with a Quarter Parameter, show data for current and prior quarter, and annotate both dates Create a report against a data mining model

3.

4. 5. 6.

7.

Visual Studio Project File


5

The project that we ll create consists of:

A Shared Data Source for the Waremart OLAP Database on the SFMPBISQL\SQL2005 server (see next slide for details on creating the shared data source) Five report RDL files

Creating the Shared Data Source


6

To create the shared data source:


1.

Right-click on the Shared Data Sources folder and select Add New Data Source Give the Data Source a name (dsWareMartOLAP), and select Microsoft SQL Server Analysis Services as the Data Source Type Click the EDIT button to edit the connection properties. In the Connection Properties window, enter the server name (SFMPBISQL\SQL2005), and select Waremart 2005 as the OLAP database

2.

3.

Demo: Example 1
Creating a trend line chart with a 12-month moving average using MDX rpt01_BasicOLAP.rdl

Demo 1, End result


8

Report prompts for a geography selection (either All, or a country, or a state, or a city) Report shows Internet Sales on the row axis, by Product Category, Product SubCategory, and individual product (using SSRS drilldown feature) Report also breaks down sales by Fiscal year and Fiscal quarter on the column axis, using the same drilldown feature

Demo 1: Steps to reproduce


9
1. 2.

Create a new report (rpt01_BasicOLAP.rdl) Create a new DataSet (using the shared Data Source dsADW2008OLAP), selecting Fiscal Year & Fiscal Quarter of Year, Product Category/Product SubCategory/Product, and one measure (Internet Sales) for the output Define a Parameter based on the Customer dimension/Customer Geography Hierarchy
1. 2. 3.

3.

Visual Studio will auto-generate a dataset with MDX code for this parameter. We will want to modify the MDX code to only show Geography down to the state level That will require a manual change to the RDL file, so that Visual Studio will never overwrite our MDX modifications

4.

Create a Matrix report that will contain Row Groups for Category SubCategory Product, and column Groups for Fiscal Year - Quarter Add totals and drilldown capabilities for the necessary groups Annotate the page headings for the Parameters selected

5. 6.

Demo 1, DataSets and Parameters


10

Report will contain the following:


DataSets

dsSales  CustomerCustomerGeography (Generated by SSRS)




Parameters


@CustomerCustomerGeography (Generated by SSRS)

Matrix

Column Groups for FY Year and Quarter  Row Groups for Product Category/SubCategory/Product


Demo 2, Step 1 of 7
11
Create a new dataset called dsSales Drag in the following from the metadata on the left:

Date Fiscal Fiscal Year and Fiscal Quarter of Year Product .Category, SubCategory, and Product Measures .Internet Sales Amount

Create a dimension filter parameters at the top (SSRS will automatically generate DataSets for the dropdown):

Customer Customer Geography Make sure to check the Parameters checkbox Visual Studio will generate a hidden dataset with MDX code, to populate a dropdown list We only want the user to make a Geography selection down to the City level. Ideally, we d have a subset hierarchy in the dimension cube but since we don t, we ll need to modify the generated MDX (see next steps)

Demo 2, Step 2 of 7
12

Developer Alert! SSRS has a UFO (Undesirable Feature Object) that we must address

Reporting Services 2008 has default behavior we need to modify In the prior step, we created a parameter for the Geography Hierarchy.

As a result, SSRS automatically generated a second dataset with MDX code to populate a parameter dropdown (and that s a good thing) SSRS (Visual Studio) also allows us to modify the MDX code for that second dataset, if we only want a subset of the hierarchy (and that s also a good thing) However, by default, if we ever go back and modify the first dataset, or modify the new report parameter, Visual Studio will regenerate the MDX dataset code. This will overwrite our custom changes and that s NOT a good thing!!!

Fortunately, there s a workaround. You must do this BEFORE modifying any of the generated MDX code:

Close the report designer, but keep the overall SSRS project open In Solution Explorer, right click on the report, and take the View Code Option For each instance of a </Query>..</DataSet> combination, enter the following:


<rd:SuppressAutoUpdate>true</rd:SuppressAutoUpdate>

Save the code, close the code window and reload the report in the Visual Studio designer

Demo 2, Step 3 of 7
13

After modifying the report RDL to suppress any auto-generation of MDX datasets, we can modify the MDX code in the 2nd dataset. By default, MDX-generated datasets don t appear in the Report Data Window pane. We need to right-click on the data source, and select Show Hidden DataSets. Visual Studio will then display the 2nd hidden dataset (CustomerCustomerGeography). We can right-click on the dataset and modify the query

Demo 2, Step 4 of 7
14

In the generated MDX code, change the ON rows statement to the following:
DESCENDANTS( [Customer].[Customer Geography], [Customer].[Customer Geography].[City], SELF_AND_BEFORE) ON ROWS

Make sure to execute the code (using the icon) to test the result set

Demo 2, Step 5 of 7
15

Drag a Matrix control into the report body Drag the Category column from the Dataset into the Matrix (into the Rows area) Drag the FiscalYear column from the Dataset into the Matrix (into the columns area) Drag the InternetSalesAmount from the Dataset into the Matrix (into the Data area) To add the additional row groups:

Click on the dropdown for Category (in the Row Group area) and add a Group (as a child group) and select the SubCategory as the column for the group. Right-click on the new Subcategory group (which is probably called Group1 ) and go to the properties. Change the name of the group to grpSubCategory. Click on the dropdown for the new Subcategory group (in the Row Group area) and add a group (as a child group) and select the Product as the column for the group. Right-click on the new Product group (which might also be called Group1 ) and go to the properties. Change the name of the group to grpProduct.

To add total sections for the row groups:

For each of the row groups, click the specific dropdown and select Add Total - Before

Demo 2, Step 6 of 7
16

To add the additional column groups:

Click on the dropdown for Fiscal Year (in the Column Group area) and add a Group (as a child group) and select the Fiscal Quarter of Year as the column for the group. Right-click on the new Fiscal Quarter group (which is probably called Group1 ) and go to the properties. Change the name of the group to grpQuarter.

To add total sections for the column groups:

For each of the column groups, click the specific dropdown and select Add Total Before

For each of the open cells in the matrix where you d want a total, click the small grid in the upper right corner and select the Internet Sales amount. (Alternatively, you can drag the sales amount from the DataSet into the cell)

Demo 2, Step 7 of 7
17

To implement drilldown in the row and column area:

First, make sure all the textboxes in the Matrix (for Category Subcategory Product, and Fiscal Year Fiscal Quarter) are named appropriately (e.g. txtSubCategory). The drilldown feature will reference these textboxes by name. For the grpProduct group, right-click, go to properties, go to the Visibility section, and set Hidden to True and Toggleitem to the textbox for SubCategory For the grpSubCategory group, right-click, go to properties, go to the Visibility section, and set Hidden to True and ToggleItem to the textbox for Category.

18

Demo: Example 2
Creating a trend line chart with a 12-month moving average using MDX rpt02_MovingAverageOLAP.rdl

Demo 2: End Result


19

Report shows sales dollar revenue as a column bar and the 12 month moving average as a horizontal line The trend line helps to visually assess if monthly revenue is above or below the 12 month average (at that month) User can select one or more years, and a product category or subcatregory

Demo 2: Steps to reproduce


20

1. 2.

3. 4.

5. 6.

Create a new report (rpt01_TwelveMonthMovingAverage.rdl) Create a new DataSet, selecting Months and two measures (Revenue and 12 month moving average) for the output Define two parameters for Year and Geography Create a chart that will plot the Monthly Revenue as a column bar and the 12 month moving Avg for Revenue as a line, with Months as the X-Axis Customize the chart (colors, legend placement, etc.) Annotate the page headings for the Parameters selected

Demo 2, DataSets and Parameters


21

Report uses the following:


DataSets

dsSales  TimeYear (Generated by SSRS)  CustomerRegionToCity (Generated by SSRS)




Parameters

TimeYear (Generated by SSRS)  CustomerRegionToCity (Generated by SSRS)




Demo 2, Step 1 of 5
22

Create a new dataset Drag in the following from the left:


Time Month Measures .Dollar Sales Measures 12 Month Avg

Create two dimension filter parameters at the top (SSRS will automatically generate DataSets for the dropdowns):

Time .Year (default to 2005) Customer RegionToCity (default to ALL)

For reference, the MDX code for 12 month average uses the MDX functions AVG and LASTPERIODS
avg ( LastPeriods(12,[Time].[Month].PrevMember), [Measures].[dollar Sales])

Demo 2, Step 2 of 5
23

Go to Layout Drop a chart from the toolbox into the report body Stretch the chart as far out as your wish Drag Month from DataSets to the Xaxis (Drag Category Fields here) Drag Dollar Sales and 12 Month Avg from DataSets to Drag Data Fields here

Demo 2, Step 3 of 5
24

Right-click on Chart Properties


Go to Legend Set Layout to Row Click on bottom center for position

Right-click on ID12MonthAvg

Change Series Label to 12 Mnth Avg Go to Appearance .check Plot Data As Line

Right-click on Chart Properties (again)


Go to Plot Area Style Go to FILL Create a Gradient Fill Then go to Y-Axis Enter Revenue for Title and C0 for Format Code

Demo 2, Step 4 of 5
25

For both Parameters, turn multi-value OFF


Under the layout tab, select the Report Menu Select Parameters Note that SSRS already generated the TimeYear and CustomerRegionToCity report parameters, when we created the query parameters (Slide 11) Turn Multi-value OFF for both parameters (by default, SSRS makes OLAP parameters MULTIVALUE)

Note that each of the 2 generated parameters has an associated DataSet that SSRS also generated. If you want to see the DataSets that SSRS generated, go back to the data tab, click the DataSet pulldown, and review the MDX code for the 2 new DataSets

Demo 2, Step 5 of 5
26

Create a Page Heading to annotate the parameter values


Select the Report Menu, select Page Header Create a textbox in the header

Enter the following expression:


= "Revenue and 12 Month Moving Average for & ltrim(Parameters!CustomerRegionToCity.Label) & " for " & ltrim(Parameters!TimeYear.Label)

Finally, to get the data recap below the graph:

Drag a table object from the toolbox, and place it below the chart Drag in the Month, Dollar Sales, and 12 Month Average into the columns (and format the Dollar Sales and 12 Month Average as currency)

You re finished! Go ahead and Preview

Demo 2, Final notes


27

Note that the Region to City dropdown is not a true hierarchical dropdown You can t expand/collapse the regions the dropdown is a flat indented list Not very practical for viewing the hierarchy Two alternatives to get a true hierarchical, tree-like dropdown:

If deploying to a native SSRS server, build an ASP.NET webpage to create a better dropdown If deploying to MOSS (SharePoint Server), MOSS has web parts that allow us to create better dropdowns

28

Demo: Example 3
Create a report that generates a nested MDX TOPCOUNT and an MDX Ranking over a userdefined date range rpt03_TopProductsAndCitiesOLAP.rdl

Demo 3: End Result


29

Report allows the user to see the TOP N Regions by Revenue, and within each region, the TOP Y categories Report allows the user to select a range of months

Demo 3: Steps to reproduce


30

1.

2.

3.

4. 5.

Create a new report (rpt03_NestedTopCountWithDateRange.rdl) Create a new DataSet and manually enter MDX code to deal with a NESTED TOPCOUNT Manually create parameters for the Customer and Product TOPCOUNT limits Create a Parameter for the time Period Define the Layout

Demo 3, DataSets and Parameters


31

Report uses the following:


DataSets

dsSales  MonthStart (generated automatically by SSRS)  MonthEnd (generated automatically by SSRS)




Parameters

MonthStart (generated automatically by SSRS)  MonthEnd (generated automatically by SSRS)  TopRegionCount (unbound parameter, manually created)  TopProductCount (unbound parameter, manually created)


Demo 3, Step 1 of 4
32
1. 2.

Click the DataSet pulldown Create a new dataset (dsSales) Enter the MDX Code: (provide code)
WITH SET [RegionSet] AS Topcount ([Customer].[Region].children,@TopRegionCount , [Measures].[Dollar Sales]) SET [MainSet] AS GENERATE( [RegionSet] , ( [Customer].[Region].CurrentMember, TOPCOUNT( [Product].[Category].Children, @TopProductCount, [Dollar Sales]))) member [RankProd] as RANK( ([Customer].[Region].currentmember, [Product].[Category].currentmember), exists([MainSet], [Customer].[Customer].CurrentMember)) member [RankCust] as RANK( ([Customer].[Region].currentmember), [RegionSet]) select {[Measures].[Dollar Sales], [RankCust], [RankProd]} ON columns, [MainSet] on rows from sales where ( strtomember(@MonthStart) : strtomember(@MonthEnd) )

Demo 3, Step 2 of 4
33
1.

Click the Query Parameters button and enter the following query parameters:
1. 2. 3.

TopRegionCount TopProductCount MonthStart (using the Time/Month Dimension/Hierarchy) MonthEnd (using the Time/Month Hierarchy)

4.

Demo 3, Step 3 of 4
34
1.

Design the report layout


1. 2.

Build a basic group for the Region Add the Ranking numbers for both the Region and Product Build a long header expression based on the parameters

3.

= "Top " & Parameters!TopRegionCount.Value & " Regions and Top " & Parameters!TopProductCount.Value & " Products from & Parameters!MonthStart.Label & " to " & Parameters!MonthEnd.Label

Demo 3, Step 4 of 4
35
1.

Report Parameter Screen


1.

You can mimic a pulldown for the topcounts by placing pairs of sequence values (1, 2, 3, etc.) in the Available values

36

Demo: Example 4
Create a report that retrieves KPIs rpt04_UsingKPIOLAP.rdl

Demo 4: End Result


37

Even if an organization doesn t use SharePoint/PerformancePoint, you can still render KPIs graphically in an SSRS report Important step beforehand:

Create a folder under C:\INETPUB\WWWROOT, called KPIImages (two I s) Save the following six images to that folder:


RedImage.JPG, GreenImage.JPG, YellowImage.JPG DownArrow.JPG, UpArrow.JPG, RightArrow.JPG

Demo 4: Steps to reproduce


38

1. 2. 3.

4. 5. 6.

Create a new report (rpt04_UsingKPIs.rdl) Drag in Product Category, Sales, and KPI information Create Parameter filters for Time/Quarter and Customer Region Lay out the report Create expressions to dynamically load images for KPIs Set report column headings for interactive sorting

Demo 4, DataSets and Parameters


39

Report uses the following:


DataSets

dsSales  TimeQuarter (generated automatically by SSRS)  CustomerRegion (generated automatically by SSRS)




Parameters

TimeQuarter (generated automatically by SSRS)  CustomerRegion (generated automatically by SSRS)




Demo 4, Step 1 of 4
40
1.

Drag in the following from the Cube Browser


1. 2. 3.

Prod Category Dollar Sales and Returns KPI Returns Pct


1.

Value, Status, Trend

2.

Note that the result set doesn t contain images for the KPI Status and Trend just the numeric values We can build images dynamically based on the 3 values Create 2 Parameter Filters for Time/Quarter and Customer/Region

3.

4.

Demo 4, Step 2 of 4
41
1.

Create the layout as shown on the left Create empty Image controls for the KPI Status and Trend
1.

2.

Drop an instance of the image control into the two columns for Status and Trend When the image wizard comes up each time, simply click FINISH (we don t want to use the image wizard) We will use dynamic image capabilities next

2.

3.

Demo 4, Step 3 of 4
42
1.

Create a report Report Pararameter called KPIPicLocation (as a hidden parameter)


1.

Click the Report menu dropdown, select Parameters, and add a new Parameter called KPIPicLocation, and make it hidden Provide a value for the location of the images: http://localhost/KPIImages/

2.

2.

For the KPI Status Image, enter the following formula (click on the image, hit F4 to access the property sheet, and go to the VALUE property) (provide code)
=switch( Fields!KPIReturnsPct_Status_.Value=1, Parameters!KPIPicLocation.Value & "greenimage.jpg", Fields!KPIReturnsPct_Status_.Value=0, Parameters!KPIPicLocation.Value & "yellowimage.jpg", Fields!KPIReturnsPct_Status_.Value=1,Parameters!KPIPicLocation.Value & "redimage.jpg")

Demo 4, Step 4 of 4
43
1.

Repeat the previous step for the KPI Trend Indicator (provide code)
=switch( Fields!KPIReturnsPct_Trend.Value=1, Parameters!KPIPicLocation.Value & "uparrow.jpg", Fields!KPIReturnsPct_Trend.Value=0, Parameters!KPIPicLocation.Value & "rightarrow.jpg", Fields!KPIReturnsPct_Trend.Value=-1, Parameters!KPIPicLocation.Value & "downarrow.jpg")

2. Miscellaneous steps:
Turn off multi-value for report parameters Create a Page Header to annotate the Parameters chosen Turn on column heading interactive sorting

44

Demo: Example 5
Implementing Cascading Parameters with OLAP data rpt05_CascadingParmOLAP.rdl

Demo 5: End Result


45

Report will demonstrate cascading parameters User will select a city, and then SSRS will only show the product SKUs in distribution for the city This could mean the difference between the SKU dropdown being thousands of items, or maybe just a dozen items

Demo 5: Steps to reproduce


46

1. 2.

3.

Create a new report (rpt05_OLAPCascadingParameters.rdl) Create a new DataSet that shows Dollar Sales by Day, with parameters for Customer/City, Product/SKU Define the 2nd Parameter (Product) to only show products that were sold for the account selected (cascading parameter)

Demo 5, DataSets and Parameters


47

Report uses the following:


DataSets

dsSales  CustomerCity (generated automatically by SSRS)  ProductRTProdSKU (generated automatically by SSRS)




Parameters

CustomerCity (generated automatically by SSRS)  ProductRTProdSKU (generated automatically by SSRS)




Demo 5, Step 1 of 3
48
1.

Drag in the following from the Cube Browser


1. 2.

Time .Day Dollar Sales

2.

Create 2 Parameter Filters for Customer/City and Product/SKU

Demo 5, Step 2 of 3
49
1.

Now we need to modify the Product DataSet, so that the Report Parameters will only list those products that were sold for the current selected account Go to the DataSet ProductRTProdSKU (sometimes you don t see the dataset right away click on LAYOUT and then back to DATA) Change the MDX code specifically, change the ON ROWS statement to the following: (provide code)
filter( [Product].[Rt Prod Sku].children, [dollar sales] > 0) ON ROWS FROM ( SELECT ( STRTOSET(@CustomerCity, CONSTRAINED) ) ON COLUMNS FROM [Sales])

2.

3.

Demo 5, Step 3 of 3
50
1.

Build the layout (just a Detail Level for Day and Dollar Sales) Annotate the Parameters at the top

2.

51

Demo: Example 6
Create a report with a Quarter Parameter, show data for current and prior quarter, and annotate both dates rpt06_QuarterTrendOLAP.rdl

Demo 6: End Result


52

Uses OLAP database dsWareMart Use SSRS OLAP Designer for the measures Use custom MDX to set the default for the Quarter Parameter to most recent quarter This report may initially seem easy, but as it turns out, requires a great deal of MDX We need to create MDX DataSets to determine the most recent quarter, and to determine the

previous quarter

Demo 6: Steps to reproduce


53

1. 2.

3. 4. 5. 6.

7.

8.

Create a new report (rpt06_QuarterParameter.rdl) Create a new DataSet and include a Quarter Dimension Parameter Verify the new Quarter Parameter Build a new DataSet to determine the most recent quarter Map the DataSet to the quarter Dimension Parameter Create a new DataSet with MDX code to determine the previous quarter Create an invisible parameter to hold the label for the previous quarter Define the Layout

Demo 6, DataSets and Parameters


54

Report uses the following:


DataSets

dsSales  TimeQuarter (Generated by SSRS)  dsPreviousQuarter  dsQuarterDefault




Parameters

TimeQuarter (Generated by SSRS)  PreviousQuarter




Demo 6, Step 1 of 7
55
1. 2. 3.

Click the DataSet pulldown Create a new dataset (dsSales) Select the Customer Region attribute and drag it into the grid results Select the 2 measures [Dollar Sales] and [DollarSalesPreviousTime] and drag them into the grid results Create the Quarter Parameter
1. 2.

4.

5.

Select the Time/Quarter attribute hierarchy Provide an initial default value (again, we ll change programmatically) Make it a parameter

3.

6.

SSRS adds a new DataSet for the Time/Quarter Parameter (TimeQuarter)

Demo 6, Step 2 of 7
56
1. 2.

Go to the Layout Tab Click the Report Parameters menu pulldown Note that SSRS automatically creates a report parameter (on Quarter) from the previous screen Change the Prompt

3.

4.

Demo 6, Step 3 of 7
57
1.

Must tweak the MDX that the SSRS designer made for the Time/Year Parameter (we don t want see the ALL Member) Go to the TimeQuarter DataSet Change the ON ROWS statement to the following (basically change ALLMEMBERS to Children):
[Time].[Quarter].children ON ROWS

2. 3.

Demo 6, Step 4 of 7
58
1. 2. 3.

Create a new DataSet Call it dsQuarterDefault Will contain one row, for the most recent quarter of available data Go into MDX mode Add the following MDX code: (provide code)
WITH MEMBER [Measures].[ParameterValue] AS [Time].[Quarter].CURRENTMEMBER.UNIQUENAME SELECT [Measures].[ParameterValue] ON COLUMNS , TAIL( FILTER([Time].[Quarter].children, [Dollar Sales]),1) ON ROWS FROM [Sales]

4. 5.

Demo 6, Step 5 of 7
59
1. 2. 3.

Go back to Layout Go back to Report Parameters For the TimeQuarter Parameter:


1.

Change the Default Values to From Query Set the DataSet to dsQuarterDefault Set the Value Field to ParameterValue

2.

3.

This will force the Timequarter dropdown to always default to the most recent quarter

Demo 6, Step 6 of 7
60
1. 2. 3.

Create a new DataSet Call it dsPreviousQuarter Will contain one row, for the previous quarter from the current quarter member Go into MDX mode Click the Query Parameters button ( ) and enter the query parameter for Time Quarter (see lower left of this slide) Add the following MDX code: (provide code)
WITH MEMBER [Measures].[ParameterCaption] AS [Time].[Quarter].CURRENTMEMBER.MEMBER_CAPTION SELECT [Measures].[ParameterCaption] ON COLUMNS , STRTOMEMBER(@TimeQuarter).PrevMember ON ROWS FROM SALES

4. 5.

6.

Demo 6, Step 7 of 7
61
1. 2.

Go back to Report Layout Create a new Report Parameter called PreviousQuarter (we ll use this parameter to capture the value from dsPreviousQuarter)
1.

Check the Hidden checkbox

2.

Change the Default Values to From Query Set the DataSet to dsPreviousQuarter Set the Value Field to ParameterCaption

3. 4.

You might also like