A Quick Way to Start Learning SQL Server MDX

By:   |   Comments (10)   |   Related: > Analysis Services Development


Problem

Speaking from personal experience, learning and comprehending how to write MDX queries for SQL Server Analysis Services was difficult after spending years writing queries in T-SQL for the relational engine. I browsed through books and websites, and I also attended a class, but nothing seemed to "make it click" with me until I stumbled upon the simple solution that is presented in this tip.

Solution

The solution presented here was created in Visual Studio, but similar steps can be performed in Report Builder. The premise is to use the drag-and-drop functionality in the Query Designer, and then switch to the text editor to see the auto-generated MDX query.

The first step is to start the Report Wizard and select your Analysis Services data source. In the Design the Query page, select "Query Builder..." as shown in the figure below.

Visual Studio Report Wizard Query Builder

In this example, drag the measures Internet Sales Amount and Internet Order Quantity from the Measures section onto the palette.

In Visual Studio Query Designer, drag the measures Internet Sales Amount and Internet Order Quantity from the Measures section onto the palette

Secondly, choose the Gender attribute of the Customer dimension and filter where the gender equals Female. Notice how the values for Internet Sales Amount and Internet Order Quantity have been reduced.

In Visual Studio Query Designer, choose the Gender attribute of the Customer dimension

Next, choose the Country attribute of the Geography dimension and filter where the country does not equal the United States.

In Visual Studio Query Designer, choose the Country attribute of the Geography dimension

Then, choose the Calendar Year attribute of the Delivery Date role-playing dimension and filter where the range is the calendar years 2005 through 2007 inclusive.

In Visual Studio Query Designer, choose the Calendar Year attribute of the Delivery Date role-playing dimension

Next, drag the Education attribute of the Customer dimension onto the palette to slice the measures by the education level.

In Visual Studio Query Designer, drag the Education attribute of the Customer dimension

Now, drag the Home Owner attribute of the Customer dimension onto the palette to slice the measures by the education level and the home owner attributes.

Finally, click on the Design Mode icon indicated by the arrow in the image below. This will display the MDX query in a textbox for editing.

In Visual Studio Query Designer, click on the Design Mode icon to see the MDX code generated

Please note that the query in the textbox is not formatted for easy viewing.

In Visual Studio Query Designer, the query in the textbox is not formatted for easy viewing

With some strategically placed CR/LF's, the query can be viewed more easily.

In Visual Studio Query Designer, with some strategically placed CR/LF's, the query can be viewed more easily

Removing the WHERE clause does not affect the outcome of the query.

In Visual Studio Query Designer, as a means to test, removing the WHERE clause does not affect the outcome of the query

