Module 3 Using MDX
Module 3 Using MDX
Expressions
Contents
Overview 1
Using MDX Expressions 2
Lab A: Using Expressions from Constants 12
Displaying Member Information 17
Displaying Family Tree Relatives 29
Working with Member Properties 38
Lab B: Displaying Cube Metadata 46
Using Conditional Expressions 55
Lab C: Using Conditional Expressions 64
Review 68
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, BackOffice, MS-DOS, Windows, Windows NT, ActiveX, Excel, PowerPoint, SQL
Server, and Visual Basic for Applications are either registered trademarks or trademarks of
Microsoft Corporation in the U.S.A. and/or other countries.
The names of companies, products, people, characters, and/or data mentioned herein are fictitious
and are in no way intended to represent any real individual, company, product, or event, unless
otherwise noted.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Instructor Notes
Presentation: In this module, students will learn how to create very simple multidimensional
100 Minutes expressions (MDX). These are expressions that use constant values or
expressions that display the names of members. Students will also learn how to
Labs: work with members in a dimension and how to use MDX member functions to
50 Minutes navigate from one member to another.
After completing this module, students will be able to:
! Create MDX expressions in calculated members and manipulate the
expressions by using numeric and string constants.
! Display information about a member—particularly the name of the member
and the name of the level of the member.
! Display information about the parent or other ancestor of a member.
! Display the member property associated with a member, and use the value
in an arithmetic expression.
! Create and use simple and complex conditional expressions.
Required Materials
To teach this module, you need the following materials:
! Microsoft® PowerPoint® file 2093A_03.ppt
Preparation Tasks
To prepare for this module, you should:
! Read all the materials for this module.
! Read the instructor notes and margin notes.
! Practice the lecture and group activities.
! Complete the labs.
! Review the Trainer Preparation materials on the Trainer Preparation
compact disc.
If the Market database already exists from a previous group exercise or lab,
and cubes in the database contain extraneous information, you can return the
Market database and its cubes to a beginning position by either:
! Deleting any calculated members that were created in a specific cube and
then saving the cube.
- or -
! Repeating the previous restore database procedure.
Other Activities
Difficult Questions
Below are difficult questions that students may ask you during the delivery of
this module and answers to the questions. These materials delve into subjects
that are within the scope of the module but are not specifically addressed in the
content of the student notes.
1. Are calculated members stored in the cube or aggregated?
The values of a calculated member are not stored or aggregated. The
definition of the calculated member is executed each time a client
application establishes a connection to the cube that requests the
calculated member’s values.
2. Do calculated members execute for all of the members of the cube?
No. A calculated member is executed only when the value is actually
displayed on the browser grid. Unless a cell is displayed in the browser,
its formula is never calculated.
3. Is the Empty value (Null) a string or a number?
The Empty value is treated as a number—typically as the number zero.
4. What is the difference between the four Name functions—dimension,
hierarchy, level, and name?
There is only one Name function. It will return the name of whatever
object it is used with. The four entries in the Functions list simply put
different tokens in front of the function name.
5. How do I find the available Microsoft Excel or Microsoft Visual Basic® for
Applications (VBA) functions?
Run Books Online for Microsoft Analysis Services, and click the Index
page. Type function libraries and choose Excel or VBA. All functions
that do not have an asterisk are available for use from MDX.
6. To use an Excel function from MDX, does Excel have to be installed on the
client or on the server?
Under default conditions, an external library (including Excel or VBA)
must be available both on the client on the server. That is because the
expression may be evaluated either by PivotTable Services (on the
client) or the Analysis server. It is possible to create a connection in a
way that forces expressions to be evaluated exclusively on the client or
on the server as opposed to the default, which is that the expression
could be evaluated in either place, and an external library must be
available on both the client and the server.
7. Why does the CurrentMember function follow the dimension name in
COM object format, whereas the Ancestor function uses arguments?
Some MDX functions use property-style syntax, whereas others use
argument-list syntax. Only functions that require no arguments can use
the property-style syntax. The choice between the two styles often
appears arbitrary, and you simply look at the syntax example to see
which to use.
8. Is there a way for an MDX expression to find out what member properties
are available for a given level?
No. When using MDX, you must know the name of the member
property and the level at which it applies. You cannot refer to a
property by number, and you cannot request the count of the
properties. You can find this information out programmatically by
using Microsoft ActiveX® Data Objects (Multidimensional) (ADO MD),
but not by using an MDX expression.
9. When would you use a string value from an MDX expression in a practical
situation?
One example is to show the name of the best-selling product for any
given state.
10. Can an MDX expression return a date?
Typically, when you want to display a date, you are displaying the name
of a member on a Time dimension, which is a string. If you want to
display a true date or time value, it is really a formatted number.
Module Strategy
Each major section of this module begins with a group activity followed by a
review lecture and then labs. The following guidelines are for delivering
materials in the context of group activities:
! Using group activities to introduce new content
You often introduce new concepts or functionality while delivering the
procedures in a group activity as a live demonstration. For example, you
may present a new MDX function by showing first its construction and then
its result set as an actual calculated member formula or within a query
statement.
Use the topic slides that follow the group activity as a review of the content,
for example, the syntax of a specific function.
! Interaction with students
A group activity flows best when you deliver it as a shared exploration. Ask
students such questions as: “What would happen if we…?” “Why did this
happen?” “Was that what you expected?” Encourage students to ask you
questions about the results being tested.
! Students follow along
In some cases, you may want to encourage students to follow your live
demonstration on their own computers. This practice works best for simpler
group activities or for a group activity that is not replicated by a later lab.
It is not a problem if a student does not follow your demonstration, or if a
student starts following and then stops before the group activity is
completed. There is no file or structure dependency between group activities
or between a group activity and a later lab.
! Lab replication of group activity
The exercises in the labs closely follow the group activity procedures but do
not define each step or show the code answer. Encourage students to write
and test the MDX expressions on their own, referring back to the group
activity procedures for clarification. Students can also refer to answer files
that are available for each procedure in the exercises.
Labs are generally more challenging when students have not followed the
instructor on their own computers during the group activity. However, many
students benefit from the two hands-on experiences of following the group
activity and then completing the labs.
! Answer files for group activities
Where applicable, answer files are provided for each procedure in a group
activity. If necessary to facilitate your demonstration, copy and paste the
correct expression from the answer file into the Calculated Member Builder.
Overview
Topic Objective
To provide an overview of
the module topics and ! Using MDX Expressions
objectives.
Lead-in ! Displaying Member Information
In this module, you will learn
how to create calculating ! Displaying Family Tree Relatives
members by using MDX
! Working with Member Properties
expressions plus how to
view metadata and work ! Using Conditional Expressions
with conditionals.
In this module, you will learn how to create very simple multidimensional
expressions (MDX). These are expressions that use constant values or
expressions that display the names of members. You will also learn how to
work with members in a dimension, and how to use MDX member functions to
navigate from one member to another.
After completing this module, you will be able to:
! Create MDX expressions in calculated members and manipulate the
expressions by using numeric and string constants.
! Display information about a member—particularly the name of the member
and the name of the level of the member.
! Display information about the parent or other ancestor of a member.
! Display the member property associated with a member, and use the value
in an arithmetic expression.
! Create and use simple and complex conditional expressions.
In this section, you learn about the basic elements of MDX expressions,
including how to use them to create calculated members and how to construct
MDX expressions by using arithmetic operators and string text.
This group activity introduces concepts and mechanics for creating MDX
Delivery Tip expressions by using constants. Topics that follow the group activity include:
Briefly explain the bullets on
this slide to provide context ! MDX Expressions
for the upcoming group
activity in which the items This topic describes how MDX expressions are used to create calculated
are functionally members. Calculated members add the calculating ability of a spreadsheet to
demonstrated. an online analytical processing (OLAP) database.
! Expression Operators
This topic describes how expression operators are used in MDX expressions
to return a value that can result from an arithmetic or string text
manipulation.
In this group activity, you may follow along on your own computer with your
instructor or observe the demonstration.
You will learn how to use constants to create MDX expressions in a calculated
member. Constant expressions display the same value in all the cells. You will
also learn how to incorporate numeric and string text operators in expressions.
Calculated members are created in the Basic Sales virtual cube. This cube has
only one measure—Sales Dollars—and three dimensions. Creating a calculated
member in a virtual cube is the same as creating one in a regular cube.
The examples may seem easy, but they allow you to focus on the main concepts
of this group activity, including:
! How to enter expressions in the Calculated Member Builder.
! How the formula is automatically applied to all necessary cells in the grid.
! How an expression always returns a single value—a string, a number, or an
empty cell—each time it is calculated.
One important use of MDX expressions is to create calculated members.
Calculated members add the calculating ability of a spreadsheet to an OLAP
database.
Delivery Tip
ConstantNumberD.txt in
C:\MOC\2093A\Demo\D03\
Answers contains the
completed MDX expression
for this procedure.
Notice that the value from the calculated member automatically expands to
fill the needed cells. This differs from a spreadsheet, where a user must copy
values into new rows and delete them from old rows.
4. To remove the Subcategory level, double-click the Category button.
3. In the browser, notice that the values in the Constant column change to the
word Hello.
Note You can use a text string as a constant value in a calculated member
if you enclose the string in quotation marks.
You cannot leave the Value property empty. If you want to create an empty
cell, you must use the keyword Null.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
6 Module 3: Using MDX Expressions
Tip When you first create a new calculated member, assign Null as its
value. You can then see the empty cells in the browser, which helps you
visualize the context for the MDX expression.
Notice that the name at the top of the calculated column changes to
Expression.
Again, the grid is similar to a spreadsheet, where the value or formula that
you type is automatically copied into all of the relevant cells—that is, it is as
if there were several different copies of the formula, each one calculating a
value independently of the others.
In an MDX expression, you can use any standard arithmetic operator:
• Plus sign (+) for addition
• Minus sign (-) for subtraction
• Asterisk (*) for multiplication
• Slash mark (/) for division
• Caret (^) for raising to a power (exponent)
MDX Expressions
Topic Objective
To highlight the types of
! Are Used to Create Calculated Members
data values that can be
used in an expression. ! Function Like Spreadsheet Formulas
Lead-in $ Calculated for each cell of browser grid
The most important use for ! Are Entered by Using the Calculated Member Builder
an MDX expression is as
the value of a calculated ! Are Edited by Using Calculated Member Builder or Value Property
member. ! Are Renamed by Using Name Property
! Expression Can Return:
$ Number
$ Text String
$ Empty Value
Tip When you first create a new calculated member, assign Null as its
value. You can then see the empty cells in the browser, which helps you
think of an expression in concrete terms.
Expression Operators
Topic Objective
To explain the elements of ! Arithmetic or String Used in Creating MDX Expressions
expression operators.
! Arithmetic Operators Combine Numeric Values
Lead-in
Think of MDX expressions $ Add: +
as simple formulas.
In the same way that you $ Subtract: –
can create formulas in a
worksheet cell, you can $ Multiply: *
create simple formulas by $ Divide: /
using an MDX expression.
$ Raise to a power: ^
! String Operator Combines String Values by Using +
! Cannot Combine Numeric and String Values
Integrate your lecture with the demonstration following the procedures for Expressions from Constants in the instructor notes.
Tip You can include spaces between tokens in an MDX expression. Spaces
around punctuation make an expression much easier to read. You cannot
include extra spaces in the name of a member or other cube object, nor can you
include extra spaces within quotation marks.
Consider the following facts and guidelines when using MDX expression
operators:
! In an MDX expression, you can use any standard arithmetic operator:
• Plus sign (+) for addition
• Minus sign (-) for subtraction
• Asterisk (*) for multiplication
• Slash mark (/) for division
• Caret (^) for raising to a power (exponent)
! The value of an MDX expression can also be a string created by using a plus
sign that joins together two text strings. This is different from an Excel
formula, which uses an ampersand (&) to combine text strings. In Visual
Basic, you can use either a plus sign or an ampersand.
! MDX does not do type conversions—that is, it does not combine numeric
and string values. In a numeric expression, both operands must be numbers.
In a string expression, both operands must be strings.
This differs from Excel and Visual Basic. You cannot use an expression,
such as “The value is ” + 45. Rather, you would need to use an expression
where both parts are strings, such as “The value is ” + “45”.
Objectives
Explain the lab objectives. After completing this lab, you will be able to:
! Use the Calculated Member Builder.
! Create simple expressions by using constants.
Prerequisites
Before working on this lab, you must have successfully completed modules 1
and 2 in course 2093A, Implementing Business Logic with MDX in Microsoft
SQL Server™ 2000.
Exercise 1
Creating a Constant Measure
In this exercise, you will create a calculated member on the Measures
Delivery Tip dimension of the Basic Sales cube, assigning it various expressions that use
The procedures in this
exercise essentially
only constant values.
replicate the previous group As you complete each procedure, compare the result shown in the procedure
activities, but without the
screen shot to the result on your own computer. If there is a difference, recheck
answers.
your entry and refer back to the related group activity procedures as necessary.
If you still cannot reconcile your result, then refer to the answer file, which is
located in:
C:\MOC\2093A\Labfiles\L03\Answers
You can copy and paste expressions from this file into the Value expression
box for any given procedure.
Before beginning the exercises in this lab, verify that the following objects exist
in Analysis Manager:
! The Market database
! The Basic Sales virtual cube
If these objects do not exist in Analysis Manager, you must perform the steps in
the following procedure before you continue with this lab.
2. Press ENTER, and then verify that the browser content is similar to the
following table.
2. Press ENTER, and then verify that the browser content is similar to the
following table.
2. Press ENTER, and then verify that the browser content is similar to the
following table.
2. Press ENTER, and then verify that the browser content is similar to the
following table.
In this section, you learn how to display metadata information such as member
Delivery Tip names, level names, and dimension names. You do not display actual values
Briefly explain the bullets on
this slide to provide context
from the cube.
for the upcoming group The group activity introduces how to use the Functions list and specific
activity where the items are
functions for displaying member information. Several topics follow this group
functionally demonstrated.
activity:
! Using MDX Functions
This topic describes how to access, learn about, and use MDX functions by
using the Functions list in the Calculated Member Builder.
Functions in MDX are the foundation for building expressions. In the
Calculated Member Builder, you combine functions with member, level,
and dimension objects to create expressions that define a calculated
member.
! CurrentMember Function
This topic describes how to display the names of current members by using
the CurrentMember function. The CurrentMember function returns the
current member.
! Name Function
This topic describes how to display a member name by using the Name
function. The Name function returns the name of a metadata object as a
constant.
! Level Function
This topic describes how to display the level name of a current member by
using the Level function. This function returns the level of the current
member.
In this group activity, you may follow along on your own computer with your
instructor or observe the demonstration.
You will learn how to use MDX functions to display metadata information.
Metadata is the naming information that describes the specific dimensions,
levels, and members in a cube.
You will also learn how to use the Functions list in the Calculated Member
Builder. In that list, you will find the Name function in the String group and the
CurrentMember function in the Member group.
You will not retrieve specific numeric values from the cube. Retrieving numeric
values is addressed extensively in later modules.
Tip When you insert a function from the Functions list, you see the syntax
for the function with placeholder tokens enclosed in chevrons (« and »).
Tokens are easy to replace: simply click anywhere in the token to select it,
and then double-click an appropriate item from the Data or Functions list.
3. Click in the «Member» token, and then, in the Data list, expand Product
dimension and Category level.
Member objects have a lollipop-shaped icon.
4. Double-click the Bread member to change the expression to:
[Product].[Bread].Name.
Notice the word “Bread” in all rows of the Name column. The expression
displays the name of the specified member, which does not change from
member to member.
Notice the correct name of the product in each row of the Name column.
The result of the function changes from cell to cell, based on the current
member.
6. Expand the Product dimension levels and notice that the calculated value of
the current member expands as needed.
7. To view a different dimension on the Rows axis, drag the State dimension
button to the Category button.
The value in the Name column changes to All Product for all the rows.
This occurs because the All Product member is selected in the filter list
above the grid. The value in the filter list is the current member for that
dimension.
8. In the Product filter list, click Bread.
The value in the Name column changes to Bread because Bread is now the
current member of the Product dimension.
9. To put the Product dimension back on the Rows axis, drag the Product
dimension button to the Country button.
Note Each cell in the grid has a current member for each dimension. If a
dimension is represented on the Rows or Columns axis, the current member
is the member that appears on the current row or column. If a dimension
appears in the filter area, the current member is the member that appears in
the filter box.
2. To return to the browser, click OK, and then to display all the levels of the
Product dimension, double-click the Category button and the Subcategory
button.
3. If necessary, scroll to view the Name column.
This displays a changing level name, depending on the current member of
the Product dimension. The top few rows of the grid appear similar to the
following table.
3. To return to the browser, click OK, and then scroll as needed to view the
Name member.
All the cells of the Name calculated member display Product—the name of
the dimension. The CurrentMember function can be omitted after the
dimension name only in a context where a member is required. The Name
function can follow a dimension as well as a member, so omitting the
CurrentMember function displays the name of the dimension.
CurrentMember Function
Topic Objective
To review the
CurrentMember function. ! Returns the Current Member of a Dimension
Lead-in ! Is Label on Row or Column, or Filter Label
If you want an expression to
return a different value for ! Is Default Function for a Dimension
each member, use the
CurrentMember function. ! Appears in Member Group of Functions List
Product.CurrentMember.Name
Product.CurrentMember.Name
Returns
Returnsthe
thename
nameof
ofthe
thecurrent
current
member
member of the Productdimension
of the Product dimension
When using the CurrentMember function, consider the following facts and
Delivery Tip guidelines:
Because students have
already been exposed to the ! Each cell in a browser grid has a current member for each dimension. If a
CurrentMember function in dimension appears on the Rows or Columns axis, the current member is the
the previous group activity,
member that appears on the current row or column. If a dimension appears
use this topic as review—
that is, do not spend a lot of
in the filter area, the current member is the member that appears in the filter
time. box.
! The CurrentMember function is the default function for a dimension—that
is, if you use a dimension name in a context where a member is required,
MDX will attempt to insert the CurrentMember function to convert the
dimension to a member.
For example, in the expression Product.Level.Name, the word Product is
a dimension, but the Level function must be preceded by a member, so
MDX interprets the expression as if it were
Product.CurrentMember.Level.Name.
If, however, a dimension is valid in the context, MDX does not insert the
CurrentMember function.
Related Functions MDX includes the following other functions that display metadata similar to the
CurrentMember function:
! The DefaultMember function returns the default member of a dimension.
Delivery Tip
These related functions are ! The DataMember function returns the system-generated data member
included for reference only. associated with a non-leaf member of a parent-child dimension.
Do not spend any time
explaining them.
You can find more information about these functions by searching on the
function name in Microsoft SQL Server Books Online.
Name Function
Topic Objective
To review the Name
function.
Lead-in ! Returns Member Name As a String
MDX expressions return ! Has Three Versions
metadata—specifically, the
name of a dimension, level, $ Name – Dimension or Hierarchy
or member. Name is the
most elemental function. $ Name – Level
$ Name – Member
! Appears in String Group of Functions List
[Colony
[Colony Bagels].Name
Bagels].Name
Returns
Returnsthe
thestring
string“Colony
“ColonyBagels”
Bagels”
Example 2 The following example returns the name of the level for the current member of
the Product dimension.
Product.CurrentMember.Level.Name
When you use the Name function, consider the following facts and guidelines:
Delivery Tip
Because students have ! There are three versions of the Name function:
already seen the Name
function in the previous • Name – Dimension
group activity, use this topic
• Name – Level
as a review—that is, do not
spend a lot of time. • Name – Member
Which one of these three you use depends on the kind of metadata object
you want to display. All three functions produce the same results. That is, if
you select the Name – Dimension function and then put a Level object in
front of it, you will automatically get the Name – Level version of the
function. The three entries in the Functions list simply give you helpful
tokens for building the rest of the expression.
! Because name is a text string, the Name function and its variations appear
in the String group of the Functions list.
Related Functions MDX includes the following other functions that display metadata similar to the
Name function:
! The UniqueName function can follow a member, a level, a dimension, or a
Delivery Tip hierarchy. It returns a string that includes the entire hierarchy for the
These related functions are
included for reference only.
dimension, level, or member.
Do not spend any time ! The Ordinal function can follow only a level object. It returns the position
explaining them. of a level in the hierarchy, with zero for the All level.
! The UserName function appears all by itself, without following any other
object. It returns the name of the current user, including both the domain
name and the user identification. The name of the current user is
independent of the current cube, so the UserName function stands
completely alone—as if it were a string constant.
You can find more information about these functions by searching on the
function name in Microsoft SQL Server Books Online.
Level Function
Topic Objective
To review the Level ! Returns Level of a Member
function.
Lead-in ! Often Follows the CurrentMember Function
You use the Level and ! Can Be Followed by the Name Function
Ordinal functions to return
level information about a ! Appears in the Level Group of the Functions List
current member.
Product.CurrentMember.Level.Name
Product.CurrentMember.Level.Name
Returns
Returnsname
nameofoflevel
levelofofthe
thecurrent
current
member
memberininProduct
Productdimension
dimension
You can return the level for a member by using the Level function. After you
have a level object, you can display that level’s name.
Syntax «Dimension».CurrentMember.Level
Example To return the name of the level for the current member of the Product
dimension, use the following expression:
Product.CurrentMember.Level.Name
When you use the Level function, consider the following facts and guidelines:
Delivery Tip
Because students have ! The Level function can follow only a member object. It often follows the
already been exposed to the CurrentMember function. You cannot display the level of a dimension.
Level function in the Using the Level function after a dimension name returns the level of the
previous group activity, treat
current member of that dimension.
this topic as a review—that
is, do not spend a lot of ! Several MDX functions use a level argument as an object, but in most cases,
time. you specify an explicit level, rather than using the Level function. One
common use is to combine the Level function with the Name function to
find the name of the level of the current member of a dimension.
! Because it returns a level object, the Level function appears in the Level
group of the Functions list.
Related Functions MDX includes the following other functions that return metadata objects
similar to the Level function:
! The Dimension function returns a dimension object for a member, level, or
Delivery Tip hierarchy.
These related functions are
included for reference only. ! The Hierarchy function returns a hierarchy object for a member or level.
Do not spend any time
explaining them.
You can find more information about these functions by searching on the
function name in Microsoft SQL Server Books Online.
In this section, you will learn how to use the Parent function to find the parent
Delivery Tip of the current member in a dimension and how to use the Ancestor function to
Briefly explain the bullets on
this slide to provide context
find the ancestor of the current member.
for the upcoming group The group activity introduces how to navigate the family tree, again by using
activity in which the items
the Name function to display member names. Three topics follow this group
are functionally
demonstrated.
activity:
! Navigating the Family Tree
This topic describes the concepts of parent and ancestor in the context of a
dimension hierarchy.
! Parent Function
This topic describes how to navigate up one level in the family tree by using
the Parent function. The Parent function returns the member that is the
parent of a specified member.
! Ancestor Function
This topic describes how to navigate up an arbitrary number of levels in the
family tree by using the Ancestor function. The Ancestor function returns
the ancestor of a member either at a specified level or at a specified distance
away in the hierarchy.
In this group activity, you may follow along on your own computer with your
instructor or observe the demonstration
You will learn how to use MDX functions to navigate the family tree of a
dimension hierarchy. Specifically, you will learn how to use the Parent
function and the Ancestor function, both of which return members and appear
in the Member group of the Functions list.
Notice that the name of the parent for the All Product member is blank. The
All Product member is at the top of the hierarchy and does not have a
parent. Attempting to display the name of a nonexistent member does not
return an error, but simply an empty cell.
7. Click in the «Level» token, and then expand the Product dimension and
double-click the Category level. The result is
Ancestor([Product].CurrentMember,[Product].[Category]).Name
2. To display the name of the grandparent for each member, click OK, and
then scroll as needed to view the Family column and the beginning rows of
the Dairy category.
Notice that the names at the Product Name level show the category for that
product. Notice that the name for the Category level member is blank.
Category does not have a grandparent, so the Ancestor function simply
returns an empty cell.
By using the Ancestor function—coupled with the CurrentMember
function—you can display the ancestor of the current member, either at a
relative position in the hierarchy or at a specific level of the hierarchy.
All State
The CurrentMember function returns the member that is on the current row,
column, or filter. After you have that member, you can use other functions that
return a member that is related to the current member. Some of these functions
navigate the family tree.
For example, assume that the current member of the State dimension is
Oregon, and notice the following:
! The Parent of the current member is North West.
! The Ancestor of the current member at the Country level is USA.
! The Ancestor that is three levels distant from the current member is
All State.
These three relationships are captured by the Parent function and by the two
versions of the Ancestor function.
Parent Function
Topic Objective
To review the Parent
function.
Lead-in ! Returns the Parent of the Specified Member
You can use the Parent ! Often Follows the CurrentMember Function
function to return the parent
of a specified member. ! Appears in the Member Group of the Functions List
Product.CurrentMember.Parent.Name
Product.CurrentMember.Parent.Name
Returns
Returnsthe
thestring
string“Bread”
“Bread”ififthe
thecurrent
current
member is Bagels
member is Bagels
You can use the Parent function to return the parent of a specified member.
Syntax «Member».Parent
Example The following example returns the string “Bread” if the current member of the
Product dimension is Bagels.
Product.CurrentMember.Parent.Name
Delivery Tip
Because students have When you use the Parent function, consider the following facts and guidelines:
already been exposed to the ! The Parent function can follow only a member object. It often follows the
Parent function in the
previous group activity, treat
CurrentMember function. You cannot display the parent of a dimension.
this topic as a review—that Using the Parent function after a dimension name returns the parent of the
is, do not spend a lot of current member of that dimension.
time. ! The Parent function appears in the Member group of the Functions list.
Related Functions MDX includes the following other functions that return family tree members
similar to the Parent function:
! The FirstChild function returns the first child of a specified member.
Delivery Tip
These related functions are ! The LastChild function returns the last child of a specified member.
included for reference only.
! The FirstSibling function returns the first child of the parent of a specified
Do not spend any time
explaining them.
member.
! The LastSibling function returns the last child of the parent of a specified
member.
You can find more information about these functions by searching on the
function name in Microsoft SQL Server Books Online.
Ancestor Function
Topic Objective ! Returns the Ancestor of a Member
To review the Ancestor
function. $ At a specified level
Lead-in
$ At a specified distance in the hierarchy
The Ancestor function is a
generalized version of the ! Equivalent to Parent Function When Distance = 1
Parent function.
Ancestor(Product.CurrentMember,Category).Name
Ancestor(Product.CurrentMember,Category).Name
Returns
Returnsthe
thestring
string“Bread”
“Bread”ififthe
thecurrent
current
member is Colony Bagels
member is Colony Bagels
Ancestor(Product.CurrentMember,1).Name
Ancestor(Product.CurrentMember,1).Name
Returns
Returnsthe
thestring
string“Bread”
“Bread”ififthe
thecurrent
current
member is Bagels
member is Bagels
The Parent function works with only one level. A generalized version that can
Delivery Tip work at any level is the Ancestor function. By using the Ancestor function—
Because students have
already been exposed to the
coupled with the CurrentMember function—you can display the ancestor of
Ancestor function in the the current member, either at a relative position in the hierarchy, or at a specific
previous group activity, treat level of the hierarchy.
this topic as a review—that
is, do not spend a lot of
The Ancestor function has two types:
time. ! Ancestor at a specified level
! Ancestor at a specified distance in the hierarchy
Tip Putting the insertion point after one item in a pair of punctuation marks—
such as parentheses, brackets, or quotation marks—turns both items bold.
Omitting one of a pair of punctuation marks turns the remaining item red. These
signals help you properly match punctuation that must come in pairs.
This section introduces how to work with member properties associated with
Delivery Tip specific members.
Briefly explain the bullets on
this diamond slide to provide Two topics follow the group activity:
context for the upcoming
group activity in which the ! Properties Function
items are functionally
demonstrated. This topic describes how to retrieve the value from a member property by
using the Properties function. The Properties function returns a string
containing the value of the specified member property.
! Val Function
This topic describes how to convert a string to a number by using the Val
function. The Val function is a Microsoft Visual Basic for Applications
(VBA) function that is available from MDX.
In this group activity, you may follow along on your own computer with your
instructor or observe the demonstration.
You will learn how to use the Properties function to retrieve values stored as
member properties. Member properties are additional information available for
each member of a level. An administrator must use the Analysis Manager to
create a member property before you can use MDX to retrieve its value.
You will also learn how to use the Val function from the VBA library. The Val
function allows you to convert a string to a number.
3. Scroll to view the Property member, expand all the levels, and then view
the category manager name. Notice that the expression returns an error for
all levels other than the subcategory level.
Note If you use only the dimension name for the member argument of the
Ancestor function, the CurrentMember function is assumed.
3. Click OK, scroll to view the Property member, and then expand the levels
to show the Product Name level and see the manager name appear for all
the members in a subcategory.
3. To see the error message, click OK, and then close the message box.
The error message indicates that the expression is not valid. This is because
you cannot multiply a string by a number. You must convert the Price
member property value to a number before multiplying the values.
2. Click OK.
This version of the expression does not produce an error message box
because you can multiply two numbers.
3. In the browser, navigate down to the product level, and then view the prices.
The values are double the value stored in the dimension.
Properties Function
Topic Objective
To show how to display the
value of a member property
by using MDX. ! Returns a Member Property
Lead-in ! Requires Property Name in Quotation Marks
In addition to the name of a
member, you can show the ! Returns an Error If Property Name Does Not Exist
text string of a member’s
member property. ! Often Used with Ancestor Function to Get Correct Level
! Returns Property Value As a String Even If It Is a
Number
Product.CurrentMember.Properties(“Price”)
Product.CurrentMember.Properties(“Price”)
Returns
Returnsstring
stringvalue
valuefrom
fromthe
thePrice
Price
member
member property for currentmember
property for current member
The level of a dimension can have one or more member properties associated
with it. Each member can have a unique value for the member property. The
Properties function allows you to retrieve a member property value for a
member.
Syntax «Member».Properties(«String Expression»)
Example 1 The following example returns the string value of the Price member property
for the current member of the Product dimension.
Product.CurrentMember.Properties(“Price”)
Example 2 The following example returns the Category Manager member property for
the subcategory of the current member of the Product dimension.
Ancestor(Product, Subcategory).Properties(“Category Manager”)
When you use the Properties function, consider the following facts and
Delivery Tip guidelines:
Because students have
already been exposed to the ! You must enclose the name of the property in quotation marks because it is
Properties function in the the argument to the function.
previous group activity, treat
this topic as a review—that ! If the property name does not exist for a member, the function returns an
is, do not spend a lot of error value. Spaces in the name are significant, but capitalization is ignored.
time.
! The Ancestor function is a useful way to navigate from the current member
of a dimension to the level that contains a specified member property.
! The Properties function always returns a string value, even if the member
property appears to be a number.
! Because the Properties function returns a string value, it appears in the
String group of the Functions list.
Val Function
Topic Objective
To review the Val function.
Lead-in
MDX does not have an
! Converts a String to a Number
internal function for ! Is Useful for Converting a Member Property to a Number
converting a string to a
number ! Is a VBA Function, Not an MDX Function
! VBA and Excel Functions Are Automatically Available
Val(Product.CurrentMember.Properties(“Price”))
Val(Product.CurrentMember.Properties(“Price”))
Returns
Returnsnumeric
numericvalue
valuefrom
fromthe
thePrice
Price
member property for current member
member property for current member
When you use the Val function, consider the following facts and guidelines:
Delivery Tip
Because students have ! The function is useful for converting the value of a member property before
already seen the Name including it in an arithmetic expression.
function in the previous
group activity, use this topic ! The Val function is not an MDX function. It is part of the VBA library.
as a review—that is, do not ! Most VBA and Excel functions are automatically available to MDX
spend a lot of time.
expressions. You can search for function libraries in Microsoft SQL Server
Books Online to see which specific functions are excluded.
! Because the Val function is not an MDX function, it does not appear in the
Functions list in the Calculated Member Builder.
Related Functions VBA includes the following other functions that you may want to use in place
of the Val function:
Delivery Tip
These related functions are ! The CDbl function converts a string to a number but takes into
included for reference only. consideration the regional settings of the operating system.
Do not spend any time ! The CLng function converts a string to a number, rounding to the nearest
explaining them. integer.
You can find other Excel and VBA functions that you can use from MDX by
searching for function libraries in Microsoft SQL Server Books Online.
Objectives
Explain the lab objectives. After completing this lab, you will be able to:
! Create expressions by using functions that display current member, level,
and dimension information.
Prerequisites
Before working on this lab, you must have successfully completed modules 1
and 2 in course 2093A, Implementing Business Logic with MDX in Microsoft
SQL Server 2000.
Exercise 1
Displaying Member Information
In this exercise, you will create and then make modifications to a calculated
Delivery Tip member in the Basic Sales virtual cube on the Measures dimension, including
The procedures in this
exercise essentially
assigning the calculated member various expressions that display metadata
replicate the previous group information.
activities, but without the
As you complete each procedure, compare the result as shown in the procedure
answers.
screen shot to the result on your own computer. If there is a difference, recheck
Students having difficulty your entry and refer back to the related group activity procedures as necessary.
with the procedures should
If you still cannot reconcile your result, then refer to the answer file, which is
first refer back to the group
activity procedures and then
located in:
go to the answer file for C:\MOC\2093A\Labfiles\L03\Answers
guidance.
You can copy and paste expressions from this file into the Value expression
box for any given procedure.
3. Browse the cube, scrolling to view the Object Name member, and
navigating down to all levels of the Product dimension, and then, with the
Product Name level visible, verify that the browser content is similar to the
following table.
2. Browse the cube drilling down to all levels of the Product dimension, and
then, with the Product Name level visible, verify that the browser content
is similar to the following table.
Exercise 2
Displaying Family Tree Relatives
In this exercise, you will create and modify a calculated member on the
Delivery Tip Measures dimension, assigning it various expressions that display parent and
The procedures in this
exercise essentially
ancestor relationships.
replicate the previous group In this exercise, you will create a calculated member on the Measures
activities, but without the
dimension of the Basic Sales cube, assigning it various expressions that use
answers.
only constant values.
Students having difficulty As you complete each procedure, compare the result as shown in the procedure
with the procedures should
screen shot to the result on your own computer. If there is a difference, recheck
first refer back to the group
activity procedures and then
your entry and refer back to the related group activity procedures as necessary.
go to the answer file for If you still cannot reconcile your result, then refer to the answer file, which is
guidance.
located in:
C:\MOC\2093A\Labfiles\L03\Answers
You can copy and paste expressions from this file into the Value expression
box for any given procedure.
3. Browse the cube, drilling to all levels of the Product dimension. With the
Product Name level visible, verify that the browser content is similar to the
following.
2. Browse the cube, drilling to all levels of the Product dimension, and then,
with the first few products of the Dairy category visible, verify that the
browser content is similar to the following table.
2. Browse the cube, drilling to all levels of the Product dimension, and then,
with the Product Name level visible, verify that the browser content is
similar to the following table.
Exercise 3
Working with Member Properties
In this exercise, you will create and then make modifications to a calculated
Delivery Tip member in the Basic Sales virtual cube on the Measures dimension, including
The procedures in this
exercise essentially
assigning the calculated member various expressions that display metadata
replicate the previous group information from a cube.
activities, but without the
As you complete each procedure, compare the result as shown in the procedure
answers.
screen shot to the result on your own computer. If there is a difference, recheck
Students having difficulty your entry and refer back to the related group activity procedures as necessary.
with the procedures should
If you still cannot reconcile your result, then refer to the answer file, which is
first refer back to the group
activity procedures and then
located in:
go to the answer file for C:\MOC\2093A\Mod3\LabMetadata.
guidance.
You can copy and paste expressions from this file into the Value expression
box for any given procedure.
3. Browse the cube, drilling to all levels of the Product dimension, and then,
with the Product Name level visible, verify that the browser content is
similar to the following table—that is, you should see an error at any level
other than Subcategory.
____________________________________________________________
2. Browse the cube, drilling to all levels of the Product dimension, and then,
with the first few rows of the Dairy category visible, verify that the browser
content is similar to the following table.
2. Browse the cube, drilling to all levels of the Product dimension, and Then,
with the Product Name level visible, verify that the browser content is
similar to the following table.
This section introduces how to create MDX statements that use conditional
Delivery Tip expressions. Conditional logic is fundamental to sophisticated analysis and
Explain the bullets on this
diamond slide to provide
model building.
context for the upcoming The following topics follow the group activity:
group activity.
! Conditional Expressions
Each item in the bullets will
be functionally introduced in This topic describes how to create a conditional expression by using
the group activity and then comparison operators.
reviewed in more complete ! Multiple Conditions
syntax and other detail in
the slide topics that follow. This topic describes how to combine multiple conditions in a single
condition by using conditional operators.
! IIF Function
This topic describes how to use the IIF function to make one expression
return different values based on the value of a conditional expression.
In this group activity, you may follow along on your own computer with your
instructor or observe the demonstration
You will learn how to use basic conditional expressions. You start by first
constructing a simple conditional expression, and then you learn how to use the
IIF function. In the final step, you create an MDX expression that incorporates
multiple conditions.
The following are several comparison operators you can use to create a
conditional expression:
• Equal to (=)
• Not equal to (<>)
• Greater than (>)
• Less than (<)
• Greater than or equal to (>=)
• Less than or equal to (<=)
5. Click OK.
6. In the browser, look for the text strings. Notice that they say “Bring” only
for the products in the Bread category.
3. Click OK.
4. In the browser, scroll down to the first few rows of the Dairy category and
look for the string values in the Conditional column. Notice that they now
say “Bring” not only for the Bread category products but also for products
in the Cheese subcategory.
There are several keywords that you can use to combine conditional
expressions, for example:
• OR returns True if either of two conditional tests returns True.
• AND returns True only if both values are True.
• XOR returns True if one and only one value is True.
• NOT reverses the True and False results of the test.
Conditional Expressions
Topic Objective
To review conditional ! Return True or False
expressions.
$ True = 1
Lead-in
MDX allows you to make $ False = 0
comparison tests in an
expression. ! Created by Using a Comparison Operator
= Equal to <> Not Equal to
> Greater Than < Less Than
>= Greater Than or Equal to <= Less Than or Equal to
Product.CurrentMember.Name
Product.CurrentMember.Name == “Bread”
“Bread”
Returns
ReturnsTrue
Truefor
forBread
Breadand
andFalse
Falsefor
forall
all
other members
other members
MDX allows you to make comparison tests in an expression. For example, you
can test whether a product belongs in a specific category by testing the name of
the product’s ancestor. Consider some examples.
Example 1 The following example returns True (1) only if the current member of the
Product dimension is the Bread category.
Product.CurrentMember.Name = “Bread”
Example 2 The following example returns True (1) only if the current member of the
Product dimension is a descendant of the Bread category.
Ancestor(Product.CurrentMember,
Product.Category).Name = “Bread”
Multiple Conditions
Topic Objective
To review combining
! Use Operator to Combine Multiple Conditions
conditional expressions in
one. $ OR returns True if either condition is True
Lead-in
Some decisions require $ AND returns True if both conditions are true
more than one condition.
$ XOR returns True if only one condition is True
$ NOT returns True if condition is False
! Result Is a Conditional Expression
Ancestor(Product,Category).Name
Ancestor(Product,Category).Name == “Bread”
“Bread” OR
OR
Ancestor(Product,Subcategory).Name
Ancestor(Product,Subcategory).Name == “Cheese”
“Cheese”
Returns
ReturnsTrue
Truefor
forall
allBread
BreadororCheese
Cheese
products, False for all others
products, False for all others
A conditional test can be composed of more than one sub test. The conditional
expressions are combined with a logical operator.
Example The following example displays the text “Bring” for any product that is either
in the Bread category or in the Cheese subcategory.
IIF(Ancestor([Product].CurrentMember,
[Product].Category ).Name = "Bread" OR
Ancestor([Product].CurrentMember,
[Product].Subcategory ).Name = "Cheese" ,
"Bring" , "Don't Bring" ).
IIF Function
Topic Objective
To review the IIF function.
Lead-in
You often need to make a
! Returns One of Two Values Based on Conditional
decision in an expression. Expression
The IIF function allows you
to do that.
! Returns First Value If True, Second Value If False
! IIF Stands for Immediate IF—Similar to VBA IIF Function
or Excel IF Function
! Both Expressions Must Be Same Data Type
IIF(Ancestor(Product,
IIF(Ancestor(Product,Category).Name
Category).Name==“Bread”,
“Bread”,“Bring”,
“Bring”,“Don’t
“Don’tBring”)
Bring”)
Returns
ReturnsBring
Bringififcurrent
currentmember
memberisisaa
descendant
descendant of the Breadcategory
of the Bread category
Sometimes you need an MDX expression that calculates one value under one
condition and a different value under another condition. The IIF function
allows you to make a decision based on a conditional test. The MDX IIF
function is similar to the IF function in Excel or the IIF function in Visual
Basic.
Syntax IIF(«Conditional Expression»,«String Expression»,«String Expression»)
IIF(«Conditional Expression»,«Numeric Expression»,«Numeric Expression»)
Example The following example displays the text “Bring” or “Don’t Bring”, depending
on the category of the current product.
IIF(Ancestor([Product].CurrentMember,
[Product].Category ).Name = “Bread” ,
“Bring” , “Don’t Bring” )
When you use the IIF function, consider the following facts and guidelines:
Delivery Tip
Because students have ! The IIF function takes three arguments. The first argument is the
already seen the IIF function conditional expression. The final two arguments are the result values. If the
in the previous group conditional expression returns True, the function returns the first result
activity, use this topic as a
value. If the conditional expression returns False, the function returns the
review—that is, do not
spend a lot of time.
second result value.
! The name of the function—IIF—is an abbreviation for Immediate IF. This
function is similar to the Visual Basic IIF function and to the Excel IF
function.
! There are two versions of the IIF function. One returns a string; the other
returns a number. Both result values must be of the same data type.
• If the first result value is a string, the second result value must also be a
string.
• Likewise, if the first result value is a number, the second result value
must also be a number.
This is different from the IF function in Excel and the IIF function in VBA,
where the arguments can have different data types.
! The IIF function that returns a string value appears in the String group of
the Functions list. The IIF function that returns a number appears in the
Numeric group of the Functions list.
Objectives
Explain the lab objectives. After completing this lab, you will be able to:
! Create conditional expressions using various forms, including the IIF
function.
Prerequisites
Before working on this lab, you must have successfully completed modules 1
through 2 in course 2093A, Implementing Business Logic with MDX in
Microsoft SQL Server 2000.
Exercise 1
Using Conditional Expressions
In this exercise, you will create and then make modifications to a calculated
Delivery Tip member in the Basic Sales virtual cube on the Measures dimension, including
The procedures in this
exercise essentially
assigning the calculated member various expressions that display metadata
replicate the previous group information from a cube.
activities, but without the
As you complete each procedure, compare the result as shown in the procedure
answers.
screen shot to the result on your own computer. If there is a difference, recheck
Students having difficulty your entry and refer back to the related group activity procedures as necessary.
with the procedures should
If you still cannot reconcile your result, then refer to the answer files, which are
first refer back to the group
activity procedures and then
located in:
go to the answer file for C:\MOC\2093A\Labfiles\L03\Answers
guidance.
You can copy and paste expressions from this file into the Value expression
box for any given procedure.
3. Browse the cube, navigating to all levels of the Product dimension, and
then, with the first rows of the Dairy category visible, verify that the
browser content is similar to the following table.
____________________________________________________________
2. Browse the cube, displaying only the Category and Subcategory levels of
the Product dimension, and then, with the first few rows of the Dairy
category visible, verify that the browser content is similar to the following
table.
____________________________________________________________
2. Browse the cube, displaying only the Category and Subcategory levels of
the Product dimension, and then verify that the browser content is similar
to the following table.
Review
Topic Objective
To reinforce module
objectives by reviewing key ! Using MDX Expressions
points.
Lead-in ! Displaying Member Information
The review questions cover
some of the key concepts ! Displaying Family Tree Relatives
taught in the module.
! Working with Member Properties
! Using Conditional Expressions
1. You want to create an expression that displays empty cells. What value
would you put in the expression?
Null.
3. You want to create an expression that displays the name of each member
along a dimension. In addition to the Name function, what other function
must you include?
The CurrentMember function.
4. If you want to display the name of the grandparent of the current member,
what function would you use, and what would you have to specify in that
function?
The Ancestor function, with 2 as the numeric distance argument.