About the Series …
This article is a member of the series, MDX Essentials.
The series is designed to provide hands-on application of the fundamentals of
the Multidimensional Expressions (MDX) language, with each tutorial
progressively adding features designed to meet specific real-world needs.
For more information about the series in general, as well as
the software and systems requirements for getting the most out of the lessons
included, please see my first article, MDX
at First Glance: Introduction to MDX Essentials.
Note: Current updates are assumed for MSSQL
Server, MSSQL Server Analysis Services, and the related Books
Online and Samples.
Functions: IsGeneration(): Conditional Logic within Calculations, another article within my MDX Essentials series, we introduced the IsGeneration() function
from the perspective of its use within a calculation. We discussed the straightforward
purpose of this logical function: IsGeneration() provides us a means of
testing whether or not a specified member exists within a specified generation.
As a part of introducing the function, we noted that the effective use of IsGeneration()
as a testing mechanism is dependent upon a good understanding of the manner
with which generation numbers are assigned within Analysis Services. For
this reason, we overviewed the generation concepts, within our preliminary commentary
surrounding the function, in Logical Functions:
IsGeneration(): Conditional Logic within Calculations.
In this article, we will examine IsGeneration(), once again as a
conditional logic modifier, but within the context of a filter.
Combining IsGeneration() with the MDX Filter()
function is another way we commonly see it in action in the business
environment, and our exposure to the practical aspects of its employment in
this way will serve to enhance our overall awareness of the potential of IsGeneration(). From the
perspective of the use of the IsGeneration() function in combination with Filter(), this article will
- A review of the general syntax surrounding the function;
- Illustrative examples of uses of the function in practice
- A brief discussion of the MDX results obtained within each
of the practice examples.
The IsGeneration() Function
As we related in Logical Functions: IsGeneration(): Conditional Logic
within Calculations, the Books Online tell
us that the IsGeneration() function “returns true if the member
indicated … is in the generation specified….” Otherwise, the function returns
false. Also, if the member we specify evaluates to an empty member, the IsGeneration()
function returns false. A Boolean value of “True” is returned if the member
to which the function is applied resides within the generation number
specified by the Numeric Expression we provide, and “False” is returned
if it does not (or if the member expression we supply identifies an empty
member). In its capacity, as a logical function, to “test” the nature / status
of a member, IsGeneration() can be employed in conjunction with the IIF()
function to conditionally drive the return of data, such as a member or
members, or values, based upon the relationship between members as ancestor /
descendant. Moreover, as we shall see in
this article, IsGeneration() can be employed more directly within a filter
expression we place into the specification of an axis within an MDX query.
We will examine in detail the
syntax for the IsGeneration() function after our customary overview in
the Discussion section that follows. Following that, we will
conduct practice examples within a couple of scenarios constructed to support
hypothetical business needs that illustrate uses for the function. This will
afford us an opportunity to explore some of the basic options that IsGeneration()
can offer the knowledgeable user. Our current examination will focus upon
the use of IsGeneration() within the context of a filter.
Hands-on practice with IsGeneration(), where we will create queries that
employ the function, will help us to activate what we have learned in the Discussion
and Syntax sections.
For more detail surrounding the Filter() function, see Basic
Set Functions: The Filter() Function, a member of my Database Journal MDX Essentials
To restate our initial
description of its operation, IsGeneration() returns “True” if a specified Member Expression is the
number of “steps” specified (by the Numeric Expression) away from the leaf
level of the containing hierarchy. Alternatively, “False” is
returned if the specified Member Expression is not the specified
number of steps away, or if the Member Expression evaluates to an empty
member. We can use IsGeneration() to apply conditional logic
based upon the location and / or existence of members. As we have noted to be
the case with most MDX functions, pairing IsGeneration() with other MDX
functions can help us to leverage its power beyond what we might otherwise
achieve in attempts to use it in standalone fashion.
Let’s look at syntax specifics to further clarify the
operation of IsGeneration().
Let’s review the syntax
involved with employing the IsGeneration() function. We employ IsGeneration()
by specifying the Member Expression (the member which we are testing
as to “generation member status”) and the Numeric Expression (the generation
number in relation to which we are testing the Member Expression)
within parentheses to the immediate right of the function. The function takes
the Member Expression and Numeric Expression thus appended as its
arguments, and returns True if the member denoted by the Member
Expression exists within the specified generation number (Numeric
Expression) of the hierarchy within which it resides (or, in other words,
if the Member Expression lies the number of steps specified by the Numeric
Expression from the leaf level of the containing hierarchy).
If the member specified by the Member
Expression is evaluated as an empty member, or if the Member Expression
does not exist within the generation number specified by Numeric
Expression, then a False is returned, as we have noted.
The general syntax is shown in
the following string:
As we noted earlier, employing
IsGeneration() is as straightforward, in the mechanical sense, as
working with most of the MDX logical functions, assuming that we
have an adequate grasp of the meaning of generation, (a topic we
discussed in depth in Logical Functions:
IsGeneration(): Conditional Logic within Calculations). As we learned, we simply place the Member
Expression and Numeric Expression, respectively, in the parentheses
to the right of the function. As an example, within a query executed against
the sample Adventure Works cube, for the dimension named Date
(with a hierarchy of Calendar), the following pseudo-expression:
returns True for the current
member of the Date dimension / Calendar hierarchy for each of
- CY 2001
- CY 2002
- CY 2003
- CY 2004
Each of the listed members is a “resident” of generation
number 4 of the Date.Calendar hierarchy – which appears among the
other levels as annotated in Illustration 1.
Illustration 1: Generation Number 4 among the Other
Generations of the Hierarchy …
information on several of the “relative” functions, of which .CurrentMember
(used in the pseudo-expression above) is an example, see my article MDX
Member Functions: "Relative" Member Functions,
within the Database Journal MDX
We will practice some uses of the IsGeneration() function,
within the context of filtering, in the section that follows.