DAX vs. MDX: An example

02.03.2013 Hilmar Buchta

SQL Server 2012

I’m often asked about the key differences between DAX and MDX or in more general, the difference between the tabular and the multidimensional model. From my presentation on the PASS Camp 2012 in Germany I’d like to share some of the ideas here.

From the perspective of an expression or query language, one of the most important differences is the concept behind both approaches.

For a cube, we have the concept of a tuple for addressing a cell in the cube space. The axis in the tuple are setting the coordinates. If we have a single tuple, the result is the content of the corresponding cell in the cube. As cube attributes have an All-member which serves as the default (in most cases) if the attribute is omitted from the tuple, we also get aggregated this way. For example, a tuple like


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

returns the (aggregated) sales amount for the year 2013. Other attributes (for example Product) are on their default members. As you see, there is no need to supply an aggregation function in this case (although MDX has such functions when aggregating values over sets) as the cube contains the definitions about how to aggregate the sales amount. The following sketch illustrates this way, of addressing a value in a cube:


For the tabular model, filters within our pivot table work like filters on the underlying tables. Even with a single selected value, multiple rows can be included in the filter of a table. For example, if you filter the Year the 2013, the underlying date table will be filter to all 365 days of that year. For a given measure all filtered tables are intersected giving the detail values that correspond to the filter. As there are potentially many rows of data after this process, an aggregation function is required to compute the aggregate. This is shown in the following illustration:


If you are more familiar with SQL than with MDX, the concept of filtering and aggregating in DAX will be more familiar to you. In SQL, as in DAX, we’re usually restricting table rows (using the WHERE clause in SQL or the FILTER function in DAX). Then we create groups (using the GROUP BY clause in SQL or the SUMMARIZE function in DAX) and finally we’re computing the aggregates using an appropriate aggregation function (like SUM).

However, many questions that require this process in SQL or DAX can be solved in MDX just by addressing cells. Let my provide you with an example that I frequently use during my MDX trainings: The requirement is to create a computed measure that gives the sales amount of sales on weekend. If you have a strong SQL background your solution in MDX may look like this:

  1. Aggregate(      
  2.     filter(      
  3.         descendants(      
  4.             [Date].[Calendar].currentmember,      
  5.             [Date].[Calendar].[Date]      
  6.         )      
  7.         ,      
  8.         [Date].[Day of Week].currentmember IS [Date].[Day of Week].[Sunday]      
  9.         or [Date].[Day of Week].currentmember IS [Date].[Day of Week].[Saturday]      
  10.     )      
  11.     ,[Measures].[Internet Sales Amount]      
  12. )      

This seems to be a natural approach. Using the descendant function we create a set of all the dates on the selected date member (for example a month, a quarter, a year). We then filter those dates using the filter function, so that only Sunday and Saturday remains in the set. Finally we aggregate the measure sales amount along this set.

Actually this way of approaching this calculation is very similar to SQL or DAX. For example in DAX we would write the same calculation in almost exactly the same way:

  1. evaluate(      
  2.     summarize(       
  3.         filter(      
  4.             ‚Internet Sales‘      
  5.             , related(‚Date‘[Calendar Year])=2007      
  6.         )                   
  7.         , ‚Date‘[Month]      
  8.         , „Umsatz“      
  9.         , Sum(‚Internet Sales‘[Sales Amount])      
  10.         , „UmsatzWE“      
  11.         , Calculate(      
  12.             Sum(‚Internet Sales‘[Sales Amount])      
  13.             , Or(      
  14.                 ‚Date‘[Day Name Of Week]=„Sunday“      
  15.                 , ‚Date‘[Day Name Of Week]=„Saturday“      
  16.             )      
  17.          )      
  18.     )      
  19. )

Although this looks very similar to the MDX code from above, the MDX code above is close the most complicated solution available. Since the weekday is a cube attribute, we can simply address sales on weekends but using a tuple (ok, a sum of two tuples):

  1. ([Measures].[Internet Sales Amount], [Date].[Day Name].[Sunday])      
  2. +      
  3. ([Measures].[Internet Sales Amount], [Date].[Day Name].[Saturday])

So when writing a DAX query, we rather think

  • How do I filter my underlying tables
  • What aggregation function do I need

In MDX on the other hand, we rather think

  • What axis do I have to address in order to pull my value out of the cube

Your email address will not be published. Required fields are marked *

Join #teamoraylispeople

Gestalte mit uns
die Welt der Daten