Home > Articles > Data > SQL Server

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

About Creating a Calculated Cell

We can create calculated cells with the Calculated Cell Wizard or the Cube Editor, both of which are accessed from within Analysis Services. Calculated cells originated in this way can be created with a global scope, meaning that they will be "permanently" available to all information consumers who browse the cube, much like a calculated member created within the cube superstructure.

We can also create calculated cells with a session scope, meaning that they are available only to MDX queries executed within the session in which they are born. These calculated cells are essentially creatures of the MDX query, and they provide a higher level of flexibility for client applications (among other uses).

We will examine the steps involved in creating calculated cells in each of the two ways and then we will focus on the calculated cell in the MDX query for the remainder of the lesson.

Creating a Calculated Cell with the Calculated Cells Wizard

Creating a calculated cell within Analysis Services will serve to introduce us to general concepts from the perspective of the Calculated Cell Wizard, in which the process of creation is largely guided and user-friendly. This brief overview with the wizard will thus afford us an opportunity to focus on the attributes and characteristics of the calculated cell before getting involved in the MDX syntax.

Creating the Basic Calculated Cell

We will start Analysis Services, navigating to the Warehouse cube, which is supplied as a sample when we install Analysis Services.

  1. Start Analysis Manager (Start, Programs, Microsoft SQL Server, Analysis Services, Analysis Manager).

  2. Expand the Analysis Servers folder by clicking the + sign to its left.

  3. Expand your server (typically named the same as your PC, but determined by the installation/setup) and then expand the FoodMart 2000 database (see Figure 1).

    Figure 1Figure 1 Navigate to the FoodMart2000 database in Analysis Manager.

  4. Expand the FoodMart2000 database by clicking on the + sign to its left.

  5. Expand the Cubes folder (as shown in Figure 2) by clicking the + sign to its left.

The cubes appear, similar to those shown in Figure 2.

Figure 2Figure 2 Sample cubes provided with the Analysis Services installation.


  1. Right-click the Warehouse cube; then click Edit from the context menu.

    The Cube Editor appears.

First, let's set the stage for the steps to come. We will be performing an analysis that focuses on individual surrounding warehouses, so let's set up the data viewing pane to conform to our needs.

  1. Click the Data tab on the right half of the screen.

  2. Drag the Warehouse dimension to the rows area (the left column of the lower pane section), dropping the icon onto the current column heading.

  3. Ensure that the 1997 Time dimension is displayed in the filter box atop the preview pane, indicating that we are filtering on 1997.

  4. Double-click the USA Country member to expand the hierarchy and show the underlying states.

  5. Double-click the State Province column heading to drill down to display members of the City level.

  6. Double-click the City column heading to drill down to display individual Warehouse Names.

  7. Scroll over so that the Warehouse Cost measure appears alongside the individual warehouse members.

    The Cube Editor preview pane should appear (as partially shown in Figure 3).

    Figure 3Figure 3 Cube Editor preview pane (partial view) after adjustments.

  8. Select Insert from the top menu.

  9. Click Calculated Cells on the Insert menu (as shown in Figure 4).

    Figure 4Figure 4 Select Calculated Cells to initialize the Calculated Cells Wizard.


    The Calculated Cells Wizard Welcome dialog appears, as shown in Figure 5.

    Figure 5Figure 5 The Calculated Cells Wizard welcome dialog box.


  10. Click Next.

    The Define the Calculation Subcube dialog box appears.

Let's focus now on the capabilities of the calculated cell to perform exception highlighting. For this example, we will create a calculated cell that highlights all USA warehouse locations with Warehouse Costs exceeding $11,000 for 1997. The first step of defining the new calculated cell is to define the range of cells in the cube, also known as the subcube (or cube region) upon which the calculated cell will act.

  1. Highlight/select the Warehouse dimension from the list on the left side of the Calculation Subcube dialog box.

  2. From the Members Set drop-down selector on the right side of the dialog box, select A single level, as shown in Figure 6.

    Figure 6Figure 5 The Calculated Cells Wizard welcome dialog box.


  3. Expand the Warehouse dimension tree, and select Warehouse Name. The dialog box appears as shown in Figure 6.

  4. Click Next.

    The Define the Calculation Condition dialog box appears.

At this stage, the wizard prompts for the calculated cell condition. We want to put highlighting in place for warehouse locations with Warehouse Costs exceeding $11,000 for 1997. To establish the condition for the application of highlighting, we will input the statement at this stage.

  1. Click the second radio button (Apply the Calculation Formula to Cells in the Calculation Subcube that Meet the Following Conditions) to select it.

  2. Enter the following into the MDX Expression text box:

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