In the following code sample, I have added line numbers at the end of each line to help explain the syntax of this query.

  • Line 1 is the SELECT command.
  • Lines 2 and 7 are the NON EMPTY operators which prevent the return of null values for the measures and for the dimensions respectively.
  • Lines 4 and 5 are the measures (sometimes these are called facts) that we are selecting from the Analysis Services cube. Please note that the measures are separated by a comma just like columns are separated by a comma in an SQL statement.
  • Line 6 is where we specify to place the measures in columns.
  • Lines 9 and 10 are where we specify the dimensional attributes on which to slice the measures. Please note that the dimensional attributes are separated by an asterisk (*).
  • Line 11 is where specify to place the dimensional attributes on rows.
  • Lines 12 and 13 are used to specify the range of the calendar years from the Delivery Date role-playing dimension. Please note the colon (:) is used between the low and high values of the range.
  • Line 14 is used to remove all values where the country is equal to the United States. Please note the minus sign (-) before the curly bracket after the word SELECT.
  • Line 15 is used to select where the Gender attribute of the customer dimension equals F for Female.
  • Line 16 specifies that we are selecting from the Adventure Works cube. Please note the three closing parentheses at the end of the line match the number of FROM ( SELECT statements in lines 12, 14 and 15.
  • Line 17 is the WHERE clause. In this example, removing the WHERE clause does not affect the results of the query.

    SELECT                                                                                   //01
    NON EMPTY                                                                                //02
    {                                                                                        //03
      [Measures].[Internet Sales Amount],                                                    //04
      [Measures].[Internet Order Quantity]                                                   //05
    } ON COLUMNS,                                                                            //06
    NON EMPTY                                                                                //07
    { (                                                                                      //08
      [Customer].[Education].[Education].ALLMEMBERS *                                        //09
      [Customer].[Home Owner].[Home Owner].ALLMEMBERS                                        //10
    ) } ON ROWS                                                                              //11
    FROM(SELECT ([Delivery Date].[Calendar Year].&[2005]:                                    //12
                [Delivery Date].[Calendar Year].&[2007]) ON COLUMNS                          //13 
    FROM(SELECT ( -{ [Customer].[Customer Geography].[Country].&[United States]}) ON COLUMNS //14
    FROM(SELECT ( { [Customer].[Gender].&[F] } ) ON COLUMNS                                  //15
    FROM [Adventure Works])))                                                                //16
    WHERE ( [Customer].[Gender].&[F] )                                                       //17
    
    Next Steps
    • After creating the MDX query in Visual Studio or Report Builder Query Designer, copy and paste the MDX query into a SQL Server Management Studio query editor window. Experiment with subtle changes to see what works and what doesn't work. This should help to build your confidence and ability to write MDX queries.
    • Please refer to the following tips for further assistance with MDX:


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Dallas Snider Dr. Dallas Snider is an Assistant Professor in the Computer Science Department at the University of West Florida and has 18+ years of SQL experience.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Tuesday, March 21, 2017 - 11:20:08 AM - Ken W Back To Top (51505)

You can also do similar testing in SQL Server Management Studio by connecting to a Analysis Services server, drilling down to the cube level in Object Explorer and then right clicking and choosing Browse. That opens the drag and drop interface. Build out a query similar to what Dr. Snider supplied here and then choose the Design Mode icon and you will get the MDX query. This way you don't have to have SSRS installed to learn MDX. *Note - tested on SSMS 2014 and 2016. I cannot speak for older versions although I am pretty sure 2012 supports this as well.

 


Tuesday, April 19, 2016 - 3:00:07 AM - Manoj Back To Top (41264)

 

Hi Dallas,

Thanks , for sharing such a beautiful article

it has helped me alot, Thanks once again.

 

Rgds,

Manoj


Tuesday, October 20, 2015 - 12:28:29 PM - Ramanjulu Back To Top (38944)

 

Hi Dallas its very good post....................your post made so easy for beginners in MDX queries.

 

Thanks

Ram

 

 

 


Wednesday, December 17, 2014 - 6:12:52 PM - shairal Back To Top (35650)

Excellent tip - very helpful! I have really struggled with this.  It reminds me when I was learning t-sql (eons ago) by using MS Access and viewing the sql code.


Sunday, August 31, 2014 - 10:28:58 PM - Farida Back To Top (34336)

Hi;

I have created reporting projec in visual studio and applied filter to query (like customer gender = female, as shown in setp 2 of your demo). when i port thie project to another computer where the cube project is already deployed. i change the connection string of data srouce for VS project but then loose all the filter applied to report (cube queries). what is the cause of this issue and is there any work around. at the moment after deploying my solution to another machine i juct reconfigure data connection and query filters but its so annoying and time taking. any solution?


Friday, February 14, 2014 - 1:09:31 AM - Yashawant Back To Top (29441)

Thank you , this is very helpful tip.


Wednesday, February 5, 2014 - 9:40:00 AM - Shovan Mukherjee Back To Top (29341)

You shown me a new way & infact the simpliest way to learn MDX query without consulting any e-book,that is never possible in Cube browser to see the generated MDX in backend. Great work Dallas Snider !! Thank You.


Tuesday, February 4, 2014 - 1:39:15 PM - Shovan Back To Top (29331)

Can we use FILTER function in MDX to filter a set based on some Dimension Attribute Member?....My MDX is not giving the same o/p as its shown in Cube browser can you help me on this.

https://skydrive.live.com/#cid=A7526CE822AF1B61&id=A7526CE822AF1B61%21107&v=3


Sunday, December 15, 2013 - 10:51:50 PM - Ola Back To Top (27803)

thanks for the tip, it was really helpful. I was working with dates and finance measures, I want to find the difference between two periods. What function can I use.

Thanks


Tuesday, October 22, 2013 - 5:20:37 PM - ivan Back To Top (27232)

Hi Dallas,

Awesome tip!  Thanks.

 

Ivan















get free sql tips
agree to terms