Book Image

MDX with SSAS 2012 Cookbook - Second Edition

Book Image

MDX with SSAS 2012 Cookbook - Second Edition

Overview of this book

MDX is the BI industry standard for multidimensional calculations and queries. Proficiency with this language is essential for the realization of your Analysis Services' full potential. MDX is an elegant and powerful language, and also has a steep learning curve.SQL Server 2012 Analysis Services has introduced a new BISM tabular model and a new formula language, Data Analysis Expressions (DAX). However, for the multi-dimensional model, MDX is still the only query and expression language. For many product developers and report developers, MDX is the preferred language for both the tabular model and multi-dimensional model. MDX with SSAS 2012 Cookbook is a must-have book for anyone who wants to be proficient in the MDX language and to enhance their business intelligence solutions.MDX with SSAS 2012 Cookbook is packed with immediately usable, practical solutions. It starts with elementary techniques that lay the foundation for designing advanced MDX calculations and queries. The discussions after each solution will provide you with a solid foundation and best practices. It covers a broad range of real-world topics and solutions and provides you with learning materials to become proficient in the language.This book will guide you through the hands-on and practical MDX solutions, best practices, and many intricacies that hide within the MDX calculations and queries. We will start by working with sets, creating time-aware, context-aware calculations, and business analytics solutions, through to the techniques of enhancing the cube design when MDX is not enough. We will then move on to capturing MDX generated by SSAS front-ends and using SSAS stored procedures, and we will explore the whole range of MDX solutions for real-world BI projects.  
Table of Contents (16 chapters)
MDX with SSAS 2012 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using a WHERE clause to filter the data returned


A WHERE clause in MDX works in a similar way as the other query languages. It acts as a filter and restricts the data returned in the result set.

Not surprisingly, however, the WHERE clause in MDX does more than just restricting the result set. It also establishes the "query context".

Getting ready

The MDX WHERE clause points to a specific intersection of cube space. We use tuple expressions to represent cells in cube space. Each tuple is made of one member, and only one member, from each hierarchy.

The following tuple points to one year, 2008 and one measure, the [Internet Sales Amount]:

( [Measures].[Internet Sales Amount],
  [Date].[Calendar Year].&[2008]
)

Using a tuple in an MDX WHERE clause is called "slicing" the cube. This feature gives the WHERE clause another name, slicer. If we put the previous tuple in the WHERE clause, in MDX terms, we are saying, "show me some data from the cube sliced by sales and the year 2008".

That is what we are going to do next.

How to do it…

Open the Query Editor in SSMS, and then follow these steps to write a query with a slicer and test it:

  1. Copy this initial query into the Query Editor and run the query. You will see the following result:

    SELECT
       { [Customer].[Customer Geography].[Country]
       } ON 0,
       { [Product].[Product Categories].[Category] } ON 1
    FROM
       [Adventure Works]
  2. At this point, we should ask the question, "What are the cell values?" The cell values are actually the [Measures].[Reseller Sales Amount], which is the default member on the Measures dimension.

  3. Add the previous tuple to the query as a slicer. Here is the final query:

    SELECT
       { [Customer].[Customer Geography].[Country]
       } ON 0,
       { [Product].[Product Categories].[Category] } ON 1
    FROM
       [Adventure Works]
    WHERE
       ( [Measures].[Internet Sales Amount],
         [Date].[Calendar Year].&[2008]
       )
  4. The result should be as shown in the following screenshot:

  5. Ask the question again, "What are the cell values?" The cell values are now the [Measures].[Internet Sales Amount], and no longer the default measure.

How it works…

We can slice the data by pointing to a specific intersection of cube space. We can achieve this by putting a tuple in the WHERE clause.

In the preceding example, the cube space is sliced by sales and year 2008. The cell values are the Internet Sales Amount for each country and each product category, sliced by year 2008.

There's more…

Notice that the data returned on the query axes can be completely different from the tuple in the WHERE clause. The tuples in the slicer will only affect the cell values in the intersection of rows and columns, not what are on the columns or rows axes.

If you need to display sales and year 2008 on the query axes, you would need to move them to the query axes, and not in the WHERE clause.

This query has moved the sales to the columns axis, and the year 2008 to the rows axis. They both are "crossjoined" to the original hierarchies on the two query axes:

SELECT
   { [Measures].[Internet Sales Amount] *
     [Customer].[Customer Geography].[Country]
   } ON 0,
   { [Date].[Calendar Year].&[2008] *
     [Product].[Product Categories].[Category]
   } ON 1
FROM
   [Adventure Works]

Run the query and you will get the following result. The call values are the same as before, but now we have the year 2008 on the rows axis, and the Internet Sales Amount on the columns axis.