Introduction to MSSQL Server 2000 Analysis Services: Using Calculated Cells in Analysis Services, Part I

Monday Nov 17th 2003 by William Pearson
Share:

Create calculated cells in Microsoft Analysis Services. In this article, Bill Pearson discusses uses for calculated cells, and walks through a hands-on setup exercise.

About the Series ...

This is the seventeenth article of the series, Introduction to MSSQL Server 2000 Analysis Services. As I stated in the first article, Creating Our First Cube, the primary focus of this series is an introduction to the practical creation and manipulation of multidimensional OLAP cubes. The series is designed to provide hands-on application of the fundamentals of MS SQL Server 2000 Analysis Services ("MSAS"), with each installment progressively adding features and techniques designed to meet specific real - world needs. For more information on the series, as well as the hardware / software requirements to prepare for the exercises we will undertake, please see my initial article, Creating Our First Cube.

Note: Service Pack 3 updates are assumed for MSSQL Server 2000, MSSQL Server 2000 Analysis Services, and the related Books Online and Samples.

Introduction

In this article, we will explore calculated cells, which enable us to apply functionality previously reserved for calculated members, custom members, and custom rollup formulas (all of which we have explored in previous articles) to a specific range of cells--or even to a single cell. We will describe the construction of a calculated cell, touching upon the basic properties that make it up.

In this lesson, we will do the following:

  • Overview calculated cells and discuss some of the advantages and benefits that attend their use
  • Discuss the properties that must be defined to create a calculated cell
  • Discuss several optional property options that can be selected after definition of the calculated cell
  • Overview briefly the practical use of Microsoft Windows operating system red-green-blue format (RGB) values
  • Modify a calculated cell to enable exception highlighting in an example business need scenario

Calculated Cells in Analysis Services

The value within a calculated cell is computed at run time through a specified MDX expression. The expression is specified when the calculated cell is defined. The expression can be conditionally applied to a cell or range of cells, based upon an MDX logical expression. In these cases, the logical expression is also specified at the point of definition of the calculated cell.

Overview

The advent of MSSQL Server 2000 Analysis Services witnessed the arrival of calculated cells, which did not exist in earlier versions of the application. Calculated cells allow us to perform actions that could be accomplished only by calculated members, custom members and custom rollup formulas.

Calculated cells consist of three main elements:

  • Calculation subcube
  • Calculation condition
  • Calculation formula

The calculation subcube is an MDX set expression that defines the slice of the cube over which the calculated cells will be in effect. A list of single dimension sets defines the calculation subcube, and each of the sets contains one of the following:

  • All members of a dimension (including the Measures dimension): Calculated members can be included (using the .AllMembers MDX function).
  • A single specified member of a dimension (Measures, again, is included).
  • All members at a specified level within a dimension: Calculated members can be included (again, using the .AllMembers MDX function).
  • The descendants of a specified member within a dimension.
  • The descendants of a specified member at a specified level within a dimension.
  • An MDX expression that generates a set containing one of the above sets.

This list of dimension sets, combined with the default member of all other unspecified dimensions in the cube, defines the calculation subcube.

Within the subcube, the calculation condition is compared with each member cell. As we noted earlier, the calculation condition is an MDX logical expression that acts to further limit the effects of the calculated cells. If the condition evaluates as True for a given cell, the formula in the calculated cell is applied to the member cell that indicates True, and the cell returns the calculated value. The member cell returns its original value if the calculation condition evaluates as False. The combination of the calculated cells condition and the calculation subcube is termed the calculation scope.

The third component of a calculation cell, the calculation formula, is an MDX value expression that calculates the value of the cells that lie within the calculation subcube.

To summarize the interaction between the parts of a calculated cell, we define a specific target range of cells, we supply a condition that must be met before applying a formula and we apply a formula within that specific range of cells for any cells meeting the condition. We will see the three main elements of the calculated cell in action in the steps of the practice exercise that follows.

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 are 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 can be used to provide a higher level of flexibility for client applications (among other uses).

We will examine the steps involved in creating calculated cells under both global and session scopes, in this and the subsequent article, respectively. We will focus on the calculated cell in an MDX query in our next lesson, Using Calculated Cells in Analysis Services, Part I.

Creating a calculated cell within Analysis Services, the primary focus of this lesson, 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 afford us an opportunity to focus on the attributes and characteristics of the calculated cell itself, before getting involved in the MDX syntax in our next lesson.

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.

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), exposing the FoodMart2000 database, as shown in Illustration 1.