Our condition formula uses the CalculationPassValue() function to ascertain the value of the cell before we apply the calculated cell. We are using this approach as a means of preventing the expression from being recursive, or referencing itself, as would normally be the case in a situation like ours (where the condition expression reads the value of the current cell, which itself references the calculation formula again). Because multiple passes are required for evaluation of MDX expressions, and because we know that evaluations of calculated cells will occur beyond pass 0, we tell Analysis Services to stop the evaluation at pass 0 (by placing 0 in the second argument position) and return the associated value. This mainly serves to prevent the expression from referencing itself ad infinitum, as if in a loop.

The completed Define the Calculation Condition dialog box appears, as shown in Figure 7.

Figure 7Figure 7 The completed Define the Calculation Condition dialog box.


  1. Click Next.

    The Define the Calculation Formula dialog box appears.

At this point, we are prompted to enter the expression for the calculation itself. The outcome of this formula will be the value that is assigned to the cells within the selected range after evaluation of those cells via the condition we have assigned the calculated cell. Remember that the condition is a logical expression, and therefore evaluates to True or False; it is applied to every cell in the range defined in the calculation subcube. For any cell for which the condition evaluates to True, the result of the calculation formula applied to that cell is returned when the cell is queried. A false condition, by contrast, returns the actual value of the cell when it is queried. (Much of the power in calculated cells is that no real alteration of the data itself is taking place).

  1. Enter the following into the MDX Expression text box:

    CalculationPassValue(Warehouse.CurrentMember, 0)

    The completed Define the Calculation Formula dialog box appears, as shown in Figure 8.

    Figure 8Figure 8 The completed Define the Calculation Formula dialog box.


