Mdx Basics


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Mdx Basics

  1. 1. Cube Functions - Introduction Dashboard reports are often disjoint, (unlike Pivot Tables). The dashboard is organized in small blocks of tables and charts, spread over the sheet. PivotTables show the data as one adjacent rectangular block of data, with limited formatting capabilities. A cube formula, on the other hand, can be placed everywhere in the Excel grid using the rich Excel formatting. Here a list with Excel cube formulas: Formula Description CUBEMEMBER(connection,member) Member defined by member_name CUBEKPIMEMBE(connection,kpi_name,kpi_type) Member defined by kpi_name CUBEVALUE(connection,member1,member2, …) Get cube value CUBESET(connection,set_expression) Set defined by set_expression CUBERANKEDMEMBER(connection,set_expression,rank) Returns the Nth item from a set What are the new CUBE functions? We have implemented seven new CUBE functions that can be used in Excel formulas just like any other function in Excel. These functions permit Excel to fetch data from SQL Server Analysis Services (2000 & 2005), including any member, set, aggregated value, property, or KPI (Key Performance Indicator) from the OLAP cube. This data can then be placed anywhere in the spreadsheet, intermingled with other local calculations and/or within other formulas. Here are the seven new CUBE functions: CUBEMEMBER (connection, member_expression,[caption]) This function will fetch the member or tuple defined by the member_expression. For example, (from the illustration above,) the formula: =CUBEMEMBER ("Adventure Works", "[Sales Reason].[On Promotion]") returns the member named “On Promotion” from the “Sales Reason” dimension of the Adventure Works cube. CUBEVALUE (connection, [member_expression_1], [member_expression_2], …) This function will fetch the aggregated value from the cube filtered by the various member_expression arguments. For example, the formula: =CUBEVALUE ("Adventure Works","[Measures].[Gross Profit]","[Product]. [Category].[Bikes]","[Date].[Fiscal Year].[FY 2004]") returns the value $5,035,271.22 which is the aggregated amount in the Adventure Works cube for Gross Profit for Bikes in Fiscal 2004.
  2. 2. CUBESET (connection, set_expression, [caption], [sort_order], [sort_by]) This function will fetch the set that is defined by the set_expression parameter. Optional parameters allow you to specify the ordering of the set as well as the caption to be displayed in the Excel cell that contains this formula. (Note that the set itself won’t have a display value.) For example, the formula: =CUBESET ("Adventure Works","[Customer]. [Customer Geography].[All Customers].children","Countries") returns the set of countries in the Customer Geography hierarchy and shows “Countries” as the cell’s display value. CUBESETCOUNT (set) This function returns the number of items in a set. Typically the argument to this function will be a CUBESET function or a reference to a CUBESET function. CUBERANKEDMEMBER (connection, set_expression, rank, [caption]) This function returns the Nth item from a set. This can be very useful when building a Top N (or Bottom N) report in Excel. CUBEMEMBERPROPERTY (connection, member_expression, property) This function returns a property of a member in the OLAP cube. CUBEKPIMEMBER (connection, kpi_name, kpi_property, [caption]) This function returns a KPI (Key Performance Indicator) from the OLAP cube. Samples Basic Reports Report Filters Formula Auto-Complete for CUBE function arguments Formula AutoComplete is a feature that provides a list of values from which to choose as you write formulas. In most Formula AutoComplete scenarios, Excel knows the list of values (formulas, named ranges, table names) that it should display because those values are part of the Excel application. For example, when you start typing a function name, Excel can give you a list of all the other functions that start with the same character(s), as is shown in this screenshot.
  3. 3. (Click to enlarge) In this case, Excel already knows what all the possible function names are that begin with “s”, so it can easily display a list of function names that begin with the specified character(s). Ditto named ranges, UDFs, table names, etc. For many arguments to the new CUBE functions, however, we have a different scenario. Excel does not inherently know about the multi- dimensional database (OLAP cube) from which data is being fetched. In order to provide an auto-complete dropdown, it is necessary for Excel to query the multi-dimensional database or OLAP cube to find out what the set of valid items will be. Here’s an illustration of how this works. In this example, I have already created a connection named “Adventure Works” to the Adventure Works database on SQL Server 2005 Analysis Services. I will start by entering a CUBEMEMBER function that uses the Adventure Works connection, and when I get to the second argument, (which calls for a member_expression,) I will only enter the opening quotation marks that tell Excel I am planning to enter a string. (Click to enlarge) The list of possible values that appear in the dropdown did not come directly from Excel. How’d that work? Excel issued a query to the Adventure Works OLAP cube and displays a list of dimension names from that cube. Let’s say that I choose the Dimension named “Customer” and then enter a period (which is used as a separator by the MDX language).
  4. 4. (Click to enlarge) I see that there are several hierarchies in the Customer dimension. Using the arrow and tab keys, I will select the “Customer Geography” hierarchy and enter another period. (Click to enlarge) The auto-complete drop down shows me that there is a single member at the top of the Customer Geography hierarchy, and that item is “All Customers”. By choosing this item and entering another period, I will see an auto-complete drop down of the children of “All Customers” (which are the countries in which the customers are located). (Click to enlarge) At any point, I can finish the argument by entering the closing quotation marks. The key point that I want to make here is that the Formula AutoComplete feature is providing a mechanism for users to navigate the hierarchies in the multi-dimensional database. Even if you have no prior knowledge of the multi-dimensional database to which you are connecting, Excel’s Formula AutoComplete feature will show you the dimensions in the cube, the hierarchies in each dimension, and the members (and their children and grandchildren, etc.) that are contained in each hierarchy. Also, it can be much faster to use auto-complete to enter CUBE functions into Excel formulas because you only need to identify the item you want from a drop-down list and then hit the Tab key, as opposed to typing the full MDX name for each function parameter.
  5. 5. The name that you get for a member using Excel’s auto-complete will be a fully qualified name because you make a selection at each level of the hierarchy. It is not the only name that could be used, nor is it the special “member unique name” for a member in the cube. When you know that a shorter MDX expression will resolve to the same member, you are free to enter the shorter expression. It’s just that Excel will help you to navigate the namespace of your database when you’re not already familiar with that data. The list of items that is displayed in each case is the list of the first fifty (50) items which match the characters that have been entered so far. As you enter more characters, the auto-complete drop down list is automatically updated. Deconstructing the PivotTable Actually, before I talk about data connections, I want to cover one other way that users can author OLAP formulas ... users can create a PivotTable bound to an OLAP cube and then use the Field List to quickly add all the fields that they care about to thier PivotTable. Then, with a single command, they can convert that PivotTable to a set of formulas, where each formula uses one of the new CUBE functions. Fast and easy. Once the Pivot Table has been converted to formulas, users can insert rows and columns, add their own calcs, and modify their work in all sorts of ways, using the converted PivotTable as a convenient starting point. Note that conversion only works in one direction. While you can convert a PivotTable to Formulas, you cannot convert a set of formulas to a PivotTable. Imagine a report that shows several thousand members of a dimension, each member on one row of the report. (As an example, this might be a report that has data for each of several thousand products, or for each of several thousand customers.) Instead of authoring thousands of CUBE functions, it can be much easier and faster to create a PivotTable that contains the data you want in the report. By bringing a single field into the report (such as Product or Customer) many thousands of values can also appear in that report. The command to “Convert to Formulas”, can be found on “Options” Tab under “PivotTable Tools”. It is located under the command labeled “OLAP tools” as illustrated in the screenshot below (these are beta 1 visuals – not final design).
  6. 6. (Click to enlarge) For a PivotTable that uses a ReportFilter, There are two options when this command is invoked. By default, the row labels, column labels and data area of a PivotTable will be converted to Excel formulas that use CUBE functions. If there was a ReportFilter, that filter will remain as it was and the CUBE functions will refer to it. This permits the filter to be changed and all the formula values will be adjusted accordingly. The other option is to convert the ReportFilter too, so that nothing of the original PivotTable remains. Here’s the dialog that comes up to give you this choice: To illustrate this capability, here is a PivotTable report where the Report Filter has not been converted along with the rest of the report. In this example, users may still dropdown the filter in cell B1 and change its value. When this cell is changed, the rest of the numbers will change with it. (Click to enlarge)
  7. 7. Here is the same report, but in this case, everything was converted, including the ReportFilter. There is no longer a dropdown in cell B1, but if the CUBEMEMBER function in that cell were changed, the rest of the report would reflect that change. (Click to enlarge) That wraps up OLAP formulas. With this series of blog postings, I hope that I’ve illustrated how Excel 12 has added an important new feature that make it an incredibly powerful tool for analysis of OLAP data. With improved PivotTables, new CUBE functions and the ability to convert from a PivotTable to formulas, there are lots of ways to build sophisticated reports based on the analytic data that is more and more commonly stored in multi-dimensional databases such as SQL Server 2005 Analysis Services. MDX stands for Multi Dimensional Expressions. This is a Microsoft proprietary language used to define and manipulate multidimensional cubes. Typical MDX query has a SELECT clause, a FROM clause, and WHERE clause (filter). MDX expressions are used to extract required portions of data from a cube for analysis. MDX also supplies a robust set of functions for the manipulation of retrieved data as well as the ability to extend MDX with user-defined functions. Hierarchical Navigation - Selecting Relative Members (CubeMember) PREVMEMBER, NEXTMEMBER and PARENT PARALLELPERIOD, CLOSINGPERIOD, OPENINGPERIOD Hierarchical Navigation - Selecting Members and Levels (CubeSet) Dimension and Levels MEMBERS CHILDREN DESCENDANTS
  8. 8. Aggregating Values (CubeSet) Simple Sums PERIODSTODATE, YTD, MTD, QTD and WTD LASTPERIODS Filtering and Sorting (CubeSet) Value, Alphanumeric and Natural Sorting FILTER Ranking (CubeSet) TOPCOUNT, TOPSUM and TOPPERCENT Implementing Interactivity with Combo Boxes Charting with Cube Formulas