How to filter multidimensional OLAP cubes in SSRS reports

How to filter multidimensional OLAP cubes in SSRS reports

How to filter multidimensional OLAP cubes in SSRS reports

SQLShack

SQL Server training Español

How to filter multidimensional OLAP cubes in SSRS reports

September 28, 2016 by Sifiso Ndlovu Ever since the early days of my career, SQL Server Reporting Services (SSRS) has been one of my preferred data visualization tools simply because end users and developers alike use it for free. Although a majority of my SSRS solutions have been based off a relational dataset that uses Transact SQL (T-SQL), I have also produced several reports that used Multidimensional Expressions (MDX) to connect and retrieve data from SQL Server Analysis Services (SSAS) multidimensional OLAP cube. Recently, I found myself having to refactor some of these SSAS based SSRS reports, particularly converting a single value SSAS-populated parameter into a multi-value parameter. In this article, I explore how you can go about making these changes using SSRS query designer’s design and query modes.

Working with query parameters in design mode

Just like using a relational database to define your report dataset requires an understanding of T-SQL, referencing multidimensional OLAP cube data requires a basic understanding of MDX. However, you can circumvent the MDX learning process by using a GUI – Graphical User Interface – to build and prepare your report dataset. In the context of SSRS, the GUI includes a dataset query designer’s design mode. It involves navigating to a new dataset, choosing your SSAS data source and then clicking and dragging the measures and dimensions you want to report on. In my FruitSales demo cube shown in Figure 1, I have chosen Count as my measure and Fruit and MOP (method of payment), as my dimensions.
Figure 1 Adding parameters to our dataset using the designer is also effortless. All you need to do is navigate to the top right filter section of the designer, specify the dimension you want to filter on and check the box underneath the Parameters field. In this example, I have chosen to use MOP as a filter with a default value of CASH. The rest of the dataset looks as shown in Figure 2.
Figure 2 Once you close the query designer, you get to see an MDX script that was generated based on the selections you made. The script generated based on Figure 2 is shown in Script 1. 123456789101112131415161718 SELECT non empty { [Measures].[Count] } ON columns, non empty { ([DimFruits].[Fruit].[Fruit].ALLMEMBERS * [DimFruits].[MOP].[MOP].ALLMEMBERS ) } dimension properties member_caption, member_unique_name ON rows FROM ( SELECT ( strtoset(@DimFruitsMOP, constrained) ) ON columns FROM [FruitsSales]) cell properties value, back_color, fore_color, formatted_value, format_string, font_name, font_size, font_flags Script 1: Generated MDX Script Having successfully set up a SSAS dataset using GUI, we can proceed to develop a report and Figure 3 shows us a preview of the newly developed report.
Figure 3 Although the GUI makes it easy for novice developers to set up a parameterized SSRS report that is based off a multidimensional cube, there are several drawbacks to this approach: The auto-generated script (in Script 1) includes unnecessary formatting information (i.e. BACK_COLOR, FORE_COLOR) that is embedded in the query – which makes the script untidy and not easy to read. The dataset parameter name (and subsequently, report parameter name) is not business friendly as it is derived from a concatenation of dimension name (DimFruits) and dimension member (MOP). Another issue relating to parameter is that parameter values contains an MDX notation (i.e. [Dimension].[Member].&[Value]) that is not business friendly.

Working with query parameters in query mode

