Group Activity: Using Set Creation Functions In this group activity, you may follow along on your own computer with your instructor or observe the demonstration You will learn how to gen
Trang 1Contents
Overview 1
Using Set Creation Functions 2
Lab A: Using Set Creation Functions 13
Using Set Manipulation Functions 20
Lab B: Using Set Manipulation Functions 30
Using Subquery Set Functions 36
Lab C: Using Subquery Set Functions 52
Working with Dimension Interactions 57
Lab D: Working with Dimension
Interactions 64
Review 67
Module 6: Creating and Manipulating Sets
Trang 2to 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
2000 Microsoft Corporation All rights reserved
Microsoft, BackOffice, MS-DOS, Windows, Windows NT, <plus other appropriate product
names or titles Replace this example list with list of trademarks provided by copy editor Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order > are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries
Content developed by OLAP Train, Inc for Microsoft Corporation and distributed under license 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
Trang 3Instructor Notes
The ability to work with sets is critical both to retrieving data from an online analytical processing (OLAP) cube in a multidimensional expression (MDX) query statement and creating powerful MDX expressions
In this module, students will be introduced to functions that return sets of members Some of the functions retrieve sets from the metadata defined in the cube's dimensions Some of the functions manipulate those sets to create new sets that meet specific analytical purposes
After completing this module, students will be able to:
! Use functions to create a set
! Use functions to manipulate a set
! Create a set from subqueries
! Manipulate dimension interactions by using advanced functions
Materials and Preparation
This section provides you with the required materials and preparation tasks that are needed to teach this module
Required Materials
To teach this module, you need the following materials:
! Microsoft® PowerPoint® file 2093A_6.ppt
Preparation Tasks
To prepare for this module, you should:
! Read all of the materials for this module
! Read the instructor notes and margin notes
! Practice the lecture and demonstrations
! Complete the labs
! Review the Teacher Preparation materials on the Teacher Preparation compact disc
Presentation:
95 Minutes
Labs:
75 Minutes
Trang 4Instructor Setup for Group Activities and Labs
This section provides setup instructions that are required to prepare the instructor computer or classroom configuration for group activities and labs All group activities and labs use the same database setup, which requires restoring a database archive
! To prepare for group activities and labs
In this procedure, you restore the Market database, which is a cab file type
1 Open Analysis Manager
2 In the left pane, expand the Microsoft Analysis Services folder
3 Expand the Server icon and verify that the Market database does not exist
4 Right-click the Server icon, and then click Restore Database
5 Navigate to the C:\Moc\2093A\Batches folder
6 Select Market.cab, click Open, and then click Restore
If the Market database already exists from a previous group exercise or lab,
and cubes within 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 preceding restore database procedure
Trang 5Other 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 Do the Members, Children, and Descendants function really create sets? These functions begin with something that is not a set—a dimension, a level, or a member—and return something that is a set In that sense, they create sets This is different from most other set functions, which begin with one or more sets and return a different set
2 How would you get all the leaf-level members of an entire dimension?
Use the Descendants function with the All Level member as the common ancestor
do, but there are exceptions As you learn a new function, you must remember its syntactic form
4 The optional flags for the Descendants function can be confusing Is there
an easy way to learn them?
In the first place, you can usually avoid using the flags of the Descendants function if you are willing to use combinations of other functions—particularly Union, Except, and Hierarchize If you do want
to use the Descendants flags, replace “before” with “above,” and
“after” with “below,” and read the arguments in the order 1, 3, 2 For
example, read the expression Descendants([1998],[Calendar
Quarter],BEFORE_AND_AFTER) as “the descendants of 1998 above
and below the calendar quarter level.”
5 The function definitions in the Syntax Examples list of the MDX2093A
application do not always match those in the Functions list of the Calculated Member Builder Which are correct?
The MDX2093A application is simply a learning tool It has many imperfections Always use the Calculated Member Builder or Books Online to find and learn unfamiliar functions
Trang 66 In both Books Online and the Functions list of the Calculated Member
Builder, the syntax for the Descendants – Level function shows that you
cannot use the third argument—Desc_flags—without also using the second argument—Level Is that true?
No You can omit the Level argument, leaving commas to show its place, and still use a flag such as LEAVES as the third argument The documentation is incorrect
to just combine the sets with braces If you want to strip duplicate members, you may want to use the Union function
8 Would you ever use the NON EMPTY keyword to create a calculated
member?
No It is used only to create a query statement that returns a report In
a calculated member, you can use the NonEmptyCrossjoin function to combine sets from multiple dimensions and remove tuples that produce
an empty result
Trang 7Module Strategy
Major sections of this module begin with or include a group activity followed
by review lecture and then labs The following are guidelines 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 functions 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 may also refer to answer files, which are available for each procedure in 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 group activities If necessary to facilitate your demonstration, copy and paste the correct expression from the answer file into the Calculated Member Builder
Trang 8Use the following additional strategies to present this module:
! Using Set Creation Functions Begin by explaining why it is important to be able to create a set of
members Next, describe the use of the Members function, explaining the syntax carefully Then introduce the Descendants function, describing how
it can be used to retrieve members from either a single level or from multiple levels Emphasize the fact that these functions are returning metadata about the cube, not data from the cube
! Using Set Manipulation Functions Explain why it is useful to be able to manipulate sets to create a new set that
contains specific members Introduce the Head, Tail, Union, Intersect, Except, and Hierarchize functions, describing the syntax for each and
giving examples of when to use each function
! Using Subquery Set Functions
Define the term subquery and describe when a subquery is used Emphasize
the difference between functions that look only at cube metadata and functions that use a subquery to retrieve actual data form the cube, although the function returns only a set of members and no actual data Introduce the
Order, TopCount, and Filter functions, describing the syntax for each and
explaining when to use each function
! Working with Dimension Interactions Explain that this section introduces more advanced processes that retrieve sets that combine members from two or more dimensions Then introduce
the Crossjoin function and the NON EMPTY keyword Describe the
syntax for each and explain when to use each
Trang 9Overview
! Using Set Creation Functions
! Using Set Manipulation Functions
! Using Subquery Set Functions
! Working with Dimension Interactions
The ability to work with sets is critical both to retrieving data from an online analytical processing (OLAP) cube in a multidimensional expression (MDX) query statement and creating powerful MDX expressions
In this module, you will be introduced to functions that return sets of members Some of the functions retrieve sets from the metadata defined in the cube's dimensions Some of the functions manipulate those sets to create new sets that meet specific analytical purposes
After completing this module, you will be able to:
! Use functions to create a set
! Use functions to manipulate a set
! Create a set from subqueries
! Manipulate dimension interactions by using advanced functions
In this module, you will learn
about the creation and
manipulation of sets
Trang 10# Using Set Creation Functions
! Group Activity: Using Set Creation Functions
! Members Function
! Children Function
! Single-Level Descendants Function
! Leaf-Level Descendants Function
In this section, you will learn the most useful functions for retrieving a set of members from the metadata of a cube These functions, when used alone, retrieve only sets of members from the cube metadata, not actual data from the cube These sets can appear as labels on the axes of a report, controlling which cube values appear
In later sections, you will learn how to manipulate these simple sets of members
The group activity introduces the use of two basic functions that generate sets—
the Members function and the Descendants function The following topics
follow the group activity:
! Members Function
This topic describes how to return members of specified levels and
dimensions by using the Members function
! Children Function This topic describes the Children function This function returns the
children of a specified member
! Single-Level Descendants Function This topic describes the Descendants function The simplest form of this
function returns a set of members at a specific level, lower in the hierarchy tree than the member and level names given in the query You can also use
the Descendants function to return all the members that descend from a
single member
! Leaf-Level Descendants Function
Topic Objective
To introduce functions that
retrieve a set of members
from the metadata of a
cube
Lead-in
In this section, you will learn
the most useful functions for
retrieving a set of members
from the metadata of a
cube
Delivery Tip
Briefly explain the bullets on
this diamond slide to provide
context for the upcoming
group activity in which the
items are functionally
demonstrated
Trang 11Group Activity: Using Set Creation Functions
In this group activity, you may follow along on your own computer with your instructor or observe the demonstration
You will learn how to generate sets by using the Members and Descendants
functions Once you understand these functions, you can easily learn other related functions
! To open the MDX2093A application and template file
In this procedure, you use the MDX2093A sample application to open templates you will use in the remainder of the group activity
1 In Microsoft® Windows® Explorer, navigate to C:\MOC\2093A\LabFiles,
double-click MDX2093A.exe, and then click OK to connect to the server
2 When asked to open a query file, navigate to the folder
C:\MOC\2093A\LabFiles\L06, and open the file SetCreate.mdx
3 In the DB list, select Market, and then in the Cube list, select Sales The
first template looks like the following text:
Members of a Dimension - SELECT
{[Sales Dollars], [Sales Units]} ON COLUMNS, «Set» ON ROWS
FROM Sales
Topic Objective
To learn how to create sets
by using the Members and
Descendants functions
Lead-in
In this group activity, you will
learn how to generate sets
of members
Delivery Tip
Explain initial template
before continuing with the
group activity
Trang 12! To show all the members of a dimension
In this procedure, you use the Members function to create a set that
consists of all members of all levels of a dimension
1 Click in the «Set» token of the Members of a Dimension query In the Syntax Examples list, expand the Set folder, and then double-click Members – Dimension
MDX includes three different Member functions: one each for Level, Dimension, and Hierarchy The Dimension and Hierarchy versions are
essentially identical: simply think of the dimension and hierarchy together
as the name of the dimension
2 Click in the «Dimension» token, and then in the Data list, double-click the Product dimension The resulting statement looks like this:
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, [Product].Members ON ROWS
FROM Sales
3 To view the results of the query, click the Run Query button
The labels on the Rows axis appear from all levels of the Product hierarchy The member names in a level are sorted according to the Order
By Column property defined in the dimension
The MDX2093A application displays members from multiple levels in a single column, but indented This differs from the browser in the Analysis Manager and the Microsoft Office PivotTable® list, which use a separate column for each level It is the client application—not the MDX statement—that determines how levels are displayed
! To show the children of a member
In this procedure, you use the Children function to create a set that consists of
members that are the children of a specified member
1 In the Queries list, select Children of a Member
2 Click in the «Set» token In the Syntax Examples list, expand the Set folder, and then double-click Children
The Children function must follow a single member Do not confuse the Children function with the Members function A member has children A
level or a dimension has members
Trang 133 Click in the «Member» token and then type [Bagels] The resulting
statement looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Bagels.Children ON ROWS
FROM Sales
4 To view the results of the query, click the Run Query button
The Rows axis contains the five members that have Bagels as a parent The Children function is analogous to the Parent function The Parent function goes up the tree, whereas the Children function goes down the tree The Parent function returns a member, whereas the Children function returns
a set That is because a member can have more than one child, but a member never has more than one parent
! To show single-level descendants of a member
In this procedure, you use the Descendants function to create a set of members
that are all descendants of one specified member
1 In the Queries list, select Descendants at a Level
2 Click in the «Set» token In the Syntax Examples list, expand the Set folder, and then double-click Descendants
3 Remove brackets around optional arguments, and then delete the third argument
The first argument is a member It is the common ancestor for all the members returned by the function
4 Click in the «Member» token and type [Bread]
The second argument is a level It is the level from which you want to retrieve members
5 Click in the «Level» token and type [Product Name] The resulting
statement looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Descendants([Bread],[Product Name]) ON ROWS FROM Sales
Trang 146 To view the results of the query, click the Run Query button
The set of members on the Rows axis comes from the Product Name level but includes only those products that share Bread as a common ancestor The Descendants function is analogous to the Ancestor function—that
is, in both cases you can specify a starting member and a level As with the
Ancestor function, you can specify the level for the Descendants function by
using either an explicit level name or a relative numeric value that gives the distance or number of levels to skip
! To show all the descendants of a member
In this procedure, you use the Descendants function to create a set of all the
members that are descendants of a specified member
1 In the Queries list, select All Descendants
2 Click in the «Set» token In the Syntax Examples list, expand the Set folder, and then double-click Descendants
3 Delete everything except the first argument
If you include only the common ancestor member in the Descendants
function, it returns all the descendants of that member at all remaining levels
of the hierarchy
4 Click in the «Member» token and type [Bread] The resulting statement
looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Descendants([Bread]) ON ROWS
Trang 15! To create a set of the leaf-level descendants of a member
In this procedure, you use the Descendants function to create a set of the
level members of a parent-child dimension In a parent-child dimension, level members—members that have no children—are not always on the same level
leaf-1 In the Queries list, select Leaf-Level Descendants
2 Click in the «Set» token In the Syntax Examples list, expand the Set folder, double-click Descendants, and then delete any brackets indicating
5 Click in the «Desc_flags» token, and then type LEAVES The resulting
statement looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Descendants(Employee, , LEAVES) ON ROWS FROM Sales
6 To view the results of the query, click the Run Query button
The set of members on the Rows axis consists of all the leaf-level members
of the Employee dimension, although those members occur on different
levels None of the summarized members appears in the set
Trang 16Members Function
! Returns a Specified Set of Members
! Retrieves All Members of a Level or Dimension
! Must Use Hierarchy Version If the Dimension Has Explicit Hierarchy
! The Members function works not only with the members of a level, but can
also be used to retrieve all the members of a dimension
When you have a large dimension, retrieving all the members of the dimension can create a very large set Use caution when you use the
Members function with a dimension
! In the Set group of the Functions list, the Members – Hierarchy function
appears This means that if a dimension has a hierarchy—for example, the
Topic Objective
To explain the syntax and
results of the Members
function
Lead-in
The Members function
returns members of specific
Because students have
already been exposed to the
Members function in the
previous group activity, treat
this topic as a review—that
is, do not spend a lot of
time
Caution
Trang 17MDX includes the following other functions that return sets similar to the
Members function:
! The AllMembers function returns all the members of a level or dimension,
including any calculated members
! The AddCalculatedMembers function adds any calculated members to a
These related functions are
included for reference only
Do not spend any time
explaining them
Trang 18Children Function
! Returns a Set of Children of a Member
! Is Similar to the Parent Function
$ Moves down the tree instead of up
$ Returns a set instead of a single member
! Is Easy to Confuse with Members Function
! Appears in the Set Group of the Syntax Examples List
[1998].Children
Returns the set of all quarters in 1998
The Children function returns members that have the specified member as a
• The Children function moves down the tree, whereas the Parent
function moves up
• The Children function returns a set, whereas the Parent function
returns a single member
! The Children function is easy to confuse with the Members function
because both return a set of members Following are the important differences:
• A level (or dimension) has Members
• A member has Children
! Because the Children function returns a set, it appears in the Set group of the Syntax Example list
MDX includes the following other function that returns sets similar to the
Children function:
Topic Objective
To explain the syntax and
results of the Children
function
Lead-in
The Children function
returns members that share
a common parent
Example
Delivery Tip
Because students have
already been exposed to the
Children function in the
previous group activity, treat
this topic as a review—that
is, do not spend a lot of
time
Ask why Children returns a
set whereas Parent returns
a member Because the
hierarchy, by definition, gets
larger as you move down
Related Functions
Trang 19Single-Level Descendants Function
! Returns a Set of Members at a Specific Level or Distance
! Is Similar to the Ancestor Function, but Moves Down
! Returns Descendants at All Levels If You Omit Level
! Appears in the Set Group of the Syntax Example List
The Ancestor function returns a single member at a specific level, higher in the
hierarchy tree The Descendants function, in turn, returns a set of members
Descendants(«Member»,[«Level»])
The following example returns the set of all months in 1998
Descendants([1998], [Calendar Month])
The following example returns all the descendants of Bread, at all levels of the
hierarchy
Descendants([Bread])
When you use the Descendants function, consider the following facts and
guidelines:
! The most common form of the Descendants function returns the members
of a level that are descendants of a specified member In place of a level, you can give a numeric value that specifies the distance—that is, the number
of levels between the specified member and the resulting members
! The Descendants function is analogous to the Ancestor function: you give
it a starting member and a level name or distance number The only
difference is that the Ancestor function moves up, and the Descendants
function moves down
! If you omit the level argument from the Descendants function, it returns all
the members that descend from the specified member, without regard to the level In effect, you get the subtree with the specified member as the top member
! Because the Descendants function returns a set of members, it appears in the Set group of the Syntax Example list
Topic Objective
To describe how the
Descendants function can
be used to return a set of
members at one level
Lead-in
The Descendants function
is similar to the Ancestor
Because students have
already been exposed to the
Descendants function in
the previous group activity,
treat this topic as a review—
that is, do not spend a lot of
time
Ask why Descendants
returns a set whereas
Ancestor returns a
member Because the
hierarchy, by definition, gets
larger as you move down
Trang 20Leaf-Level Descendants Function
! Optional Third Argument Adds Flags
! LEAVES Flag Returns Only Descendants at Leaf-Level
Descendants([Maya Gutierrez], , LEAVES))
Returns leaf-level descendants of Maya at any level
Returns leaf-level descendants of Maya at any level
The Descendants function can also return a set that consists only of leaf-level
members This is particularly useful with a parent-child dimension, where the leaf-level members may occur on different levels
To return a set that includes only leaf-level members, you use an optional third argument to specify the levels from which you want to return the descendants
Descendants(«Member», [«Level»],LEAVES)
The following example returns all the employees who report—directly or indirectly—to Maya Gutierrez
Descendants ([Maya Gutierrez], , LEAVES))
MDX includes the following other functions that return sets similar to the
Descendants function:
! The Descendants function has additional optional arguments that allow you
to return various subsets of the descendants of a member
! The Ancestors function returns the set of ancestors between a specified
member and a specified level—or the top of the tree—with one member from each level
You can find more information about these functions by searching on the function name in Microsoft SQL Server Books Online
Topic Objective
To describe how the
Descendants function can
be used to return the
leaf-level members under a
specified member
Lead-in
The Descendants function
can also be used to return a
set of members that are all
Because students have
already been exposed to the
Descendants function in
the previous group activity,
treat this topic as a review—
that is, do not spend a lot of
time
Delivery Tip
These related functions are
included for reference only
Do not spend any time
explaining them
Trang 21Lab A: Using Set Creation Functions
Objectives
After completing this lab, you will be able to:
! Create sets by using the Members and Descendants functions
Prerequisites
Before working on this lab, you must have successfully completed modules 1
through 5 in course 2093A, Implementing Business Logic with MDX in
In this lab, you will create
sets by using the Members
and Descendants
functions
Explain the lab objectives
Trang 22Exercise 1
Using Set Creation Functions
In this exercise, you will use functions that retrieve a set from a dimension—the
Members function and the Descendants function
As you complete each procedure, compare the result as shown in the procedure screen shot to the result on your own computer If there is a difference, recheck 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:
! The Market database
! The Sales cube
If these objects do not exist in Analysis Manager, you must perform the following procedure before you continue with this lab
! To restore the Market database
1 In the left pane of Analysis Manager, expand the Microsoft Analysis Services folder
2 Right-click the Server icon, and then click Restore Database
3 Navigate to the C:\Moc\2093A\Batches folder
4 Select Market.cab, click Open, and then click Restore
Delivery Tip
The procedures in this
exercise essentially
replicate the previous group
activities but without the
answers
Students having difficulty
with the procedures should
first refer back to the group
activity procedures and then
go to the answer file for
guidance
Trang 23! To show all the members of a dimension
In this procedure, you use the Members function to generate a report that includes all members of the Employee dimension MembersDimension.txt in
the Answer folder is the completed MDX expression used in this procedure
1 In Windows Explorer, navigate to C:\MOC\2093A\LabFiles, double-click
MDX2093A.exe, and then click OK to connect to the server
2 When asked to open a query file, navigate to the folder
C:\MOC\2093A\LabFiles\L06, and double-click the file LabA.mdx
1 In the DB list, select Market, and then in the Cube list, select Sales
2 Modify the initial MDX statement to create a report that shows Sales Dollars and Sales Units across the Columns axis and all the members of the Employee dimension down the Rows axis
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Employee.Members ON ROWS
FROM Sales
3 Click the Run Query button , and then verify that the Results pane, which is
located in the lower left corner of the MDX Builder, appears similar to the following table
Trang 24! To show the children of a member
In this procedure, you use the Children function to create a set that consists of
members that are the children of a specified member MemberChildren.txt in the Answer folder is the completed MDX expression used in this procedure
1 With the LabA.mdx file open in the MDX2093A sample application, in the
Queries list, select Children of a Member
2 Modify the initial MDX statement to create a report that shows Sales Dollars and Sales Units across the Columns axis and all the children of Meat down the Rows axis
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Meat.Children ON ROWS
FROM Sales
3 Run the query, and then verify that the Results pane appears similar to the following table
Trang 25! To show single-level descendants of a member
In this procedure, you use the Descendants function to generate a report that
displays a set of members from a single level that share a common ancestor MembersDescendantsLevel.txt in the Answer folder is the completed MDX expression used in this procedure
1 With the LabA.mdx file open in the MDX2093A sample application, in the
Queries list, click Descendants on a Level
2 Modify the initial MDX statement to create a report that shows Sales Dollars and Sales Units across the Columns axis and all the Meat products
down the Rows axis
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Descendants([Meat],[Product Name]) ON ROWS FROM Sales
3 Run the query, and then verify that the first few rows of the Results pane appear similar to the following table
Trang 26! To show all the descendants of a member
In this procedure, you use the Descendants function to generate a report that
displays all the descendants of a common ancestor from multiple levels
MembersDescendantsAll.txt in the Answer folder is the completed MDX expression used in this procedure
1 With the LabA.mdx file open in the MDX2093A sample application, in the
Queries list, select All Descendants
2 Modify the initial MDX statement to create a report that shows Sales Dollars and Sales Units across the Columns axis and all the descendants of USA down the Rows axis
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Descendants([USA]) ON ROWS
FROM Sales
3 Run the query, and then verify that the Results pane appears similar to the following table
Trang 27! To show all the leaf-level descendants of a member
In this procedure, you use the Descendants function to generate a report that
displays all the leaf-level descendants of a common ancestor
MembersDescendantsLeaves.txt in the Answer folder is the completed MDX expression used in this procedure
1 With the LabA.mdx file open in the MDX2093A sample application, in the
Queries list, select Leaf-Level Descendants
2 Modify the initial MDX statement to create a report that shows Sales Dollars and Sales Units across the Columns axis and all the leaf level descendants of Maya Gutierrez down the Rows axis
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Descendants([Maya Gutierrez], , LEAVES) ON ROWS FROM Sales
3 Run the query, and then verify that the Results pane appears similar to the following table
Trang 28# Using Set Manipulation Functions
! Group Activity: Using Set Manipulation Functions
! Head and Tail Functions
After you have returned a set of members—whether by using braces with
individual members or by using a function such as Members or Descendants—you can then modify the set
The group activity introduces several functions you can use to manipulate sets The following topics follow the group activity:
! Head and Tail Functions
This topic describes how to extract the specified number of members from
the beginning or end of the original set by using the Head and Tail
functions
! Union Function This topic describes how to combine two sets by using the Union function
The members in the sets must come from the same dimension, but they can
be from any level
! Other Set Combination Functions This topic describes how to use other functions that return a new set based
on two original sets These functions are:
• The Intersect function, which creates a set containing only those
members that are in both source sets
• The Except function, which creates a set containing only those members
that are in the first set but are not in the second set
! Hierarchize Function
This topic describes how to sort any set in the hierarchy order by using the
Hierarchize function
Topic Objective
To introduce functions that
enable modification of a set
of members
Lead-in
After you have a set of
members, you can then
modify the set to contain
only specific members
Delivery Tip
Briefly explain the bullets on
this diamond slide to provide
context for the upcoming
group activity in which the
items are functionally
demonstrated
Trang 29Group Activity: Using Set Manipulation Functions
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 functions that take existing sets—created by using
either set creation functions, such as Members and Descendants, or explicit
members enclosed in braces—and modify them to create new sets
! To display the first members of a set
In this procedure, you learn how to use the Head function to extract the first
members from the beginning of a set
1 In the MDX2093A application, click the Open Query File button, navigate
to the C:\MOC\2093A\Labfiles\L06 folder, and then double-click
SetManipulate.mdx
2 In the Queries list, click First Members of a Set The initial query looks
like this:
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Subcategory.Members ON ROWS
FROM Sales
If you run this query, the Rows axis displays all 12 subcategories You can
remove all but the first three by using the Head function
3 Change definition of the set on the Rows axis to Head(Subcategory.Members,3) The final query looks like this:
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Head(Subcategory.Members,3) ON ROWS
FROM Sales
Topic Objective
To learn how to manipulate
sets by using functions
Lead-in
In this group activity, you will
learn how to manipulate
existing sets to create new
Trang 304 Run the query
The Rows axis displays only the first 3 subcategories
! To display the last member of a set
In this procedure, you use the Tail function to extract the one final member
from the end of a set
1 In the Queries list, select Last Member of a Set
2 Change the definition of the set on the Rows axis to Tail(Subcategory.Members,1). The final query looks like this:
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Tail(Subcategory.Members,1) ON ROWS
FROM Sales
3 Run the query
The Rows axis displays only the last subcategory—Hot Dogs This is a
single member, but it is still a set
! To create the union of two sets
In this procedure, you use the Union function to combine two sets
1 In the Queries list, select Union of Two Sets
2 Click in the «Set» token on the Rows axis, and then in the Set folder of the Syntax Examples list, double-click the Union function
The Union function requires two sets and includes an optional third argument
3 Delete the optional third argument, along with the comma and the brackets
4 Click in the first «Set» token, and then enter [Calendar Year].Members This expression returns a set of all the members of the Calendar Year
Trang 315 Click in the second «Set» token, and then enter [Calendar Month].Members This expression returns a set of all the members of the Calendar Month level The resulting statement looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Union([Calendar Year].Members, [Calendar Month].Members)
ON ROWS FROM Sales
6 To view the results of the query, click the Run Query button
The Rows axis now displays the years and also the months The values are
from different levels but appear as part of the same set The Union function
does not rearrange the members in any way; it simply returns all the members of the first set followed by all the members of the second set
If you omit the optional third argument, with the value ALL, the Union
function discards duplicate members from the result set If you do include the ALL argument, duplicate members are retained in the result
In order to combine two sets by using the Union function, the two
sets must be from the same dimension
! To arrange members of a set in hierarchy order
In this procedure, you use the Hierarchize function to sort a set into hierarchy
order
1 In the Queries list, select Hierarchize This initial query matches the final
query of the preceding procedure
2 Click before the Union function, and then in the Set group of the Syntax Examples list, double-click Hierarchize
3 Delete the «Set» token and the closing parenthesis, and then add a closing parenthesis after the closing parenthesis of the Union function The final
statement looks like this:
SELECT {[Sales Dollars],[Sales Units]} ON COLUMNS, Hierarchize(Union([Calendar Year].Members, [Calendar Month].Members)) ON ROWS
FROM Sales
Point out the order of the
members Ask the students
if this is the way that a
report should appear
Trang 324 Click the Run Query button
The Rows axis now displays the years interleaved in the proper order The
Hierarchize function simply wraps around any set, sorting the set into the
hierarchy order defined in the dimension
! To remove specific members from a set
In this procedure, you use the Except function to remove specific members
from a set This is equivalent to what you do in the PivotTable list browser when you deselect individual members from a non-measures dimension
The Except function returns a set, based on two original sets, that contains
members that are in the first set but not the second This gives the effect of removing the members of the second set from the first set
1 In the Queries list, select Remove Members from a Set
The original set expression on the Rows axis is Subcategory.Members That set displays all 12 subcategory members You can remove explicit
members—for example, Milk and Cheese—by using the Except function
2 Before the original expression, type Except(
3 After the original expression, type , {Milk, Cheese}) The final statement
looks like the following:
SELECT {[Sales Dollars], [Sales Units]} ON COLUMNS, Except(Subcategory.Members, {Milk, Cheese}) ON ROWS FROM Sales
Trang 33The second argument of the Except function—the members you exclude—
must be a set Thus, if you use one or more explicit member names, they must
be enclosed in braces You can also use a set function as the second argument
For example, the expression Except([Time].[Calendar].Members,[Calendar Quarter].Members) returns a set of all the members of the Time.Calendar
dimension except the quarters
Trang 34Head and Tail Functions
Returns the last four members
of the Calendar Month level
Returns the last four members
of the Calendar Month level
Returns the first four members
of the Calendar Month level
Returns the first four members
of the Calendar Month level
The Head and Tail functions return the specified number of members from the
beginning or end of the original set
These functions do not sort or rearrange the starting set in any way
Even if you extract a single member from the set, the result is still a set The
Head and Tail functions always generate sets
Head Function
The Head function returns members from the beginning of a set
Head («Set», «Number of Members»)
To return the first four members of the Months level, use the following query:Head({[Fiscal Period].[Months].Members}, 4)
Tail Function
The Tail function returns members from the end of a set
Tail («Set», «Number of Members»)
To return the last four members of the Months level, use the following query:Tail({[Fiscal Period].[Months].Members}, 4)
MDX includes the following other function that returns sets similar to the Head and Tail functions:
! The Subset function returns a specified range of members from a specified
Topic Objective
To describe Head and Tail
functions
Lead-in
The Head and Tail functions
extract a specified number
of members from either the
beginning or the end of a
set
Delivery Tip
Because students have
already been exposed to the
Head and Tail functions in
the previous group activity,
treat this topic as a review—
that is, do not spend a lot of
Trang 35Union Function
! Returns a Set That Is the Combination of Two Sets
! Default Discards Duplicates; Optional ALL Retains Duplicates
! Equivalent to Enclosing Both Sets in Braces
! Appears in the Set Group of the Syntax Examples List
Union([Calendar Year].Members, [Calendar Quarter].Members)
Union([Calendar Year].Members, [Calendar Quarter].Members)
Returns years and quarters, with years preceding quarters
Returns years and quarters, with years preceding quarters
The Union function combines two sets into one The members in the sets must
come from the same dimension—but they can be from any level
Union («Set1», «Set2» [, ALL])
The following example returns Years and Quarters
Union([Calendar Year].Members,[Calendar Quarter] Members)
When you use the Union function, consider the following facts and guidelines:
! As a default, the Union function discards any members that occur more than
once in the combined set If you want to retain all members from the
original sets, use the optional All keyword
! Rather than explicitly use the Union function, you can include the sets,
separated by commas, in a pair of braces, because braces around sets simply combine the sets
When you use braces to combine the sets—whether with a plus sign or with commas—duplicate members are not discarded In order to discard
duplicate members, you must use the Union function
! Because the Union function returns a set, it appears in the Set group of the Syntax Example list
Topic Objective
To describe the Union
function
Lead-in
The Union function allows
you to combine any two sets
from the same dimension
into one set
Syntax
Example
Delivery Tip
Because students have
already been exposed to the
Union function in the
previous group activity, treat
this topic as a review—that
is, do not spend a lot of
time
Trang 36Other Set Combination Functions
KY IN MI IL
NY MA CT NJ
Set2
KY IN MI IL
NY MA CT NJ
CA OR WA AZ
KY IN MI IL
NY MA CT NJ
In addition to the Union function, you can use the Intersect and Except
functions to return a new set based on two original sets In all three cases, the two original sets must contain members from the same dimension
The following are definitions of the three functions:
! The Union function creates a set containing all members from the two
source sets
! The Intersect function creates a set containing only those members that are
in both source sets
! The Except function creates a set containing only those members that are in
the first set, but are not in the second set
You can create the union of two sets—equivalent to using the Union
function—by enclosing both sets in a pair of braces separated by a plus sign
(+) You can also subtract one set from another—equivalent to the Except
function—by enclosing both sets in a pair of braces separated by a minus sign (-)
Topic Objective
To introduce the Intersect
and Except functions
Lead-in
The Intersect and Except
functions also return a
single set of members
based on two original sets
from the same dimension
Delivery Tip
Because students have
already been exposed to the
Intersect and Except
functions in the previous
group activity, treat this topic
as a review—that is, do not
spend a lot of time
Note
Trang 37Hierarchize Function
! Returns Set Arranged in Hierarchy Order
! Wraps Around Existing Set Expression
! Appears in the Set Group of the Syntax Examples List
Hierarchize(Union([Calendar Year].Members, [Calendar Quarter].Members)))
Hierarchize(Union([Calendar Year].Members, [Calendar Quarter].Members)))
Returns Years and Quarters in hierarchy order
Returns Years and Quarters in hierarchy order
Combining two sets—particularly by using the Union function—often results
in a set that is not arranged in hierarchy order You can use the Hierarchize
function to sort any set in the hierarchy order
Hierarchize(«Set»)
To return Years and Quarters in the hierarchical order defined in the dimension, use the following expression
Hierarchize(Union([Calendar Year].Members, [Calendar Quarter].Members) )
When you use the Hierarchize function, consider the following facts and
guidelines:
! You use the Hierarchize function by wrapping—that is, enclosing the
function around—an existing set expression
! Because the Hierarchize function returns a set, it appears in the Set group
of the Syntax Examples list
MDX includes the following other function that adjusts a set similar to the
Hierarchize function:
! The Distinct function removes all duplicate members from a set
You can find more information about this function by searching on the function name in Microsoft SQL Server Books Online
Topic Objective
To describe the Hierarchize
function
Lead-in
The Hierarchize function
allows you to sort any set
into the hierarchy order
Syntax
Example
Delivery Tip
Because students have
already been exposed to the
Hierarchize function in the
previous group activity, treat
this topic as a review—that
is, do not spend a lot of
time
Related Functions
Delivery Tip
These related functions are
included for reference only
Do not spend any time
explaining them
Trang 38Lab B: Using Set Manipulation Functions
Objectives
After completing this lab, you will be able to:
! Use functions to create new sets of members from previously existing sets
of members
Prerequisites
Before working on this lab, you must have successfully completed modules 1
through 5 in course 2093A, Implementing Business Logic with MDX in
In this lab, you will
manipulate existing sets of
members to create new sets
by using set manipulation
functions
Explain the lab objectives
Trang 39Exercise 1
Using Functions to Manipulate Sets
In this exercise, you will manipulate sets to create new sets
As you complete each procedure, compare the result as shown in the procedure screen shot to the result on your own computer If there is a difference, recheck 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\L02B1\Answers
You can copy and paste expressions from this file into the query pane for any given procedure
! To display the first members of a set
In this procedure, you manipulate a set to create a new set containing the first
four members from the beginning of an original set by using the Head function
ManipulateHead.txt in the Answer folder is the completed MDX expression used in this procedure
1 In Windows Explorer, navigate to C:\MOC\2093A\LabFiles, double-click
MDX2093A.exe, and then click OK to connect to the server
2 When asked to open a query file, navigate to the folder
C:\MOC\2093A\LabFiles\L06 and open the file LabB.mdx
3 In the DB list, select Market, and then in the Cube list, select Sales
4 Modify the set on the Rows axis to create a report that shows the first four
members of Product Name
What MDX query statement did you use?
SELECT {[Sales Dollars], [Sales Units]}
Head([Product Name].Members, 4) ON ROWS FROM Sales
5 Run the query, and then verify that the Results pane appears similar to the following table
Delivery Tip
The procedures in this
exercise essentially
replicate the previous group
activity but without the
answers