Home > Articles > Data > SQL Server

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

Creating a Calculated Cell in an MDX Query

Another means of creating a calculated cell is via an MDX query. As in the process of creating a calculated member within an MDX query, the WITH clause comes into play. We can also use the WITH clause to define cell properties. Calculated cells can be created via MDX using the CREATE CELL CALCULATION statement as well, but we will focus on the WITH clause approach. This is a typical approach for creating a calculated cell with session scope, in contrast with the global scope one would expect to provide with a calculated cell created by using the Calculated Cells Wizard or by using the CREATE CELL CALCULATION statement.

We begin our creation of a calculated cell by firing up the MDX sample application once again because it provides an excellent platform from which to learn about MDX.

  1. Go to the Start button on the PC; navigate to Microsoft SQL Server, Analysis Services; then to the MDX sample application.

    The Connect dialog box appears, as shown in Figure 15.

    Figure 15Figure 15 The Connect dialog box for the MDX sample application.

Figure 15 depicts the name of my server, MOTHER, and properly indicates that we will be connecting via the MSOLAP provider (the default).

  1. Click OK. (We might also choose to cancel the dialog box and connect later by clicking Connect on the File menu.)

    The MDX Sample Application window appears.

  2. Clear the top area (the Query pane) of any remnants of queries that might appear.

  3. Ensure that FoodMart 2000 is selected as the database name in the DB box of the toolbar.

  4. Select the Warehouse cube in the Cube drop-down list box.

The MDX Sample Application window should resemble that shown in Figure 16, complete with the information from the Warehouse cube displaying in the Metadata tree (left section of the Metadata pane).

Figure 16Figure 16 The MDX sample application window.

We will create an MDX query that defines and creates a calculated cell, and that also returns cell properties as a part of the query result.

  1. Type the following core query into the query pane of the sample application:

    -- Step 2-1, Calculated Cells
    SELECT {[Measures].[Warehouse Cost]} ON COLUMNS, 9
      [Warehouse].[Warehouse Name].Members ON ROWS 9
    FROM Warehouse
  2. Execute the query using the Run Query button.

    The results set appears as shown in Figure 17.

    Figure 17Figure 17 Results set, core query.


  3. Save the query as Step2-1, leaving it open for the next step.

Now we add the WITH clause with the definition of the calculated cell, which we will call "Warehouse Cost Scope" for this exercise.

  1. Remove the comment from the top line of the query (-- Step 2-1, Calculated Cells).

  2. Add the following syntax into the query pane before the SELECT clause:

    -- Step 2-2, Calculated Cells
    WITH CELL CALCULATION [Warehouse Cost Scope] 
     FOR '([Warehouse].[Warehouse Name].MEMBERS)'
    AS 'CalculationPassValue (Warehouse.CurrentMember, 0)',
    CONDITION = 'CalculationPassValue ((Warehouse.CurrentMember, 9 
       [Warehouse Cost]), 0) > 11000'

    The full query should now appear in the query pane, as shown in Figure 18.

    Figure 18Figure 18 Creating the calculated cell via the WITH clause.

The WITH clause contains several of the required elements that are "filled in" by the Calculated Cells Wizard. Let's look at each in turn to reinforce our understanding.

The first part of the WITH clause function functions just like it did for the creation of a calculated member:

WITH CELL CALCULATION [Warehouse Cost Scope], 

It defines the calculated cell (CELL CALCULATION is the syntax), giving it a name (Warehouse Cost Scope). The next line of the WITH clause equates to Step 1 of the Calculated Cell Wizard, and is the point of definition of the first of the three required elements for a calculated cell, the subcube (or cube section/region):

FOR '([Warehouse].[Warehouse Name].MEMBERS)', 

We are defining our subcube as the Warehouse Names members of the Warehouse dimension. (Note that we kept it simple for the exercise: We can have multiple dimensions specified here, together with additional more-sophisticated syntax for other purposes.)

The next line of the clause represents the calculation formula, the second of the three main requirements:

AS 'CalculationPassValue (Warehouse.CurrentMember, 0)', 