The drawbacks mentioned in the preceding section can be resolved by writing your own MDX script which gives you control of the dataset and ultimately the report. To edit MDX script we will switch from design mode into query mode. Single-value parameters using STRTOMEMBER MDX has several built-in functions that can be useful when it comes to parameterising query datasets. One of those functions is the STRTOMEMBER which returns a member specification. Script 2 shows a refactored version of Script 1 that makes use of the STRTOMEMBER function. 123456789 SELECT NON EMPTY { [Measures].[Count] } ON COLUMNS,NON EMPTY{ ( [DimFruits].[Fruit].[Fruit].AllMembers ) * { StrToMember ( @MOP ) }} ON ROWSFROM [FruitsSales] Script 2: MDX Script Using STRTOMEMBER Script 2 makes references to a parameter called @MOP. Unlike in design view, this parameter will not be automatically added into the dataset instead we have to manually add it. Figure 4 shows the definition of the @MOP parameter with a default value of CASH.
Figure 4 Following the changes made to our shared dataset, a preview of our demo report is shown in Figure 5. In terms of the layout, not much has changed since we updated the script to use STRTOMEMBER except for the parameter name which is no longer prefixed by a dimension name.
Figure 5 Evidently, we still have an issue of a predefined parameter value that contains MDX expression. We need to refactor this in such a way that it displays member value only (i.e. CASH, ELECTRONIC etc.). We can do this by having the MOP parameter populated by a separate lookup dataset. Script 3 shows an MDX script for a dataset that will be used to populate the MOP parameter. 1234567891011121314 WITH MEMBER [Measures].[Label] AS [DimFruits].[MOP].currentmember.member_caption MEMBER [Measures].[Value] AS [DimFruits].[MOP].currentmember.uniquename SELECT { [Measures].[Label], [Measures].[Value] } ON COLUMNS, [DimFruits].[MOP].allmembers ON ROWS FROM [FruitsSales] Script 3: MOP Lookup MDX Query The updated report which references the newly added dataset is shown in Figure 6. As it can be seen, this view of the report allows end users to choose the method of payment without having to specify an MDX expression.
Figure 6 Multi-value parameters using STRTOSET STRTOMEMBER function resolved several limitations of an MDX script generated via design mode. Unfortunately, STRTOMEMBER function has its own limitation which is – it doesn’t allow us to choose multiple parameter values. For instance, if we attempt to choose multiple MOPs, we receive an error as shown in Figure 7.
Figure 7: STRTOMEMBER function expects a member expression error In order to address this limitation of STRTOMEMBER function, we make use of another MDX built-in function –STRTOSET. The revised script that uses STRTOSET function is shown in Script 4. 1234567 SELECT non empty { [Measures].[Count] } ON columns, non empty { ([DimFruits].[Fruit].[Fruit].ALLMEMBERS) * {strtoset(@MOP)} } ON rows FROM [FruitsSales] Script 5: MDX Script Using STRTOMEMBER The MOP parameter was also modified to include multi-values as shown in Figure 8.
Figure 8 A preview of the updated report that uses the script that references STRTOSET is shown in Figure 9. As it can be seen, it is now possible to choose multiple methods of payments without breaking report execution.
Figure 9

Conclusion

In this article, we have demonstrated that unlike using T-SQL, populating an SSRS dataset using a multidimensional OLAP cube requires, at the very least, a basic understanding of MDX. We also demonstrated that out of the two modes, design and query mode, the query mode gives you more control of the dataset properties and handling of query parameters using the STRTOMEMBER and STRTOSET built-in MDX functions.

References

StrToMember (MDX) StrToSet (MDX) Multidimensional Expressions (MDX) Author Recent Posts Sifiso NdlovuSifiso is Data Architect and Technical Lead at SELECT SIFISO – a technology consulting firm focusing on cloud migrations, data ingestion, DevOps, reporting and analytics. Sifiso has over 15 years of across private and public business sectors, helping businesses implement Microsoft, AWS and open-source technology solutions. He is the member of the Johannesburg SQL User Group and also hold a Master’s Degree in MCom IT Management from the University of Johannesburg.

Sifiso's LinkedIn profile

View all posts by Sifiso W. Ndlovu Latest posts by Sifiso Ndlovu (see all) Dynamic column mapping in SSIS: SqlBulkCopy class vs Data Flow - February 14, 2020 Monitor batch statements of the Get Data feature in Power BI using SQL Server extended events - July 1, 2019 Bulk-Model Migration in SQL Server Master Data Services - May 30, 2019

Related posts

SSRS Report Builder introduction and tutorial Using multi-value parameters in SSRS Convert SQL Server results into JSON Report filtering: Excel slicer vs SQL Server Reporting Services (SSRS) parameters SQL Server Business Intelligence features – creating reports based on OLAP cubes 11,988 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ▼Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ►Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ►Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ►Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!