Home > Articles > Data > SQL Server

  • Print
  • + Share This
Like this article? We recommend

Looking Ahead to Calculated Members

We'll finish this lesson with a further enhancement to the query, and create a simple dynamic calculated member. (In a later article, we'll return to calculated members and expand their uses to include aggregation functions, along with an array of other uses for calculated members and calculated measures.)

We'll begin by changing the query from the previous example. The items to change are shown below in bold:

SELECT {[Store Cost], [Store Sales], [Unit Sales]} ON COLUMNS,
NonEmptyCrossJoin([Store].[Store State].Members,
[Product].[Product Family].Members) ON ROWS FROM Sales

Here's the new version we need. The changed items are again in bold:

SELECT [Measures].Members ON COLUMNS,
NonEmptyCrossJoin([Store].[Store State].Members,
[Product].[Product Family].Members) ON ROWS FROM Sales

In essence, we're changing the query to retrieve all measures as columns, instead of only the three measures specified earlier.

  1. Change the query as indicated above. {[Store Cost], [Store Sales], [Unit Sales]} should become [Measures].Members.

  2. Add comments, if desired, in the Query pane.

  3. Save the query as MDX04-5.MDX.

  4. Click the Run Query button. Figure 35 shows the results.

    Figure 35Figure 35 All measure members appear on the column axis.

    Now we'll add a calculated member to provide an average revenue per unit figure that can be used in very rough comparisons between store states. But first, to see a calculated member in the Results pane, we need to make a further adjustment to the query.

  5. Change the .Members part of the [Measures].Members expression to read .AllMembers. The query will now read as follows:

    SELECT [Measures].AllMembers ON COLUMNS,
    NonEmptyCrossJoin([Store].[Store State].Members,
    [Product].[Product Family].Members) ON ROWS FROM Sales
  6. Click the Run Query button. Figure 36 shows the results.

    Figure 36Figure 36 The calculated members appear on the column axis.

    We now see the existing calculated members—all calculated measures (the most common type of calculated members), and therefore members of the Measures dimension. The .AllMembers function allows us to see these as well as the regular members.

  7. Modify the query to add the Avg Rev Per Unit calculated member by placing the following statement in front of the existing query:

    WITH MEMBER [Measures].[Avg Rev Per Unit] AS '[Store Sales]/[Unit Sales]'

    Figure 37 shows the query's current format.

    Figure 37Figure 37 The modified query.

  8. Click the Run Query button. Figure 38 shows the results.

    Figure 38Figure 38 The report with the new calculated member.

    Our calculated member differs from the other calculated members shown earlier, in that our calculated member is dynamically created in the MDX query. The other calculated members were created in the cube structure, and are thus permanent and available for retrieval by any process with appropriate access.

  9. Let's format (rounding to one-tenth of a cent) the annoying strings of digits spawned by the well-meaning sample application. Add the following instruction to the end of the WITH clause (the added expression is shown in bold):

    WITH MEMBER [Measures].[Avg Rev Per Unit] AS
    '[Store Sales]/[Unit Sales]', format = '$ #,###.000'
  10. Click the Run Query button. Figure 39 shows the final view of the report.

    Figure 39Figure 39 The report with the dynamic calculated member.

The exercises just completed should help to introduce you to the potential within the creation of dynamic calculated members, which can be extended to accomplish far more, including aggregation and other valuable reporting functions. The next article revisits calculated members for an in-depth examination.

  • + Share This
  • 🔖 Save To Your Account