The calculation formula is responsible for the ultimate value of the cell once it is evaluated by Analysis Services (and provided that any given cell concerned 1) lies within the subcube definition, and 2) passes the conditional test—if any—imposed by the third main part of the calculated cell definition: the condition statement itself. We are not seeking to change the values themselves, merely to highlight them; therefore, we are basically making the cells targeted "equal to themselves." (See the comments in the earlier section for the corresponding step in the Calculated Cells Wizard for the reasoning behind the use of the CalculationPassValue function.)

The last line of the clause fills the third (optional) requirement in defining the calculated cell, and supplies the calculated cell condition that is imposed upon the subcube cell selection to determine whether the value in the calculation formula is assigned to the respective cells:

CONDITION = 'CalculationPassValue ((Warehouse.CurrentMember, 9
   [Warehouse Cost]), 0) > 11000', 

(The comments in the earlier section surrounding the use of the CalculationPassValue function apply here, as well.)

  1. Execute the query using the Run Query button.

    The results set appears, as shown in Figure 19.

    Figure 19Figure 19 Results set, MDX query Step 2.


It's difficult to tell if any benefit is obtained through our newly created calculated cell: No tangible evidence of its presence appears. We see no highlighting at this stage of creation because we have yet to define the setpoints for the cell properties we wish to put in place to make the highlights appear. We will accomplish this in the following steps.

  1. Save the query as Step2-2, leaving it open for the next step.

  2. Immediately following the CONDITION = 'CalculationPassValue ((Warehouse.CurrentMember, [Warehouse Cost]), 0) > 11000', (ensure that there is a comma after > 11000') line of the WITH clause, insert the following:

    FORE_COLOR = '16711935', BACK_COLOR = '0', 9
       FORMAT_STRING = '$#,##0.00;($#,##0.00', FONT_FLAGS = '3'

This puts into place the cell property setpoints, namely the ForeColor of magenta and the BackColor of black, together with the same Format String that we used previously. For details about these setpoints, please see the discussion surrounding properties in the earlier section.

All that remains is to "enable" the property setpoints by adding a final line to the query. We employ the CELL PROPERTIES keyword, along with one or more intrinsic cell properties: In our case, these are ForeColor and BackColor.

  1. Immediately following the FROM Warehouse line of the SELECT clause, insert the following:

    CELL PROPERTIES VALUE, FORMATTED_VALUE, 9
       FORE_COLOR, BACK_COLOR, FONT_FLAGS

The combination of the CELL PROPERTIES keyword and the properties that follow it, as shown above, is not required. We just need to keep in mind that, by default, if the CELL PROPERTIES keyword is not used, the cell properties returned are VALUE, FORMATTED_VALUE, and CELL_ORDINAL. Also, if the CELL PROPERTIES keyword is used, only those cell properties explicitly stated with the keyword line are returned. So it's basically a case of "if you specify, specify completely."

The full query should now appear in the query pane as shown in Figure 20.

Figure 20Figure 20 Adding the cell properties information to finish.

  1. Execute the query via the Run Query button.

    The results set appears, as shown in Figure 21.

    Figure 21Figure 21 Results Set, MDX Query Step 2


We see the effects of the calculated cell that we have created in action with the appearance of the exception highlighting we defined in the MDX query for individual Warehouses with Warehouse Costs over $11,000.

  1. Save the query as Step2-3, closing the sample application when finished.

In addition to providing exception highlighting, as seen in the two exercises above, calculated cells can provide other functionality. Examples include the following:

  • Customization of query results

  • Restriction of calculated cells–based formulas to precise data subsets (through subcube definition)

  • Efficient and simple subset manipulation

  • Flexibility and portability options

  • Provision of a cube structure layer outside (and independent of) cube physical structure

  • Capacity for multipass evaluation (such as in a "simultaneous equation" scenario in which costs are allocated in more than one step)

An added benefit of using the powerful calculated cells functionality relies upon the fact that they can be configured on the server via Analysis Manager. This can mean the deployment of enhancements to distributed applications and user interfaces with immediate benefit, in many cases, without the immediate necessity of reprogramming the interfaces/applications to take advantage of the new features.

  • + Share This
  • 🔖 Save To Your Account