Illustration 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 by clicking the + sign to its left.

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


Illustration 2: Sample Cubes Provided with the Analysis Services Installation

6.  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 warehouses, so let's set up the Data viewing pane to conform to our needs.

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

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

9.  Ensure that 1997 is displayed in the filter box in the Time dimension in the top half of the pane, indicating that we are filtering on 1997.

10.  Double-click USA, in the Country column, to expand the hierarchy and show the underlying states.

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

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

13.  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 Illustration 3.


Illustration 3: Cube Editor Preview Pane (Partial View), After Adjustments

14.  Select Insert from the top menu.

15.  Click Calculated Cells... on the Insert menu, as shown in Illustration 4.


Illustration 4: Select Calculated Cells... to Initialize the Calculated Cells Wizard

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


Illustration 5: The Calculated Cells Wizard Welcome Dialog Box

16.  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.



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




18.  From the Members Set drop-down selector on the right side of the dialog box, select A Single Level.



19.  Expand the Warehouse dimension tree, and select Warehouse Name.




The dialog box appears as shown in Illustration 6.





Click for larger image

Illustration 6: Select A Single Level

20.  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.

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

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

CalculationPassValue((Warehouse.CurrentMember, 
	[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 Illustration 7.


Illustration 7: The Completed Define the Calculation Condition Dialog Box

23.  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 that it 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).

24.  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 Illustration 8.


Illustration 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).




25.  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 Illustration 9.




Click for larger image

Illustration 9: The Completed Finish the Calculated Cells Wizard Dialog Box

26.  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, as depicted in Illustration 10).


Illustration 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. We do this in a manner very similar to that in which we set properties for calculated members.

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

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

29.  Click the Advanced tab.

The Properties pane--Advanced tab appears, as shown in Illustration 11.


Illustration 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 intersect values are depicted in Illustration 12.


Illustration 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 select sample colors and their corresponding RGB hue values are summarized in Table 1.




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


Table 1: Select Sample Colors with Corresponding RGB Hues

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 16711935 into the ForeColor property box of the Advanced tab.

Now, let's derive the needed setting to make our BackColor property equal Black. This is easy enough, for we can see from the table above that all settings are zeroes.

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

The setting, 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.

2.  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 settings, or a combination of the settings, shown in Table 2.

Value

Description

1

Bold

2

Italic

4

Underline

8

Strikeout


Table 2: Basic FontFlags Property Values

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.

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

The FontName property allows us to set the font of the displayed calculated cell, whereas the FontSize property makes it possible to customize the font size of the calculated cell. 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.

4.  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 are set to behave 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' settings, see the Books Online (installed with MSSQL 2000 Analysis Services, or available on the web and elsewhere).

The Properties pane - Advanced tab with our additions appears, as shown in Illustration 13.


Illustration 13: The Properties Pane - Advanced Tab with Additions

5.  After filling in the Properties 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 Illustration 14.


Illustration 14: Partial Results in the Data Viewing Pane, with Calculated Cell Effects

6.  Save the modified cube, as desired.

We see that the new calculated cell has accurately performed its work, highlighting those Warehouse Costs exceeding 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 "rollup" 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 settings that can be used for sophistication and flexibility in our displays. (Books Online serves as a great place to start exploring these powerful options.)

Summary ...

In this lesson, we explored calculated cells, which comprise functionality that is new with MSSQL Server 2000 Analysis Services, and which was previously reserved for calculated members, custom members and custom rollup formulas. Calculated cells allow us to apply formulas and property settings to a specific range of cells or even to a single cell, and optionally to base these assignments upon conditional criteria that we can stipulate in the creation of the calculated cell. We explored the construction of a calculated cell, touching upon some of the basic properties that make it up, as a part of our creating a calculated cell.

We manipulated calculated cells within our practice example, and exposed the procedures behind applying an MDX value expression selectively, and providing support for exception highlighting. We practiced the application of the expression to specific cells, based upon an MDX conditional expression.

We discussed the creation of calculated cells with a global scope, and practiced this "permanent" calculated cell creation process within the Calculated Cells Wizard. In our next session together, we will focus on calculated cells with a session scope, when we take up their creation using an MDX query. Within the lesson, we will practice the use of the WITH statement in creating calculated cells, noting that a calculated cell created in this way offers greater flexibility for client applications, in many cases.

» See All Articles by Columnist William E. Pearson, III

Discuss this article in the MSSQL Server 2000 Analysis Services and MDX Topics Forum.

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved