Вы находитесь на странице: 1из 60

Calculated Members

Calculated
Members
Objectives
Understand MDX and MDX expressions.
Discover how to name members of a cube.
Explore the Calculated Member Builder.
Learn how to build MDX expressions.
Review some of the basic MDX functions.
Find out how to navigate between members.
Learn how to create conditional MDX expressions.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-1
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

MDX
MDX is the language of retrieving information from Analysis Services cubes.
The term MDX is derived from the phrase multidimensional expression. The
term is used the same way SQL is used instead of Structured Query Language.

The acronym MDX implies the ability to extract information from the cube
and perform multidimensional calculations in the process. MDX is a part of the
OLE DB for OLAP specification. Microsoft helped to create this specification
and MDX is now a standard administered by an industry review board.

Because MDX is a standard, you may find the MDX language used with other
products. The OLE DB for OLAP specification is open, allowing other
vendors to create their own multidimensional products. This helps to make
MDX portable across different products.

The MDX used in Analysis Services has been modified since it was first
created for Microsoft SQL Server 7.0 OLAP Services. Because of these
changes, there are features of MDX in Analysis Services that are not a part of
the industry standard. The standard also contains elements that are not a part of
the current version of MDX in Analysis Services.

Functions of MDX
The literal meaning of MDX (multidimensional expression) does not
adequately express its usage. There are two ways to use MDX: in MDX
statements and in MDX expressions.

MDX Statements: A query language for accessing the cube from


client applications. It is similar to SQL statements, and it returns a
complete set of data to the application.
MDX Expressions: Used as formulas, similar to the ones used in a
spreadsheet. MDX expressions return a single value, and are used to
create calculated members, default members, and many dimension and
cube properties.
One thing that statements and expressions share is the nomenclature for
identifying members and the functions within the MDX language. The only
difference between them is where and how they are used. Therefore, they are
referred to as different functionalities of MDX.

NOTE MDX expression might sound redundant, but so does SQL


language, or GUI interface. Often when an abbreviation is used
more commonly than the full term, it ceases to be treated as an
abbreviation, and that has happened with MDX.

7-2 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX

When you see an MDX statement, it appears almost like an SQL statement.
The MDX statement uses MDX expressions for referencing the cube. The SQL
statement uses SQL expressions for referencing the relational database.

Most of the time MDX statements are used in client applications that build
queries. The application handles these queries such that users do not have to
know the MDX language, just as applications hide SQL syntax. The only time
you will use MDX statements is when you build a client application using a
programming language, or when using a third-party application that allows you
to create and see the MDX statements.

NOTE The MDX Sample Application ships with Microsoft SQL Server
2000 Analysis Services. It allows you to create and write MDX
statements and execute them against cubes. The application is
written in Visual Basic 6.0 and ships with all of the source code,
so you can look at the sample code to learn how to write queries
for your own code.

When you create cubes and manage them, MDX expressions become an
everyday part of the management process. If you are a cube administrator,
understanding and becoming familiar with MDX expressions is an important
part of the job.

Member Names
The first step in learning MDX is to know how to refer to the members of a
cube. Members include the metadata of the cube, the dimensions, hierarchies,
levels and members, as well as the cube measures. Measures are stored in a
special dimension called the Measures dimension.

MDX expressions treat all members the same, regardless of the type of
dimension. This means that even though the Measures dimension has some
features that differentiate it from other dimensions, MDX treats it exactly the
same. In other words, the querying process of a cube returns a value that is
identified by one member from each dimension of the cube.

You create member names by putting together a string of names that are
separated by periods. The name parts can be dimension names, hierarchy
names, level names, and member names. The sequence is read from left to
right, and as long as the names in the cube are clear, there should be no
problem reading them.

Bracketing
Square brackets ([]) are often used around each part of a member name. They
are not always required, but it is a good idea to get into the habit to avoid

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-3
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

potential problems. The parts of a member name may refer to the dimension
name, the hierarchy name, the cube name, the level name, and the actual
member names.

Brackets are required in the following three cases:

Spaces: A name containing spaces or any special character that might


otherwise interfere with the MDX expression needs brackets. For
example, if you have a member name Store Sales, then the part of the
MDX expression that refers to it is [Store Sales].
Keyword: If the name is an MDX keyword, such as the name of a
function, then it needs the brackets. For example, if there is a member
named Parent, then it must be used as [Parent].
Number: If the name starts with a number, then it must be in brackets.
For example, Year level members are the actual years, so they would
look like [2002].
There is no rule that you cannot use brackets in certain situations, so you can
use brackets for all parts of the name without causing problems.

NOTE It is sometimes regarded as a best practice to use brackets for all


parts of the name just to be on the safe side, or just for the sake of
naming consistency. One thing that helps is that the tools within
Analysis Manager automatically use square brackets in every
situation.

Qualifying
The same member name can appear in multiple locations within the cube.
Sometimes the same name even appears twice or more within the same
dimension or level. If that sounds odd, remember that January has the same
name no matter what year it is in. Qualifying is how members are uniquely
identified.

Qualifying helps to avoid ambiguity when the expression is being read.


Although there are places in a cube where a full qualification of a member is
not necessary, it is recommended so that the expression makes the members
location clear.

For example, in a Time dimension, there are many instances of the same name
quarter. To indicate the exact quarter member you refer to, qualify it with the
parent year member, as in [2002].[Q1].

Another example involves the same name used in different dimensions. In this
case, you might have a city name such as Norman, and an employees name
that is also Norman. In this case, the dimension name could suffice to
distinguish between the two members, as in [Store].[Norman] and
[Employee].[Norman].

7-4 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX

If the name is ambiguous, the MDX expression will not cause an error.
Instead, Analysis Server will return the first instance of the name that it finds
in the cube. In the time example above, if the years started with 2000, then
specifying [Q1] without a qualifier would return the quarter member for the
year 2000 every time. As for the name Norman, Analysis Server will return the
member from the first dimension it encounters in the cube.

Creating
The member name is read from left to right, but you create it from right to left.
The first thing to specify is the member. You can specify members by member
name or the member key. If you indicate the member by name, square brackets
around the name are sufficient, or just the name itself. When you specify a
member by its key, precede the key name with an ampersand (&). If you place
brackets around the key value, place the ampersand to the left of the left
bracket.

Sometimes the member name or member key is all that is necessary to identify
the member. To remove ambiguity, you can precede the member name or key
with other information. You choose the amount of information when you
create the MDX expression.

You can use any or all of these preceding qualifiers:

Parent: You can precede a member name or key with the parent
member identifier. This is necessary when there are two members in
the same level with the same name, such as January. The parent
member identifier follows the same rules as the member identifier.
That is, the parent member can be specified with the name or key, and
it can be preceded by any or all of these options. An example of using
a parent member qualifier is [2002].[Q4].
Level: If the member identifier is unique within its level, you can
place the level name in front of the member identifier without any
other member qualification. An example of this is [Store
City].[Mexico City]. The only valid preceding name for a level name
is the dimension name.
Dimension: If a member identifier is unique within the entire
dimension, then only the dimension name is necessary as a qualifier,
as in [Store].[Arizona].
You can use combinations of all three methods for creating preceding names.
And you can refer to the same member in different ways. For example, the
name [Time].[Year].[2001].[Q3] identifies the third-quarter member whose
parent member is 2001 from the Year level of the Time dimension. Once you
qualify the name by using parent members from the left side, there is no way
to refer to the level name of the member being named. In this example, there is
no need to specify [Quarter] after the parent year member. In fact, it would
create an error in the MDX expression.

Here is an example of multiple names for the same member:

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-5
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

[Store].[All Store].[USA].[CA].[San Diego]


[Store].[Store Country].[USA].[CA].[San Diego]
[Store].[Store State].[CA].[San Diego]
[Store].[Store City].[San Diego]
[Store].[San Diego]

All of these examples use the dimension name. The first one uses no level
names; it just starts with the topmost member name, which is unique. The
second through fourth examples all start at different levels. They are only valid
if the first member name is unique within that level. Most likely, these three
examples would work without the dimension name. The last example depends
on the member name being unique within the entire dimension.

Figure 1 shows a tree diagram of a sample time dimension.

Evaluating

Figure 1. An example hierarchy of the time dimension.

The diagram in Figure 1 illustrates a portion of the dimension and highlights


the member for February, 2002. The following names all clearly indicate the
shaded member:

[Time].[All Time].[2002].[Q1].[Feb] This is a completely qualified


name.
[2002].[Q1].[Feb] This name omits the dimension name and the top
member name. It works because year member names are usually the
only member names that look like that.

7-6 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX

[(All)].[All Time].[2002].[Q1].[Feb] This works without the


dimension name. Although several dimensions might have the (All)
level, the rest of the name makes it clear that this is the Time
dimension.
[Year].[2002].[Q1].[Feb] This one omits the dimension name, but
specifies the Year level name, and the cube probably contains only one
level of that name.
Here are some examples of names that are in error or could identify a different
member than the one you intend to point to:

[Time].[Feb] This name is ambiguous. Given the sample structure in


Figure 1, this expression points to the member for February, 2001,
which is the first instance of the Feb member name.
[2002].[Month].[Feb] This is an attempt to omit the quarter parent
member, but it is illegal because a member name cannot precede a
level name.
[(All)].[2002].[Q1].[Feb] This is illegal because once you specify a
level name, you have to use a member from that level and 2002 is not
on the (All) level.
[Year].[2002].[Q2].[Feb] This is just plain wrong. The parent
member of Feb is not Q2.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-7
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

The Calculated Member Builder


MDX expressions are used to create calculated members inside a cube.
Calculated members are different from stored members of dimensions. The
values that stored members identify are added to the cube at processing time,
and any aggregations are created and stored in the cube at that time as well.
Calculated members are not stored in the cube. The only part of a stored
member that is in the cube is the MDX expression used to build the calculated
member.

Calculated members have the following characteristics:

MDX: Calculated members are created using MDX expressions, in


contrast to stored members or derived measures, which are created
using SQL expressions. This means that calculated members are
created from within the cube, not from the source relational database
system.
Query Time: Calculated members are not stored in the cube. Instead,
the calculation occurs at query time, after any aggregations are
created.
Other Members: Calculated members use the stored members of the
cube, or other calculated members. This is done by creating an MDX
expression referring to members by name or function, and then using
common arithmetic operators or numeric constants to perform the
calculation. You can also use a rich set of functions to perform certain
calculations.
Dimensions: A calculated member can belong to any dimension in the
cube. Most of the time, calculated members are calculated measures.
When that is the case, the calculated member is a member of the
Measures dimension. Nevertheless, calculated members can be placed
in any dimension at any location. Once a calculated member is in a
dimension, it can be used to identify values in the cube. This is done
by selecting the calculated member from that dimension to identify the
value. This causes the MDX expression to be evaluated.
Processing: Calculated members do not affect the cube size and
processing time. If you use the Cube Editor, there is no need to
reprocess the cube after adding a calculated member; you can view it
immediately.
Query Retrieval: Calculated members are created at query time, thus
slowing the queries that use them. The calculation engine of Analysis
Server is designed to work very fast and the results of the calculation
can be cached in the same way as stored members and aggregations.
They have no impact on queries that do not use them.

7-8 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

Usually you add calculated members by using the Calculated Member Builder,
a tool within the Analysis Manager that you access by creating a new
calculated member. To demonstrate this tool, restore a database that was
originally created in another chapter of this course. A copy of it is in the folder
for this chapter.

Try It Out!

See CalcMembers In the following steps you will restore an archived OLAP database and open
Start.cab the Calculated Member Builder to examine its features.

1. In Analysis Manager, right-click the server name and select Restore


Database from the context menu. In the Open Archive File dialog box,
locate CalcMembersStart.cab in the folder for this chapter, select it, and
click the Open button. In the Restore Database dialog box, click the
Restore button and once the restore is complete, click the Close button.

2. Expand the CalcMembers database folder and the Data Sources folder
within it. Right-click the localhost data source and select Edit from the
context menu. In the Data Link Properties dialog box, make sure that the
connection properties indicate localhost as the server name, Windows NT
Integrated Security, and NWindMart as the database.

3. Click the Test Connection button, click the OK button in the Microsoft
Data Link message box, and click the OK button to close the Data Link
Properties dialog box.

4. Expand the Cubes folder in the CalcMembers OLAP database, right-click


the Sales cube, and select Edit from the context menu.

5. Right-click the Calculated Members folder in the tree pane and select New
Calculated Member from the context menu. This opens the Calculated
Member Builder.

In the next few sections, you will learn how to use the Calculated Member
Builder by exploring its interface. The controls will be explained, with special
emphasis on the Data tree view and the Value expression entry box.

Regions
The Calculated Member Builder (shown in Figure 2) contains five regions.
These areas are differentiated so that you will know where and when to use
any particular section. They are arranged vertically, top to bottom.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-9
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Figure 2. The Calculated Member Builder.

The top area has four controls and is where you name and locate the member:

Parent Dimension: A drop-down list of all the dimension names in


the cube. The default dimension is the Measures dimension. There are
two reasons for this: it is the only dimension name that is certain to be
in every cube, and most calculated members are calculated measures.
Parent Member: If the parent dimension is the Measures dimension,
this text box and button will be disabled because the Measures
dimension has no hierarchy, so all members are at the same level with
no parent member. Otherwise, you can do one of the following
actions:
Type: Type a member identifier in the text box. This is an
MDX expression identifying the parent member.
Change: Click the Change button. This opens the Select the
parent member dialog box, as shown in Figure 3. Select the
parent member from the dimension data and click OK. This
puts an MDX expression in the text box.

7-10 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

Figure 3. The Select the parent member dialog box, opened with the Change
button.

Blank: Leave the text box blank. This has the effect of placing
the calculated member in the topmost level of the dimension.
Regardless of your choice, calculated member values are not
summarized in the parent member values. The calculations occur
after the aggregations, and values are summarized into parent
member values during the aggregations. The placement of the
calculated member is a logical positioning. Users expect to see the
values for this new member next to like members of that
dimension.
Member Name: Where you name the new calculated member. Enter
any name that you want that identifies the member meaningfully. The
default entry is New Calculated Member, so be sure to change that.
The second region is where you enter and check the MDX expression.

Value Expression: Where you build the MDX expression. You can
type expressions into this box, edit them, and also use drag and drop
and double-clicks to add objects to the value expression.
Check: Use the Check button to validate the syntax of the value
expression.
The third, or middle, region is where the metadata for the cube is displayed.
You can use this region to build the value expression by using double-clicks,
point-and-click, and drag-and-drop operations. This makes it easy to create
MDX expressions and typing is often unnecessary.

Data: The data list that has all of the metadata of the cube. Every
dimension, every level, every member, every member property can be
selected, expanded and used from this tree view list. You can locate
members by expanding the dimension name, then expanding the level

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-11
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

name where the member resides. Or you can find a parent member and
drill down into its descendants.
Functions: A complete list of all of the MDX functions, organized by
function type. Use the function type to identify the type of value you
want, and then select the appropriate function.
Insert: Use this button to transfer a selected name from the data list,
or a selected function from the function list, into the value expression
box.
Buttons: Use the buttons below the Insert button to type digits, and
certain special characters, into the value expression box.
Register: A special button that allows you to register third-party
function libraries. Besides the MDX functions, calculated members
can use functions from certain external libraries such as Excel and
Visual Basic functions.
The fourth area is a status box that shows examples of items you select in
either the Data or Function list. It is useful to see the type of object you
selected, as well as the syntax for the object. What you see in this area will
become part of the value expression if you choose to insert the selected object.

At the bottom of the dialog box are the usual buttons: OK, Cancel, and Help.

Value Expressions
When you type matching punctuation, the Value expression box uses color
coding and bolding to make sure that you see the bracketed expression, and
whether a closing bracket or parenthesis is missing.

Try It Out!
In the following steps you will learn how to use the tools of the Calculated
Member Builder by experimenting with them.

1. In the Calculated Member Builder, type (12345) in the Value expression


box. Notice that the opening parenthesis was red until you typed the
closing one. Note also that when the insert cursor is to the right of either of
the parentheses, they are bold. If both parentheses appear, they are both
bolded.

2. Click in front of the opening parenthesis. Then click the opening brace ({)
button just above the Register button. The left brace appears in red in the
Value expression box. Red is used to indicate that there is an unmatched
bracket.

7-12 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

3. Place the cursor at the end of the expression and click the closing brace
button (}). Now both braces are black.

4. Clear the Value expression box.

5. Type Food in the value expression box and press the SPACEBAR. The text
appears black. Member names, constants, and non-keywords appear in
black.

6. Type Avg in the value expression box and press the SPACEBAR. Since
this word is an MDX function it appears in maroon, or dark red.

7. Type And in the value expression box and press the SPACEBAR. Since
this word is an MDX keyword it appears in blue.

8. Type Hello in the value expression box (yes, the quotation marks as
well) and press the SPACEBAR. The text you typed appears in red. The
quotation marks will also be red, and they will be bold when the insert
cursor is to the right of either of them. This will help you locate a missing
quotation mark because too much or too little of the expression will be
bright red.

9. Clear the Value expression box.

Data List
10. In the Data tree box, click the plus sign next to Measures to expand that
folder. The MeasuresLevel appears. The Measures dimension is special
because it has no hierarchy of levels. All the measures appear as members
of the single level in the dimension. Nevertheless, Measures is a
dimension, and there has to be a level that is distinct from the dimension.
Analysis Server names it MeasuresLevel, as it appears here.

11. Select MeasuresLevel. Notice that the status box shows two pieces of
information: the type and name of the selected object, and the syntax that
Analysis Server uses if you drag and drop this object in the Value
expression box. It shows that you have selected a Level named
MeasuresLevel and the fully qualified name of the object you selected is:

[Measures].[MeasuresLevel]

12. Expand MeasuresLevel, to examine the measures that are in this cube.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-13
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Icons
Each name in the Data tree view has an adjacent icon indicating the type of
object. In Figure 4, you can see that measures use an icon that indicates they
are numeric, and possibly are currency data types.

Figure 4. Measures icons.

A three-pointed arrow, as shown in Figure 5, indicates the dimensions.

Figure 5. Dimension icons.

Expand the Product dimension to see the level icons, as shown in Figure 6. As
you can see, the (All) level uses an empty blue rectangle, while all other levels
use a small blue square, one for each level of depth.

Figure 6. Level icons.

Expand the Department level to see the members of that level, as shown in
Figure 7. The member icons are supposed to be pushpins, but they look like
lollipops!

7-14 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

Figure 7. Member icons.

The last type of icon is for hierarchies, but the sample cube does not have
them. If you look at Figure 8, you will see that the icon for a hierarchy is like a
staircase, representing the levels, and therefore the hierarchy, of a dimension.

Figure 8. Hierarchy icons.

Members
The Data tree view has all of the dimensions, hierarchies, levels, and members
of the cube. To locate a specific member, drill down into the tree until you find
the member you are looking for. The search is made easier because the same
member is stored in multiple locations.

Querying the cube metadata whenever you drill into the tree dynamically
creates the presentation in the Data list. This makes it appear that the members
are in multiple locations. In reality, there are multiple ways of locating
members.

Expanding dimensions shows hierarchy or level names. Expanding hierarchies


exposes level names. Expanding level names exposes all of the members of
that level. Expanding a member shows all the child members of that member.

The multiple paths to a member name might be confusing at first, but after a
short while you will understand better how to use the Data tree view.

13. Expand the Store dimension, then expand the Store State level. Notice
that none of the state member names have plus signs next to them. That
indicates that you are looking at the lowest level of the dimension in the
cube. Note the Yucatan member in particular.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-15
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

WARNING! Store State is not actually the lowest level of the Store dimensions,
because the Sales cube in this example disabled the two lower levels.
That means that although you see them in the Cube Editor, they have
no data in the cube, so the Calculated Member Builder cannot detect
them. Disabled levels are covered in another chapter of this course.

14. Expand the Store Country level and note the Mexico member. It has a plus
sign next to it. This indicates that it is not at the lowest level and it does
have child members. Click the plus sign next to Mexico and scroll the
Data view down until you see Yucatan again, this time as a child member
of Mexico. You can even scroll down the window so that both instances of
Yucatan are visible, as shown in Figure 9.

Figure 9. The Data tree view showing the Yucatan member at both the top and
bottom of the box.

TIP: Some built-in properties appear here as well as the child members. They
appear as all caps, with a property icon next to each one. They are advanced
properties and are not covered in this course, but you can select them to see
the syntax that would be used in an MDX expression.

Notice that the Yucatan member under Mexico has a plus sign. If you expand
Yucatan at this point, you will see the same properties that are listed under
Mexico. If you ever need those properties from the lowest level of a
dimension, remember to use the drilldown technique to find them, rather then
going directly to the lowest level.

15. Now expand the (All) level, expand the All Store member, and expand the
Mexico member. Note that Yucatan appears again when you start at the
highest level of the dimension and drill down from parent to child to child

7-16 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

until you reach the lowest level. The values are also indented so that you
can actually count how low you went by looking at the light dotted lines
on the left.

Insert into Expression


16. Expand the Store dimension, expand the Store State level, and double-
click the Veracruz member. This automatically inserts the member name
into the Value expression box. Note that the name for this member is:

[Store].[Veracruz]

17. The name is simply the dimension name and the member name. No level
name or parent member name qualifies the member. The Member Names
Unique property is set to True for the entire Store dimension. This
guarantees that each member name in the dimension is unique regardless
of its level and parent, so the only qualification necessary is the dimension
name.

18. Clear the Value expression box.

19. Expand the Time dimension, expand the Year level, expand the 2002
member, expand the Q3 member, and double-click the September
member. This produces a longer and more confusing name:

[Time].[Year].&[2002].&[Q3].&[9]

20. Member names are not unique in the Time dimension. September appears
more than once as a member name, and the quarter names also appear
multiple times. This means that the name must be qualified through the
parent member-child member relationship. It starts with the dimension
name and the level name, and then it proceeds through the key values of
the membersfirst the year, then its quarter child member, and finally its
month child member.

21. Clear the Value expression box.

22. Expand the Product dimension, expand the Product Name level, and
double-click the Good Light Beer member. As you can see, this name is
much shorter even though there are five levels in the Product dimension:

[Product].[Product Name].&[273]

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-17
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

23. This name is shorter because the Member Keys Unique property is set to
True for the Product Name level. This means that the ancestors of this
member do not need to be specified, because the keys at the lowest level
are unique across that entire level. This name contains only the dimension
and level names, and then the member key. If you tried to use this with
member names that are not unique, you would get the first member of the
level with that name.

24. Click the Cancel button.

The form of the member name that you add to the Value expression box (by
double-clicking, dragging and dropping, or via the Insert button) is determined
by properties of the dimension and its levels. These forms follow this set of
rules, by checking them from top to bottom. In other words, the first condition
of the properties in this list determines how the member name is qualified.

If the dimension property Member Names Unique is set to True, the


member name is used with no other qualification than the dimension
name.
If the level property Member Names Unique is set to True, the
member name is used with the dimension name and level name as the
only qualifiers.
If the dimension property Member Keys Unique is set to True, the
member key is used and qualified only by the dimension name. The
key is indicated by the ampersand.
If the level property Member Keys Unique is set to True, the
member key is used and is preceded by the dimension and level
names. The key is indicated by the ampersand.
If none of these properties is set to True, the key is used and is
preceded by the parent member. The parent member identifier is
determined by these rules from top to bottom. These rules will
continue to be applied to member names until either a level name or a
dimension name is used in the name.

TIP: These rules apply to the automatic naming that the Calculated Member
Builder does. If you enter members manually, you can type in as much or as
little of the member identifier as you wish. Keep in mind that ambiguous
member identifiers will pick up the first match.

Correct Avg
Now use the Calculated Member Builder to create a calculated measure,
Correct Avg. Examine the Cube Editor for this Sales cube and you will see a
derived measure, called Incorrect Avg. This was created by using a SQL
expression to divide the Store Sales by Unit Sales in the source database before
importing the values into the cube. However, the calculations were all done at

7-18 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
The Calculated Member Builder

the lowest level of the values and then added together for the aggregations.
Note that the Aggregate Function property for this measure is Sum. This
created an incorrect value in the cube.

This example will show how a calculated member produces the correct value.

Try It Out!
In the following steps, you will open the Sales cube of the CalcMembers
OLAP database, which was restored in a previous Try It Out section in this
chapter. You will create a calculated member to show an average that will
work at any level of the cubes dimensions.

1. In the Cube Editor for the Sales cube of the CalcMembers OLAP database,
click the Insert Calculated Member button on the toolbar.

2. Enter Correct Avg in the Member name text box.

3. In the Data tree view, expand the Measures dimension and then expand
the MeasuresLevel level.

4. Double-click the Store Sales measure. This causes the member identifier
to appear in the Value expression box.

5. Click the division (/) button, just below the Insert button on the far right.

6. Drag and drop the Unit Sales measure from the Data tree view to the
Value expression box, right after the division symbol. The expression
should now read:

[Measures].[Store Sales]/[Measures].[Unit Sales]

7. Click the Check button to make sure that the syntax is correct, click the
OK button in the message box, and then click OK to save the Correct Avg
measure and close the builder.

8. Click the Data tab at the bottom of the schema pane, and scroll over until
you see the Correct Avg column.

TIP: Note that no processing is necessary after adding calculated members.


Remember that they are calculated during the query, so they use no
processing time or storage.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-19
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

9. The format is not correct, so select the Correct Avg member in the tree
view, select the Format String property on the Advanced tab of the
Properties pane, and select Currency from the drop-down list of values.
Then press the ENTER key.

10. Go back to the data pane, scroll over, and you should see that the value for
All Product, All Store, All Time, and Correct Avg is $2.13. You can
verify that this is the correct value by examining the two columns on the
left side.

7-20 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

MDX Expressions
One of the most important uses of MDX expressions is to create calculated
members. Calculated members give the OLAP cube the same capability as a
spreadsheet with formulas.

You create the calculated member formula by entering an MDX expression


into the Value expression box of the Calculated Member Builder. Once you
create the calculated member, you can edit it in the dialog box, or by
modifying the Value property in the Cube Editor. You can change the name of
the calculated member by changing the Name property, or by using two clicks
on the name in the Cube Editor tree pane.

An MDX expression can return only these types of values:

Numeric
String: The string must be enclosed in double quotation marks.
Empty: The constant Null creates an empty cell. An empty cell
behaves like zero when used arithmetically, but is treated like Null
values when calculating averagesin other words, it is ignored. To
create an empty cell, use the keyword Null.

Expression Operators
MDX expressions use arithmetic operators that combine numeric values. The
standard operators are used:

Plus sign (+) for addition


Minus sign (-) for subtraction
Asterisk (*) for multiplication
Slash (/) for division
You can do exponentiation by using the caret (^).

You can combine strings by using the plus sign (+) for concatenation. This is
different from Excel, where you use the ampersand (&) to combine strings. In
Visual Basic, either operator can be used for string concatenation.

MDX does not support combining string and numeric data types; there is no
automatic conversion between data types.

You can use constants in MDX expressions. Numeric constants are just the
numeric digits. String constants are enclosed in double quotation marks.
Spaces can be used around operators in MDX expressions, but no extra spaces

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-21
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

are allowed within member names. Spaces can make the MDX expression
easier to read.

Try It Out!
In the following steps, you will use the Store Sales virtual cube to keep the
data presentation simple. You can add calculated members to a virtual cube in
the same way as you add them to source cubes. Furthermore, a virtual cube can
import any calculated members from the source cubes.

1. In the CalcMembers OLAP database, right-click the Store Sales cube in


the Cubes folder and select Edit from the context menu.

2. Click the Insert Calculated Member button on the toolbar.

3. Enter Constant in the Member name text box.

4. Enter 1000 in the Value expression box, and click the OK button.
In the data pane of the Virtual Cube Editor, youll see a column called
Constant, and every cell in that column has the value 1000. This is one
way to see that calculated members are not used for aggregations. There
are three cells in the Store Country level that have 1000. The All Store cell
also has 1000. If there had been an aggregation of the values, you would
expect to see 3000 in the All Store cell.

5. Double-click the Store Country button, then the Store State and Store
City buttons.
Every cell in the virtual cube shows 1000. Even though certain cells are
labeled as totals for that particular parent cell, all cells show 1000 in the
Constant column.
Compare this to a spreadsheet in which each cell has the same formula,
which is =1000. That same calculation is performed for the Constant
column when it is queried, no matter which members are chosen from the
Store dimension.

6. Select the Constant member in the tree view pane of the Virtual Cube
Editor, and select the Value property on the Basic tab of the Properties
pane. Enter Hi there as the property value. Dont forget to include the
quotation marks. Press the ENTER key.

The Constant column changes immediately to show the string value you
entered.

7. Select the Constant member in the tree view pane of the Virtual Cube
Editor, and select the Value property on the Basic tab of the Properties
pane. Enter Null as the property value and press the ENTER key.

This causes all of the Constant column cells to appear empty.

7-22 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

8. Select the Constant member in the tree view pane of the Virtual Cube
Editor, and select the Name property on the Basic tab of the Properties
pane. Enter Something as the property value and press the ENTER key.

This changes the Constant column to the Something column.

9. Select the Something member in the tree view pane of the Virtual Cube
Editor, and select the Value property on the Basic tab of the Properties
pane. Enter 32 + 75 as the property value and press the ENTER key.

Now every cell in the Something column displays 107, which is the value
obtained from the expression 32 + 75.

10. Select the Something member in the tree view pane of the Virtual Cube
Editor, and select the Value property on the Basic tab of the Properties
pane. Enter Now for + Something else as the property value and
press the ENTER key.

The string Now for Something else appears in every cell of the Something
column, showing that the MDX expression did a string concatenation.

11. Right-click the Something calculated member, select Delete from the
context menu, and click the Yes button in the message box. This shows
how calculated members can be removed from a cube or virtual cube.

12. Click the Save button on the toolbar and close the editor.

Member Information
Member information is one of the most common functions used in MDX
expressions. Many calculated members are created using information from
other members that are already in the cube. The expressions take advantage of
the relationships between members of a dimension.

One example is a calculation for a member that represents the percentage of


the total for its parent member. A calculation like this has to identify current
member that is being used in the query results, as well as identifying the parent
member.

Member information and the relationships between members of the same


dimension is handled through the MDX functions in the Functions list within
the Calculated Member Builder.

MDX Functions
Functions in MDX are the basis for almost all MDX expressions. Virtually all
MDX expressions involve one or more functions. The Functions list in the
Calculated Member Builder has all of the MDX functions grouped by type.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-23
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

The way to use the Functions list is to identify the type that you want to use at
that point in the MDX expression. Then open the list of functions of that type
and choose the one that you need. If you need a string value, use the String
functions. If you need a number, use the Numeric functions. If you need a
member, use the Member functions. There are many more function types
available.

These are the features of the Functions list in the Calculated Member Builder:

Syntax: When you select a function name, the syntax for the function
appears in the status window of the builder. This makes it easier to tell
if the function name you are considering is the right one to use.
Insert: When you double-click the function name, it is automatically
placed in the Value expression box, where the cursor is at that
moment. You can also use the Insert button to move the currently
selected function.
Tokens: If the function requires another type of object, it will have
chevron placeholders around the name of the type needed. These are
placeholders. You select the token in the Value expression box, and
select another function, or the appropriate object from the Data tree
view, and whatever you insert will take the place of the type name and
the chevrons. One click selects the token.

NOTE Optional parameters for the function are enclosed in square


brackets. It is up to you to delete the brackets, and if you dont
need the option, delete any placeholders, tokens, or punctuation
within the brackets.

Try It Out!
In the following steps, you will use the Calculated Member Builder to create
MDX expressions for a cube that will return the name of the current member
of one particular dimension.

1. In the CalcMembers OLAP database, right-click the Store Sales cube in


the Cubes folder and select Edit from the context menu.

2. Click the Insert Calculated Member button on the toolbar.

3. Enter Name in the Member name text box.


As you can see from the member name, the plan is to put a string in this
member that will represent the name of something. Since the object you
need is a string, you will look for a String function type.

4. In the Functions list, expand the String folder. The Functions list with
some of the String functions is shown in Figure 10.

7-24 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

Figure 10. The Functions list showing String functions.

From here youll see four different Name functions, one for each type of
object in a dimension. There is really only one Name function, but the
Member Builder separates them so that whichever one you choose will
have the token for the object type specified in the function list.

5. Select the Name Member function. Notice the syntax for this function
in the status area at the bottom of the builder. Double-click Name
Member.

6. Now that same syntax appears in the Value expression box. The
Member token is bright red, while the function name, Name, is dark red.
Click in the Member token. This highlights the entire token. Since the
token name is Member, you need to select a member of a dimension from
the Data box, or a Member function from the Functions box.

7. In the Data box, expand the Store dimension and the Store Country level.
Double-click the Mexico member. This creates the following expression:

[Store].[Mexico].Name

8. Click the OK button to save the member and close the builder.
As you can see, every cell in the Name column shows Mexico, the
member name of the Mexico member. It is kind of redundant, but the point
is that Name is an MDX function that returns the name of an object from
the OLAP cube.

9. Now you will edit the Name calculated member to show something a bit
more meaningful. Right-click the Name member in the tree pane and
select Edit from the context menu.

10. Clear the Value expression box.

11. In the Functions list, expand the String folder and double-click the Name
Member function.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-25
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

12. Click in the Member token in the Value expression box.


Whenever you see the Member token, you can use a member directly
from the Data tree view, or you can use a function that returns a member.
These functions are in the Member folder of the Functions list.

13. In the Functions list, expand the Member folder and double-click the
CurrentMember function.
This puts the CurrentMember function in front of the Name function
where the Member token was. Now there is a Dimension token in
front of the CurrentMember function.

14. Click anywhere in the Dimension token, and double-click the Store
dimension in the Data tree view. The Value expression box now contains:

[Store].CurrentMember.Name

15. Click the OK button, and the data pane should appear as in Figure 11.

Figure 11. The Store Sales cube data pane showing the current member name.

16. Expand the Store dimension levels to see that no matter what level you
look at, the Name column always displays the Store member name from
that row.

17. Leave the Virtual Cube Editor open.

CurrentMember Function
The CurrentMember function is very powerful. In the MDX expression it
refers to whatever cell the expression is evaluating at that moment. It always
returns the current member of one dimension from the cell. Consider the
following facts about the CurrentMember function:

Cells: The term cell describes any one value in the cube. The cell is
described by using one member from each dimension in the cube.
These members represent every cell that you see in the Cube Browser.
Each member that describes the cell is the current member of its
dimension. If the dimension is on the row axis, the current member of
that dimension is on the left of the cell. If the dimension is on the

7-26 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

column axis, the current member of that dimension is at the top of the
column. If the dimension is in the filter area, the current member of
that dimension is the member name that appears next to the dimension
name.
Default: The CurrentMember function is the default function for a
dimension name. When an MDX expression requires a member, and
only the dimension name is specified, MDX tries to use the
CurrentMember function. It is handled through context. For example,
[Store].Name returns the name of the dimension, not the current
member, because a dimension can be used with the Name function.
Member: Since the CurrentMember function returns a member, it is
found in the Member folder of the Functions list.
Two other MDX functions that return member information are:

DefaultMember: Returns the default member of a dimension.


DataMember: Returns the system-generated data member that is
associated with a non-leaf member of a parent-child dimension.

Name Function
The Name function returns the name of an object in a cube. It has four
different versions:

Name Dimension
Name Hierarchy
Name Level
Name Member

NOTE Since hierarchies are treated as separate dimensions in most


queries, the Name Dimension version can be used for
hierarchies.

You choose the function according to the object you are using. In actuality, the
only difference between these functions in the Calculated Member Builder is
the token that Analysis Server uses. The token serves as a guide to what you
are trying to do. If you put a level name into the Dimension token, the
builder will not raise any errors, and a level works with the Name function the
same way.

The Name function returns a string, so it is in the String folder of the Functions
list.

Here are three other functions that work with the metadata of the cube in a
similar way to the Name function:

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-27
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

UniqueName: Returns the fully qualified name of the member, level,


hierarchy, or dimension.
Ordinal: Can only be used with a level. It returns the zero-based
ordinal position of a level within the hierarchy of the dimension. It is
used to show the depth of a level, where the (All) level has an ordinal
value of 0.
UserName: Has nothing to with the objects of the cube, and is used as
a stand-alone function. It returns the domain name and user name of
the user of the cube.

Level Function
The Level function returns the level object for a member. Once you have the
level object, you can use it to get the name of the level or use it with any other
function that is used with levels.

The Level function can only be used with members. In other words, the Level
function is a property of members. Most often, it is used following the
CurrentMember function, like so CurrentMember.Level.

The Level function can be used with the name of a dimension. This is one of
those cases in which the CurrentMember function is used on the dimension
before the Level function is evaluated.

This is not a commonly used function. In most places in MDX expressions


where a level is needed, the level is explicitly named. However, the Level
function will be used with the Name function to return the name of the level of
the current member.

The Level function returns a level, so it is in the Level folder of the Functions
list.

Two similar functions are the Dimension and Hierarchy functions.

Try It Out!
In the following steps, you will modify the Name calculated member so that it
returns the name of a level. The Name member was created in a previous Try
It Out section in this chapter.

1. In the Virtual Cube Editor, where Store Sales is being edited, select the
Name calculated member in the tree pane, and select the Value property
on the Basic tab of the Properties pane.

2. Click the ellipsis button that appears to the right of the Value propertys
value. This is another way to open the Calculated Member Builder for
editing.

7-28 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

3. Clear the contents of the Value expression box.

4. In the Functions list, expand the String folder and double-click the Name
Level function.

5. Click anywhere in the Level token, and in the Functions list, expand the
Level folder, and double-click the Level function.

6. Click anywhere in the Member token, and in the Functions list, expand
the Member folder, and double-click the CurrentMember function.

7. Click anywhere in the Dimension token, and double-click the Store


dimension in the Data tree view. The Value expression box now contains:

[Store].CurrentMember.Level.Name

8. Click the OK button. In the data pane, double-click Store Country and
Store State, and the grid should look something like Figure 12.

Figure 12. The Store Sales cube data pane showing the current level name in the
Name column.

9. Right-click the Name member, select Delete from the context menu, and
click the Yes button in the Confirm message box.

Family Tree
The family tree of a member contains the other related members of the same
dimension. It is another way of expressing the relationships that exist between
members of a dimension. Figure 13 shows a family tree that was used in
another section of this chapter.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-29
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Figure 13. A sample family tree of a Time dimension.

Figure 13 has one highlighted member: Feb, which is a child of Q1, which is a
child of 2002. When an MDX expression is evaluating a cell linked to this
member, the following can be said:

CurrentMember: The CurrentMember function points to that


member.
Parent: The Parent function points to the Q1 member that is its parent
member.
Ancestor: All Time, 2002, and Q1 are all ancestors of the member.
Each ancestor is either at a particular level or at a particular distance.
The ancestor at the Year level is 2002, while the ancestor three levels
distant is All Time.

Parent Function
The Parent function returns the parent member of another member. This
function is only used with members. Usually the Parent function is used with
the CurrentMember function. Since it returns a member object, the Parent
function is in the Member folder of the Functions list.

The following family tree functions are similar to the Parent function:

FirstChild: Returns the first child member of a member.


LastChild: Returns the last child member of a member. Member order
is a property of a level and that order determines first and last
positions.
FirstSibling: Returns the first child of the parent member of the
specified member. In Figure 13, the first sibling of Feb is Jan.
LastSibling: Returns the last child of the parent member of the
specified member.

7-30 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

Try It Out!
In the following steps, you will create a calculated member that is name of the
current members parent.

1. In the Virtual Cube Editor, where Store Sales is being edited, click the
Insert Calculated Member button on the toolbar.

2. Enter Family in the Member name text box.

3. In the Functions list, expand the String folder and double-click the Name
Member function.

4. Click anywhere in the Member token and in the Functions list, expand
the Member folder and locate and double-click the Parent function.
From the token that appears, you can see that this function will only work
with members.

5. Click anywhere in the Member token, and in the Functions list, double-
click the CurrentMember function.

6. Click anywhere in the Dimension token and double-click the Store


dimension in the Data tree view. The Value expression box now contains:

[Store].CurrentMember.Parent.Name

7. Click the OK button. In the data pane, double-click Store Country, and
the grid should look something like Figure 14.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-31
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Figure 14. The Store Sales cube data pane showing the parent member name in
the Family column.

NOTE Note that the All Store member has an empty cell. This happened
because that member has no parent. An MDX expression that
attempts to display a nonexistent member does not cause an error,
but instead returns an empty cell.

Ancestor Function
The Parent function only shows one level up. You can daisy chain the Parent
function to show multiple levels upward, as follows:

[Store].CurrentMember.Parent.Parent.Parent.Name

However, there is a much easier way to do this by using the Ancestor function.
The Ancestor function is not used as a property of a member, but as a function
with parentheses and arguments.

7-32 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

TIP: This is one of the confusing things about MDX expressions. Some functions
are used as properties or methods of the objects in the cube. Other functions
are not attached to objects, but instead specify the objects inside parentheses.
Why does MDX treat them differently? Well, there is no easy answer to the
why, but it is easy to understand when one format is chosen over another. If
the function has no arguments, other than the object it works on, it will be
used as a method or property with the dot notation. If the function takes two
or more arguments, it is a function with parentheses and no dot notation. And
then there are the exceptionsfunctions that use the dot notation and
parentheses.

There are two versions of the Ancestor function:

Ancestor(Member, Level): Returns the ancestor member from


the level specified of the Member specified in the first argument.
Ancestor(Member, Distance): Returns the ancestor member
from a level above the member specified in the first argument, by
using the Distance value to go that many levels upward. You can
specify the parent level by using a Distance of 1.

Try It Out!
In the following steps, you will modify the Family calculated member to use
the Ancestor function. The Family member was created in a previous Try It
Out section in this chapter.

1. In the Virtual Cube Editor, where Store Sales is being edited, select the
Family calculated member in the tree pane, and select the Value property
on the Basic tab of the Properties pane.

2. Click the ellipsis button that appears to the right of the Value propertys
value.

3. Clear the contents of the Value expression box.

4. In the Functions list, expand the String folder and double-click the Name
Member function.

5. Click anywhere in the Member token, and in the Functions list, expand
the Member folder.
Note that there are two versions of the Ancestor function. Double-click the
Ancestor Level function.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-33
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

This function takes two arguments. Usually the member argument will be
the current member of a dimension. The level is a level name from that
dimension.

6. Click anywhere in the Member token, and in the Functions list, double-
click the CurrentMember function.

7. Click anywhere in the Dimension token and double-click the Store


dimension in the Data tree view.

8. Click anywhere in the Level token, then expand the Store dimension,
and double-click the Store Country level in the Data tree view. The result
should be:

Ancestor([Store].CurrentMember,
[Store].[Store Country]).Name

9. Click the OK button. In the data pane, double-click Store Country and
Store State, and the Family column should show the correct country
ancestor for any member at any level.

10. Right-click the Family calculated member and select Edit from the
context menu.

11. This edit is easy, just highlight the [Store].[Store Country] portion of the
Value expression and put 2 in that place. The expression should look like:

Ancestor([Store].CurrentMember, 2).Name

12. Click the OK button. The data pane should look something like Figure 15.
Since the distance number is always relative to the current member of the
cell, the name in the Family column is the grandparent member of the
current member. All Store and the members of the Store Country level are
empty because they have no grandparent members.

Figure 15. The Store Sales cube data pane showing the ancestor member name
two levels up, in the Family column.

7-34 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

13. Right-click the Family member, select Delete from the context menu, and
click the Yes button in the Confirm message box.

Member Properties
If the dimensions of the cube have member properties, they can be accessed in
MDX expressions just like members can be accessed. You can design the cube
to have calculated members that take advantage of those properties.

There is a certain symmetry to using member properties in calculated


members. Member properties do not take up any space in the cube, nor do they
need to be processed. The same is true for calculated members. That means
that a calculated member that uses member properties must locate the
properties and do the calculation when it is queried.

Properties Function
The function that retrieves properties is the Properties function. It is used as a
method of a member object, and the name of the property is used as an
argument to the function. The property name argument is a string expression.

Consider the following factors when you use the Properties function:

Property Name: The property name must be a string expression, so


you have to enclose it in quotes when the name is expressed as a
constant.
Error: If the property name does not exist for the member, then an
error value is returned. This is not an error in retrieval, but a special
error indicator in the cell itself. Spaces in the name are important, but
case is ignored.
Ancestor: The Ancestor function is a good way to navigate to the
level that has the properties. Member properties are set at a level of a
dimension.
String: The Properties function always returns a string, regardless of
the original data type supplied to the property. Even if the property
appears to be a number, MDX treats it as a string.
Functions List: Because it returns a string, the Properties function is
in the String folder of the Functions list.
Data: You can select the Properties function from within the Data tree
view by drilling down to the members with properties and selecting
the desired property there. When you use this approach, the expression
includes the qualified member name.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-35
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Val Function
A member property is always returned as a string. This means that it cannot be
used in numeric calculations. However, you can use the Val function to change
a string expression into a numeric expression.

The Val function is not an MDX function. Instead it is from the Microsoft
Visual Basic for Applications (VBA) library. This library, as well as any
others that are registered, is available for use within MDX expressions.

The Val function takes one argument, a string expression, and returns a
numeric expression. Because it is from the VBA library and not a part of
MDX, it is not in the Functions list in the Calculated Member Builder. All you
need to do is type it into the Value expression box, and when the expression is
queried, Analysis Server will access it in the VBA library.

TIP: Actually, there may be a VBA folder in the Functions list, depending on how
software is loaded on your system.

Try It Out!
In the following steps, you will create a calculated member that does numeric
calculations using member properties.

1. In the Virtual Cube Editor, where Store Sales is being edited, click the
Insert Calculated Member button on the toolbar.

2. Enter Property in the Member name text box.

3. In the Functions list, expand the String folder and double-click the
Properties function.

4. Click anywhere in the Member token, and in the Functions list, expand
the Member folder and double-click the CurrentMember function.

5. Click anywhere in the Dimension token and double-click the Store


dimension in the Data tree view box.

6. Click anywhere in the String expression token, and type Store Type.
The resulting expression should be:

[Store].CurrentMember.Properties("Store Type")

7-36 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

7. Click the OK button. In the data pane, double-click Store Country, Store
State, and Store City.
The Property column shows the appropriate property value for rows that
have store names. But the other rows have the error indicator, #ERR,
which shows that those members do not have that property. One way to get
rid of the error expression is to use the Ancestor function. The members in
levels above the Store Name level do not have the Store Name level as an
ancestor, so the cell values will be empty, not errors.

8. Edit the Property calculated member so that it looks like the following
expression:

Ancestor([Store],[Store Name]).Properties("Store Type")

The first argument to the ancestor function should be a member, but the
expression above only has the dimension name. This is one of those cases
where the CurrentMember function is used by default, since the first
argument must be a member.

9. Click the Check button to make sure that the syntax is correct, and click
the OK button in the message box. Then click the OK button in the
builder.

10. The data pane will look the same as in Step 7, except that there will no
longer be any cells marked with #ERR.

11. Edit the Property calculated member, changing the Store Type to
Frozen Sqft. The Value expression box should look like:

Ancestor([Store],[Store Name]).Properties("Frozen Sqft")

12. Click the Check button to make sure that the syntax is correct, and click
the OK button in the message box. Then click the OK button in the
builder.
When you view the Property column, the value looks just like a number.
But it is not really a number in the MDX expression; it is a string.

13. Edit the Property calculated member and change the expression to:

Ancestor([Store],[Store Name]).Properties("Frozen Sqft")/


Ancestor([Store],[Store Name]).Properties("Store Sqft")

14. Click the Check button, and an error message appears indicating a
problem with the division operator. Click the OK button in the message

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-37
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

box and the division slash becomes highlighted in the Value expression
box.
The operator is not the real problem. The real problem is that the
Properties function returns strings, and you need to do a numeric
calculation on the properties.

15. Change the expression to:

Val(Ancestor([Store],[Store Name]).
Properties("Frozen Sqft"))/
Val(Ancestor([Store],[Store Name]).
Properties("Store Sqft"))

Since the Val function is not an MDX function, it will appear as black text.

16. Click the Check button to make sure that the syntax is correct, and click
the OK button in the message box. Then click the OK button in the
builder.

17. Select the Format String property of Property. It is on the Advanced tab
of the Properties pane. Select Percent from the drop-down list of values
and press the ENTER key. You should see something like Figure 16.

Figure 16. A portion of the Store Sales cube data pane showing the calculated
member for the Property column.

The error expression in all but the Store Name member cells is the result of
dividing an empty expression by another empty expression.

7-38 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

18. Right-click the Property member, select Delete from the context menu,
and click the Yes button in the Confirm message box.

WARNING! The Val function has a dangerous side. It converts strings to numbers
by searching for a decimal point. The danger of this is that it will not
work correctly in some international applications. To avoid this
problem, use the CDbl function instead. This function takes into
account the regional settings of the system it runs on.

Conditional Expressions
MDX handles conditions just like most other computer languages. It interprets
a return value of 1 as True and a return value of 0 as False. Using comparison
operators creates conditional expressions. These are the same as in most
computer languages.

The comparison operators in MDX are:

Equals (=)
Not equal (<>)
Greater than (>)
Less than (<)
Greater than or equal (>=)
Less than or equal (<=)
You can compare strings to strings and numbers to numbers, but not to each
other.

You can link multiple comparisons to create complex conditional expressions,


which consist of two or more conditional expressions.

The conditional operators for combining expressions in MDX are the standard
ones:

OR: Returns True if either of the expressions is True.


AND: Returns True only if both of the expressions are True.
XOR: Returns True only if one of the expressions is True. This is
known as an exclusive OR.
NOT: Used with one expression and it reverses the results of that
conditional expression.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-39
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

As with other languages, parentheses can be used to nest conditional


expressions within other conditional expressions. It is advisable to use
parentheses when you combine three or more conditional expressions.

Iif Function
The Iif, or Immediate If, function takes three arguments. It uses a conditional
expression as the first argument, and then returns one of the other two
arguments depending on whether the conditional expression returns a value of
True or False. The second argument is returned for True, and the third
argument is returned for False.

In MDX, both the second and third arguments must be of the same data type,
and the Iif function returns a value of that type. There are two versions of the
Iif function. One is in the String folder of the Functions list and requires the
second and third arguments to be string expressions. The other is in the
Numeric folder of the Functions list.

This requirement that both arguments be the same data type makes the Iif
function different from the equivalent functions in VBA and Excel.

Try It Out!
In the following steps, you will create a calculated member that uses the Iif
function.

1. In the Virtual Cube Editor, where Store Sales is being edited, click the
Insert Calculated Member button on the toolbar.

2. Enter Condition in the Member name text box.

3. In the Value expression box, build the following statement:

Ancestor([Store],[Store Country]).Name = "USA"

4. Click the OK button.

5. In the data pane, you will see that USA has a value of 1 in the Condition
column, while the other countries and All Store have 0. Double-click the
Store Country button to see the effect on the Store State level.
In this expression, the equals (=) sign is used as a comparison operator
resulting in an expression that returns 1 for True and 0 for False.

6. Edit the Condition member and change the expression to:

7-40 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

Iif(Ancestor([Store],[Store Country]).Name = "USA",


"America", "Foreign State")

7. Click the OK button.


As you can see, the Iif function is used to turn the conditional results of 0
and 1 into more meaningful data.

8. Edit the Condition member and change the expression to:

Iif(Ancestor([Store],[Store Country]).Name = "USA" OR


Ancestor([Store],[Store Country]).Name = "Canada",
"English", "Spanish")

9. Click the OK button. Your results should look something like Figure 17.

Figure 17. A portion of the Store Sales cube data pane showing the logical results
for the Condition column.

The reason that All Store has the value of Spanish is that the test was for
USA or Canada. Since All Store is not one of those, the other result is
returned.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-41
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Summary
MDX is the language of OLAP cubes. It is used to query the cube as
well as for certain properties within the cube.
There are two ways of using MDX, expressions and statements. MDX
statements are the querying tool. MDX expressions create a single
value, just like a spreadsheet formula.
Use MDX expressions to create calculated members. Calculated
members are members that take up no space in the cube or any
processing time. They are calculated based on other members of the
cube and are created at query time. They do impact query
performance.
Calculated members are a better choice than derived measures because
the calculated member is not created until after aggregations are
created. Derived measures are created from the database before
entering the cube and then they are aggregated.
The Calculated Member Builder is an easy-to-use graphical tool for
creating calculated members. It contains all of the metadata of the
cube, as well as a complete list of all MDX functions. It also uses
color coding and other signals to help the edit process.
Every member in a cube has a name that is made up from the
dimension name, level name, and other member names, to uniquely
identify a member separate from other members in the cube. Member
keys can be used in place of member names by adding an ampersand
in front of the key. Brackets are recommended around each object
name.
MDX functions are used as part of MDX expressions to extract
information from the members of the cube. Commonly used functions
include Name, CurrentMember, Parent, Level, Ancestor, Properties,
and Iif.
MDX expressions can also take advantage of other function libraries,
such as the ones that come with VBA and Excel, as long they are
registered for use.
MDX supports conditional expressions through the use of comparison
operators, logical combining operators, and the Iif function.

7-42 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

Questions
1. Is MDX specific to Microsoft?

2. What error message appears when you use ambiguous member names in
MDX?

3. True/False: A cube must be processed after adding a calculated member in


order for the new information to be queried.

4. How do calculated members impact processing and query times?

5. How can you create an empty cell in a calculated expression?

6. What do you use to access the grandparent member of the current


member?

7. You are creating a calculated member that uses a conditional expression. If


the condition is True, the member displays a numeric value. If the
condition is False, the member displays a string value. What MDX
function do you use for this scenario?

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-43
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Calculated Members

Answers
1. Is MDX specific to Microsoft?
No. It is an industry standard.

2. What error message appears when you use ambiguous member names in
MDX?
None. The expression will locate the first member that matches
the name and return that.

3. True/False: A cube must be processed after adding a calculated member in


order for the new information to be queried.
False

4. How do calculated members impact processing and query times?


They have no impact on processing times. They will slow queries
that reference them, but have no impact on queries of other non-
calculated members.

5. How can you create an empty cell in a calculated expression?


Use Null in the expression

6. What do you use to access the grandparent member of the current


member?
Ancestor(dimensionName.CurrentMember, 2)

7. You are creating a calculated member that uses a conditional expression. If