In this example, we do not provide a surrogate value for cells that meet the condition, but use the condition as a basis for the application of exception highlighting only. Therefore, the address of the current cell is appropriate. We use the CalculationPassValue() function to prevent the expression from referencing itself once again (via the same logic we used in the calculation formula in the last step.

  1. Click Next.

We arrive at the Finish the Calculated Cells Wizard dialog box. We provide the name Warehouse Cost Subanalysis Scope, to define a list of warehouses whose level of costs exceeds the threshold of our scope for further examination. We see a summary of the parameters we have assigned through the wizard for the calculation subcube (defaults were assigned for dimensions that we did not specify). In addition, we see a confirmation that the calculation condition has been specified.

    The Finish the Calculated Cells Wizard dialog box appears, as shown in Figure 9.

    Figure 9Figure 9 The completed Finish the Calculated Cells Wizard dialog box.


  1. Click Finish.

At this point in the calculated cells creation process, we have a new calculated cell that has no display criteria. We should see the calculated cell Warehouse Cost Subanalysis Scope in the left side of the Cube Editor within the appropriate folder of the tree pane (see Figure 10).

Figure 10Figure 10 The new calculated cell appears in the tree.

Setting Properties for the Calculated Cell

We now need to set the display properties for the calculated cell in a manner very similar to the way in which we set properties for the calculated members.

  1. Click once to select the Warehouse Costs to Subanalyze calculated cell.

  2. Expand the Properties pane in the lower tree pane.

  3. Click the Advanced tab.

    The Properties pane—Advanced tab—appears, as shown in Figure 11.

    Figure 11Figure 11 The Properties pane—Advanced tab.


The ForeColor and BackColor cell properties are used to store color information for the text and the background of a cell, respectively. The color and hue are defined in these properties boxes using an RGB value (the Microsoft Windows operating system red-green-blue format).

Any color in the spectrum exists at some intersection point of the basic colors (or as a "pure" color), a few common examples of which are shown in the simple illustration shown in Figure 12.

Figure 12Figure 12 Color definitions from the perspective of RGB.

Without going too far into the details of the nature of the RGB system, let's say that a normal color is represented by a number between 0 and 16,777,215. In our example, we can define our color combination by selecting the hue (a number ranging between 0 and 255, with 255 being the brightest and 0 being the darkest within the range) and plugging that hue into the respective color variables (R, G, or B) in the following formula I derive. The resulting number is the color value, which is placed into the appropriate ForeColor and BackColor cell property box on the Advanced tab.

To simplify, all we really need to know is that the numerical components for color value are represented as Red, Green, and Blue (assigned R, G, and B, respectively) in the following formula.

Color Value	 =	(R * 256 0) + (G * 256 1) + (B * 256 2)
             =	1(R) + 256(G) + 256 2 (B)
             =	R + 256G + 65536B

Eight sample colors and their corresponding RGB hue values appear in Table 1.

Table 1 Eight Sample Colors with their Respective RGB Hue Values

Color

RGB Hue Values

Red

255, 0, 0

Green

0, 255, 0

Blue

0, 0, 255

Cyan

0, 255, 255

Magenta

255, 0, 255

Yellow

255, 255, 0

White

255, 255, 255

Black

0, 0, 0


Let's say we want to customize our exception highlighting ForeColor to have a magenta appearance. We would derive the color value as follows:

Color Value	=	R + 256G + 65536B
     		=	255 + 256 (0) + 65536 (255)
	    	=	255 + 0 + 16,711,680
	    	=	16,711,935
  1. Type the number 16,711,935 into the ForeColor property box of the Advanced tab.

Now, let's derive the needed setpoint to make our BackColor property equal black. Easy enough, for we can see from the table above that all setpoints are zeroes.

Color Value	=	R + 256G + 65536B
    		=	0 + 256 (0) + 65536 (0)
	    	=	0

The setpoint, as we see, is zero.

Why, then, should we have to type anything into the BackColor property box? Although it seems that "nothing," or null, might equal "zero" in a field, the default is set for white as the color in this case, simply because this would be the most common desire in presentation.

  1. Place a zero in the BackColor property box of the Advanced tab.

We can also set other display characteristics on the Advanced tab. The FontFlags property, for example, can have the basic setpoints, or a combination of the setpoints, shown in Table 2.

Value

Description

1

Bold

2

Italic

4

Underline

8

Strikeout


We can add numbers together to achieve combinations of their effects. Examples might include the value 5, which represents the combination of bold and underline font effects, and the value 3, which represents the combination of bold and italic.

  1. Place the number 3 in the FontFlags property box.

The FontName property allows us to set the font of the displayed calculated member, whereas the FontSize property makes it possible to customize the font size of the calculated member. We will leave these at the default values.

The Format String property enables the customization of the format for displaying cell values. The set of possible values within the Format String property is substantial; a user-defined format expression for numbers can have anywhere from one to four sections separated by semicolons, and its values can be different—depending on the data types involved (numeric, string, etc.). For our example, we will be dealing with a numeric value, Warehouse Cost, so we can use up to four parameters, in the format string value that we assign, to instruct Analysis Services how to handle positive numbers, zero values, negative numbers, and null values, in that order. Although potentially confusing, the option also exists to supply fewer than the four parameters: The values given will then apply to more than one of the four standard groups, creating a composite parameter, as it were. An example is illustrated in our next step.

  1. Place the following two-parameter numeric format string in the Format String property box:

    $#,##0.00;($#,##0.00)

The above example has two sections: The first section dictates the format for positive values and zeros; we have included the dollar ($) sign to indicate that we wish to have it precede the number in the cell, the # characters to act as digit placeholders, and the thousand separator (,) and decimal placeholder (.) information in this section. The second section, in our example, defines the format for negative values. We have selected parentheses as the negative number indicator (instead of the negative sign). The other characters act as they do in the first section.

As we have discussed, the Format String property options are voluminous and far-reaching. For more information on them, as well as the other properties', setpoints, see the Books Online that were installed with the Typical MSSQL 2000 Analysis Services installation; or that can be found on the original installation CD, on the Microsoft website, and/or from other sources.

The Properties pane—Advanced tab with our additions appears, as shown in Figure 13.

Figure 13Figure 13 The Properties pane—Advanced tab with additions.

  1. After filling in the property boxes above, press the Enter key (clicking outside the Properties pane also refreshes the Data Viewing pane).

The partial results set returned in the Data Viewing pane should resemble those shown in Figure 14.

Figure 14Figure 14 Partial results in the Data Viewing pane, with calculated cell effects.


We see that the new calculated cell has accurately performed its work, highlighting those Warehouse Names where the Warehouse Cost exceeds our $11,000 threshold. We can almost certainly go further in determining information consumer requirements in our own working environments, such as to ascertain whether consumers would want to see highlighted "rollups" values (as a means of management and/or analysis by exception at a higher level), for totals whose membership contained highlighted exceptions, and so forth. In addition, there are many other setpoints that can be used for sophistication and flexibility in our displays. (Books Online serves as a great place to start exploring these powerful options.)

  1. Save the work as desired.

  • + Share This
  • 🔖 Save To Your Account