|Introduction to MDX for PowerPivot Users, Part 5: MDX Queries|
|Written by Chris Webb|
|Wednesday, 19 December 2012 19:24|
Reposted from Chris Webb's blog with the author's permission.
In part 4 of this series (sorry for the long wait since then!) I finished off looking at what you can do with named sets. Now, before I go on to more important topics like Excel cube functions and calculated members I'd like to take a high-level look at what you can do with MDX queries running against PowerPivot - high level, because there's much more to MDX queries than can be covered in a single post and, as I explain below, you probably won't want to do this very often.
So why would I need to write whole queries against a PowerPivot model?
This is a very good question, given that in my opinion 99% of the time you can achieve what you want when building Excel reports using either PivotTables (either with or without named sets) or Excel cube functions. Having said that, the post I wrote a few years ago about binding a table in Excel to an MDX query has been one of the most popular I've ever written, so maybe I'm wrong about how frequently people need to do this.
I'd say that you would probably only want to write your own queries when you needed complete control over the MDX and didn't mind that it made linking the query up to filters or slicers very difficult - for example, if you wanted a list of unpaid invoices, or a list of customers that met some specific criteria, in a dashboard.
Also, when you run MDX queries in Excel you're going to use an Excel table to show the results rather than a PivotTable. This is actually the format you need to use to pass data to other Excel-based tools like like Excel Data Mining Addins (as well as PowerPivot), so writing your own MDX queries might actually save you having to convert to formulas, as Kasper does here, or cutting/pasting in cases like this.
Why use MDX instead of DAX?
From PowerPivot V2, PowerPivot models can be queried in either MDX or the DAX query language (if you want to learn about DAX queries take a look at the posts I wrote on this topic last year, starting here), and if you've already learned a lot of DAX for PowerPivot you're probably going to be more comfortable using DAX queries. However, I know there are a lot of old SSAS-fans out there doing work with PowerPivot who prefer MDX, and there are still a few things that MDX can do that PowerPivot can't, so choosing MDX over DAX is a legitimate choice. Examples would be when you want to pivot your resultset and put something other than measures on columns, or show a calculated member on rows, and I show how to do both of these things below.
How do I display the results of an MDX query in Excel?
As I said, when you display the results of an MDX query in Excel you'll need to use an Excel table to do so. I blogged about a few ways to do this here but there's actually a better way now: using DAX Studio. DAX Studio is a free Excel addin for people who want to write DAX queries against a PowerPivot model, but it can run MDX queries too. Unfortunately it doesn't display any MDX metadata for you to use - only DAX metadata - but it's still a much more convenient way of running MDX queries than doing a drillthrough and then editing the query property of a table.
The DAX Studio documentation gives you a good overview of how to use the tool and I won't repeat that here, but to prove it does work here's a screenshot of an MDX query run against a PowerPivot model:
OK, so get on with it and tell me how to write an MDX query.
The basic MDX query is quite simple. Books online has all the details:
.but really all you need to know is this:
Each MDX query needs a SELECT clause. Inside the SELECT clause you need to define one or two axes, either just a columns axis or a columns axis or a rows axis, and the way you define what appears on an axis is using a set, an object we've seen a lot of in the last few posts in this series. Each MDX query also needs a FROM clause, with the name of the cube that is to be queried; for PowerPivot the name of the 'cube' is always [Model].
Here's an example of a simple MDX query on a PowerPivot model built on Adventure Works DW that returns a measure on columns and three years on rows:
Everything you do on columns, you can do on rows, and vice versa, so:
Using a set of tuples on rows and/or columns gives a crosstabbed effect:
After the FROM clause, you can add a WHERE clause to slice the resultset. Do not confuse the MDX WHERE clause with the SQL WHERE clause: it does something similar but it doesn't directly affect what appears on rows or columns, it filters the values returned inside the query. For example:
.returns sales for Black Bikes for the years 2005 to 2007:
Notice that the Colour Black and the Product Category Bikes don't appear anywhere on rows or columns, but the values that are shown are for Black Bikes nonetheless.
The WITH clause
You can define your own calculated members (which I'll talk about in a future post) and named sets inside a query if you add a WITH clause before your SELECT clause. Here's an example of this:
Here I've defined a named set called [MY YEARS] which I've then used to define what goes on the rows axis, and two calculated members, [Total 2005-7] which returns the subtotal of the years 2005 to 2007, and a new measure [Percent of Total] that shows the percentage that each row makes up of this subtotal. Incidentally, even though DAX can do this kind of subtotalling, it's only in MDX that you can define any calculation you want on any axis in your query.
You might be wondering, looking at the examples above, why the column headers are all in human-unfriendly MDX and why the [Percent of Total] measure hasn't had any formatting applied. You will also notice in this query how the name of the All Member on the [CalendarYear] hierarchy doesn't get returned, and you get a blank row name instead:
This is because, when you run queries that get bound to an Excel table they are returned as flattened rowsets and not cellsets (which is how most SSAS client tools and SQL Server Management Studio returns MDX queries). Basically, this means your nice, multidimensional resultset gets squashed into something tabular - and when this happens, a lot of useful stuff gets lost along the way. Here's the official documentation on how flattened rowsets are generated:
This is a pain, but there's no way around it unless you want to write your own code to render a cellset in Excel unfortunately.
Writing your own MDX queries against a PowerPivot model isn't exactly something you'll need to do every day, but it's a useful addition to your PowerPivot toolbox and I wanted to mention it in this series for the sake of completeness. In my next post I'll be taking a look at MDX calculated members.
Latest Author Articles
- NaturalInnerJoin And NaturalLeftOuterJoin DAX Functions In Excel 2016
- ConcatenateX() DAX Function In Excel 2016
- Optimising SSAS Many-To-Many Relationships By Adding Redundant Dimensions
- If I Could Have New Features In SSAS Multidimensional, What Would They Be?
- A Closer Look At Power Query/SSAS Integration