the condition is True, the member displays a numeric value. If the
condition is False, the member displays a string value. What MDX
function do you use for this scenario?
None. This action is not allowed in MDX. The Iif function requires
both arguments that are returned to be the same data type.

7-44 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
MDX Expressions

Lab 7:
Calculated
Members
TIP: The result of this lab is an OLAP database. The Completed folder within this
labs folder has an archived copy of this database. To see the completed
database, right-click on the server name and select Restore Database from
the menu. Then locate and open the CalcMembersLab.CAB file.

Because this lab includes code that you must type in, weve tried to make it
simpler for you. Code needed for this lab can be found in the
CalcMembers.txt file in this labs folder. To avoid typing the code, you can
copy/paste it from the text file instead.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-45
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Lab 7 Overview
In this lab youll learn how to create calculated measures. You will also learn
how they are different from derived measures and can be an improvement over
them. You will learn how calculated members can work together with derived
measures. In this lab, you will use the MDX functions CurrentMember,
Ancestor, and Properties, to create a calculated measure. You will also learn
how to use conditional expressions as part of a calculated member. These
exercises use the data from a Microsoft SQL Server 2000 sample database,
NwindMart.

To complete this lab, youll need to work through three exercises:

Derived Measures vs. Calculated Members


Use Derived Measures with Calculated Members
Using MDX Functions
Each exercise includes an Objective section that describes the purpose of the
exercise. You are encouraged to try to complete the exercise from the
information given in the Objective section. If you require more information to
complete the exercise, the Objective section is followed by detailed step-by-
step instructions.

7-46 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Derived Measures vs. Calculated Members

Derived Measures vs. Calculated


Members

Objective
In this exercise, youll restore the sample OLAP database for the lab,
CalcMembersLab. Then you will use the Inventory cube within the database
and add derived measures and calculated members. By using both types of
calculations, you will learn when to use derived measures and when to use
calculated members.

Things to Consider
The MSSQLServerOlapService service has to be running.
This exercise assumes that you are using Windows authentication, and
that you have read privileges for the NwindMart database on your
SQL Server. If you need to use SQL Server authentication, then enter
the appropriate user name and password.

Step-by-Step Instructions
1. Open Analysis Manager, right-click the local server name, and select
Restore Database from the context menu.

2. In the Open Archive File dialog box, locate the folder for this lab, select
the CalcMembersLabStart.cab file, and click the Open button.

3. In the Restore Database dialog box, click the Restore button, and after the
restore completes, click the Close button in the Restore Database Progress
dialog box.

4. Expand the CalcMembersLab OLAP database folder, expand the Data


Sources folder, right-click the localhost NwindMart entry, and select
Edit from the context menu.

5. In the Data Link Properties dialog box, make sure that the server is
localhost, that the log on information is correct, that the database selected
is NwindMart, and then click the Test Connection button. After the
Microsoft Data Link message box appears, click the OK button. If there
were any errors in the connection, correct them and test them again. Once
the connection works, click the OK button to close the Data Link
Properties dialog box.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-47
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members
6. Expand the Cubes folder of the CalcMembersLab OLAP database, right-
click the Inventory cube, and select Edit from the context menu.

7. In the Cube Editor, click the Data tab at the bottom of the schema pane.

8. In the data pane, pivot the Measures dimension onto the row axis by
dragging the MeasuresLevel button from the column axis of the data grid,
and dropping it on the Family button in the data grid.
This places the measures along the row axis, which makes them easier to
see and compare with each other.

9. Click the Insert Measure button on the Cube Editor toolbar.

10. In the Insert Measure dialog box, select units_shipped from the list and
click the OK button.

11. This adds a new measure called Units Shipped 1 to the cube. Change the
name of this measure to Percent Shipped. Then select the Source Column
property of Percent Shipped, which is on the Basic tab of the Properties
pane.

12. Change the value of the property to the following expression:

"dbo"."inv_storage_facts"."units_shipped" /
"dbo"."inv_storage_facts"."units_ordered"

13. Select the Data Type property on the Advanced tab of the Properties pane,
and select Double from the drop-down list of values.

14. Select the Display Format property on the Advanced tab of the Properties
pane, and select Percent from the drop-down list of values.

15. Click the Process Cube button on the toolbar, click the Yes button in the
Save the Cube message box, and then click the No button in the message
box asking if you want to design aggregations. In the Process a Cube
dialog box, select the Full Process option, click the OK button, and once
the cube completes processing, click the Close button in the Process dialog
box.

16. The data pane should look similar to Figure 18. Notice that the Percent
Shipped is not correct because the aggregation function is Sum, which
adds all of the percentages.

7-48 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Derived Measures vs. Calculated Members

Figure 18. A portion of the Inventory cube data pane, showing the Percent
Shipped derived measure.

17. Drill down into the Product dimension by double-clicking the Drink label,
double-clicking the Alcoholic Beverages label, double-clicking the Beer
and Wine label, and double-clicking the Beer label. Then in the filter
area, open up the Warehouse dimension, and drill down through All
Warehouse|Canada|BC|Vancouver|Bellmont Distributing. Scroll to the
right so you can see individual product name columns. You should see
something like Figure 19. Here you can see that the Percent Shipped
values are accurate.

Figure 19. The Inventory cube data after drilling down.

18. Collapse the data grid back to where it started by double-clicking the
Drink label, and selecting All Warehouse in the filter area as the
Warehouse dimension selection.

19. Click the Insert Calculated Member button on the toolbar.

20. In the Calculated Member Builder dialog box, enter Shipped Percentage
as the Member name.

21. In the Data tree view box, expand the Measures dimension, expand the
MeasuresLevel level, and select Units Shipped. That portion of the
Calculated Member Builder should look like Figure 20.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-49
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Figure 20. A portion of the Calculated Member Builder showing the Data box.

22. Drag the Units Shipped measure from the Data box, and drop it into the
Value expression edit box. Click the / (division) button on the right side of
the builder, and double-click the Units Ordered item in the Data box. The
end result should be the following expression:

[Measures].[Units Shipped]/[Measures].[Units Ordered]

23. Click the Check button. A message box should appear noting that the
syntax is okay. Click the OK button to close the message box.

24. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

25. Back in the Cube Editor, the Format String property of the Shipped
Percentage member should be selected. Select Percent from the drop-
down list of values for the property and press the ENTER key. Your screen
should look something like Figure 21.

Figure 21. A portion of the Inventory cube data pane, after creating Shipped
Percentage.

7-50 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Derived Measures vs. Calculated Members

26. As you can see the calculated member is accurate, without regard to
whether the dimension member values are sums of other members or not.
Calculated members are not aggregated, but are calculated based on the
aggregated values of regular measures.

27. Click the Insert Measure button on the Cube Editor toolbar.

28. In the Insert Measure dialog box, select warehouse_cost from the list and
click the OK button.

29. This adds a new measure called Warehouse Cost 1 to the cube. Change the
name of this measure to Cost Percent Sales. Then select the Source
Column property of Cost Percent Sales, which is on the Basic tab of the
Properties pane.

30. Change the value of the property to the following expression:

"dbo"."inv_storage_facts"."warehouse_cost" /
"dbo"."inv_storage_facts"."warehouse_sales"

31. Select the Data Type property on the Advanced tab of the Properties pane,
and select Double from the drop-down list of values.

32. Select the Display Format property on the Advanced tab of the Properties
pane, and select Percent from the drop-down list of values.

33. Click the Process Cube button on the toolbar, click the Yes button in the
Save the Cube message box, and then click the No button in the message
box asking if you want to design aggregations. In the Process a Cube
dialog box, select the Full Process option, and click the OK button. Your
Process dialog box should look like Figure 22. You can see that there were
divide by zero errors in trying to create the derived measure. Click the
Close button.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-51
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Figure 22. The Process dialog box after creating derived measure.

34. Right-click the Cost Percent Sales derived measure, select Delete from
the context menu, and in the Confirm Measure Delete message box, click
the Yes button.

35. Click the Process Cube button on the toolbar, click the Yes button in the
Save the Cube message box, and then click the No button in the message
box asking if you want to design aggregations. In the Process a Cube
dialog box, select the Full Process option, click the OK button, and once
the cube completes processing, click the Close button in the Process dialog
box.

36. Click the Insert Calculated Member button on the toolbar.

37. In the Calculated Member Builder dialog box, enter Cost Percent Sales as
the Member name, and enter the following into the Value expression box:

[Measures].[Warehouse Cost]/[Measures].[Warehouse Sales]

38. Click the Check button. A message box should appear noting that the
syntax is okay. Click the OK button to close the message box.

39. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

40. Back in the Cube Editor, the Format String property of the Cost Percent
Sales member should be selected. Select Percent from the drop-down list

7-52 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Derived Measures vs. Calculated Members

of values for the property and press the ENTER key. Your screen should
look something like Figure 23.

Figure 23. A portion of the Inventory cube data pane, after creating Cost Percent
Sales.

41. Another difference between derived measures and calculated members is


that by default the calculated member will be empty if a cell it is based on
is also empty. Even if an error does occur, only the cell in error shows an
error. Other cells will show the good values.

42. Drill down into the Product dimension by double-clicking the Food label,
double-clicking the Starchy Foods label, double-clicking the Starchy
Foods label, and double-clicking the Rice label. Scroll to the right to see
Shady Lake Rice Medley. Then in the filter area, open up the Time
dimension, and drill down through All Time|2001|Q4 2001.

43. The cell value for Cost Percent Sales will be -1.#IND which is an error
indicator in the cell. Save the cube and close the Cube Editor.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-53
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Use Derived Measures with Calculated


Members

Objective
In this exercise, youll create a calculated member that depends on a derived
measure. You will also create the derived measure. Derived measures are okay
to use when the values they calculate are additive. That way the aggregate
function of Sum will still work with the measure. Derived measures do have an
advantage for query performance. Calculated members are created when they
are queried. In this exercise, you will create a Warehouse Profit derived
measure, and then use it to create a Profit Percentage calculated member.

Things to Consider
The MSSQLServerOlapService service has to be running.
This exercise assumes that you are using Windows authentication, and
that you have read privileges for the NwindMart database on your
SQL Server. If you need to use SQL Server authentication, then enter
the appropriate user name and password.
The CalcMembersLab OLAP database should already have been
restored from the CalcMembersStart.cab file. If this has not been done,
then perform the restore database operation.
This exercise does not depend on completing the previous exercise in
this lab.

Step-by-Step Instructions
1. In Analysis Manager, right-click the Inventory cube in the
CalcMembersLab OLAP database, and select Edit from the context menu.

2. In the Cube Editor, click the Data tab at the bottom of the schema pane.

3. In the data pane, pivot the Measures dimension onto the row axis by
dragging the MeasuresLevel button from the column axis of the data grid,
and dropping it on the Family button in the data grid.

4. Click the Insert Measure button on the Cube Editor toolbar.

5. In the Insert Measure dialog box, select warehouse_sales from the list and
click OK.

7-54 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Use Derived Measures with Calculated Members

6. This adds a new measure called Warehouse Sales 1 to the cube. Change
the name of this measure to Warehouse Profit. Then select the Source
Column property of Warehouse Profit, which is on the Basic tab of the
Properties pane.

7. Change the value of the property to the following expression:

"dbo"."inv_storage_facts"."warehouse_sales" -
"dbo"."inv_storage_facts"."warehouse_cost"

8. Click the Process Cube button on the toolbar, click the Yes button in the
Save the Cube message box, and then click the No button in the message
box asking if you want to design aggregations. In the Process a Cube
dialog box, select the Full Process option, click the OK button, and once
the cube completes processing, click the Close button in the Process dialog
box.

9. Click the Insert Calculated Member button on the toolbar.

10. In the Calculated Member Builder dialog box, enter Profit Percentage as
the Member name, and enter the following into the Value expression box:

[Measures].[Warehouse Profit]/[Measures].[Warehouse Cost]

11. Click the Check button. A message box should appear noting that the
syntax is okay. Click the OK button to close the message box.

12. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

13. Back in the Cube Editor, select the Format String property of the Cost
Percent Sales member, which is found on the Advanced tab of the
Properties pane. Select Percent from the drop-down list of values for the
property and press the ENTER key.

14. For All Store, All Time, All Warehouse, All Product, you should see a
value of 120.48%. This makes sense because the Warehouse Profit is
greater than the Warehouse Cost. Save the cube and close the Cube Editor.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-55
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Using MDX Functions

Objective
In this exercise, youll learn how to use MDX functions in the creation of
calculated members. You will create a calculated member that will use
member properties from the Store dimension of the Inventory cube. The
functions will be used to obtain the correct member level from the Store
dimension, to know which cell is being processed, to retrieve properties, to
convert them into numbers, and to apply conditions to eliminate those values
that make no sense. The calculated member will be one that uses two
properties of the Store Name level to calculate a percent of store space allotted
to groceries.

Things to Consider
The MSSQLServerOlapService service has to be running.
This exercise assumes that you are using Windows authentication, and
that you have read privileges for the NwindMart database on your
SQL Server. If you need to use SQL Server authentication, then enter
the appropriate user name and password.
The CalcMembersLab OLAP database should already have been
restored from the CalcMembersStart.cab file. If this has not been done,
then perform the restore database operation.
This exercise does not depend on completing the previous exercises in
this lab.

Step-by-Step Instructions
1. In Analysis Manager, right-click the Inventory cube in the
CalcMembersLab OLAP database, and select Edit from the context menu.

2. In the Cube Editor, click the Data tab at the bottom of the schema pane.

3. In the data pane, pivot the Measures dimension onto the row axis by
dragging the MeasuresLevel button from the column axis of the data grid,
and dropping it on the Family button in the data grid. Then pivot the Store
dimension onto the column axis by dragging the Store button from the
filter area, and dropping it on the Family button in the data grid.

4. Drill down into the Store dimension by double-clicking the Canada label,
double-clicking the BC label, and double-clicking the Vancouver label.

7-56 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Using MDX Functions

Also double-click the Victoria label. Scroll to the right so that your screen
looks like Figure 24.

Figure 24. A portion of the Inventory cube data pane, showing data for Stores 19
and 20.

5. Click the Insert Calculated Member button on the toolbar.

6. In the Calculated Member Builder dialog box, enter Grocery Space as the
Member name, and enter the following into the Value expression box:

[Store].CurrentMember.Name

7. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

8. In the data pane, you will see that the Name function returns the store
dimension member name, and the CurrentMember function refers to the
current cell. As you scroll to the right, you will see the column headers for
the Store dimension repeated in the Grocery Space member values.

9. Right-click the Grocery Space calculated member, and select Edit from
the context menu.

10. Change the Value expression to the following:

[Store].CurrentMember.Properties("Grocery Sqft")

11. Click OK to save the new calculated member and close the Calculated
Member Builder dialog box.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-57
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members
12. In the data pane, you will see that the Properties function returns the value
of the property for those Store members on the Store Name level. All other
members have the #ERR indicator, which you want to eliminate.

13. Right-click the Grocery Space calculated member, and select Edit from
the context menu.

14. Change the Value expression to the following:

Ancestor([Store].CurrentMember,[Store Name]).
Properties("Grocery Sqft")

15. Click the Check button. A message box should appear noting that the
syntax is okay. Click OK to close the message box.

16. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

17. The Ancestor function retrieves the member at the level indicated, if it is
above the current members level. In this example, none of the other levels
of the Store dimension are below the Store Name level. That means that
they have no ancestor at that level. Instead of displaying an error indicator,
Analysis Server just makes the cell blank.
The members at the Store Name level have themselves as ancestors, so the
Properties function is then applied and the value shows in the cells.

18. Now you want to create a calculation of the grocery square footage as a
percent of the total store square footage. The Properties function always
returns a string, so the Val function is used. Right-click the Grocery
Space calculated member, and select Edit from the context menu.

19. Change the Value expression to the following:

Val(Ancestor([Store].CurrentMember,[Store Name]).
Properties("Grocery Sqft")) /
Val(Ancestor([Store].CurrentMember,[Store Name]).
Properties("Store Sqft"))

20. Click the Check button. A message box should appear noting that the
syntax is okay. Click the OK button to close the message box.

21. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

7-58 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Using MDX Functions

22. Select the Format String property on the Advanced tab of the Properties
pane and select Percent from the drop-down list of values for the property.
Press the ENTER key.

23. The cells corresponding to Store Names look fine, with a percentage, but
the other member cells have divide by zero indicators. Even though the
Ancestor function eliminated one problem, it created another when you
added the division operator. Since the Ancestor function returns an empty
cell when there is no member above the current member, the division
operation treats the empty cell as zero, and creates the error.

24. MDX does not allow for results to be different types, so the best that can
be done is to create some numeric value to replace the error indicator. This
can be done by using the Iif (Immediate If) MDX function. The first
parameter to the function must be the logical expression. That expression
can check to see if the current member of the Store dimension is at the
Store Name level.

25. Right-click the Grocery Space calculated member, and select Edit from
the context menu.

26. Change the Value expression to the following:

Iif([Store].CurrentMember.Level.Name = "Store Name",


Val(Ancestor([Store].CurrentMember,[Store Name]).
Properties("Grocery Sqft")) /
Val(Ancestor([Store].CurrentMember,[Store Name]).
Properties("Store Sqft")), 0)

27. Click the Check button. A message box should appear noting that the
syntax is okay. Click the OK button to close the message box.

28. Click the OK button to save the new calculated member and close the
Calculated Member Builder dialog box.

29. Now the non-Store Name members show 0.00%. This may not be the
most desirable result, but at least it is better than showing errors when
there are none. This way those cells with errors are real errors, and there
are some square footage properties that show zeroes.

30. Pivot the Measures dimension onto the column axis by dragging the
MeasuresLevel button from the row axis of the data grid, and dropping it
on the Store Country button in the data grid. Then expand the Store
dimension fully by double-clicking Store Country, Store State, and
Store City. Scroll all the way to the right and you should see what is
shown in Figure 25. Note the error for Store 21. It has zero values for
Store Sqft and Grocery Sqft.

SQL Server 2000: OLAP Cubes and Queries Professional Skills Development 7-59
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
Lab 7:
Calculated Members

Figure 25. A portion of the Inventory cube data pane, showing the Grocery Space
calculated member.

31. Save the Inventory cube and close the Cube Editor.

7-60 SQL Server 2000: OLAP Cubes and Queries Professional Skills Development
Copyright by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.

Вам также может понравиться