Logical Functions: IsAncestor(): Conditional Logic within Filter Expressions
April 2, 2007
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.
In IsAncestor(): Conditional Logic within Calculations, another article within my MDX Essentials series, we introduced the IsAncestor() function from the perspective of its use within a calculation. We discussed the straightforward purpose of this logical function, to return whether or not a specified member is an ancestor of another member we specify (By ancestor, of course, we mean a member from which the specified member is descended within a dimensional hierarchy.) We discussed the manner in which IsAncestor() manages to do this, and ways we can leverage the operator to support effective conditional logic to meet various business needs within our own environments.
In this article, we will examine IsAncestor(), once again as a conditional logic modifier, but within the context of a filter. Combining IsAncestor() 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 round out our overall awareness of the potential of IsAncestor(). From the perspective of the use of the IsAncestor() function in combination with Filter(), this article will include:
The IsAncestor() Function
As we related in IsAncestor(): Conditional Logic within Calculations, the Books Online tell us that the IsAncestor() function returns whether a specified member is an ancestor of another specified member. A Boolean value of True is returned if the member expression to which the function is applied is an ancestor of the second specified member; otherwise IsAncestor() returns False. In its capacity, as a logical function, to test the nature / status of a member, IsAncestor() is often 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.
We will examine in detail the syntax for the IsAncestor() 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 the basic options that IsAncestor() can offer the knowledgeable user. Our current examination will focus upon the use of IsAncestor() within the context of a filter. Hands-on practice with IsAncestor(), where we will create queries that employ the function, will help us to activate what we have learned in the Discussion and Syntax sections.
NOTE: For more detail surrounding the Filter() function, see Basic Set Functions: The Filter() Function, a member of my Database Journal MDX Essentials series.
To restate our initial description of its operation, IsAncestor() returns True if a specified member expression represents an ancestor of another member (that is, lies between the secondary member and the top / All dimensional level) that we specify within a given use of the function; otherwise, the function returns False. We can use IsAncestor() 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 IsAncestor() with other MDX functions can help us to leverage its power much further than we might do in an attempt to use it in standalone fashion.
Lets look at syntax specifics to further clarify the operation of IsAncestor() .
To review the syntax involved with employing the IsAncestor() function, we specify the primary member expression (the member which we are testing as to ancestor status) and the secondary member expression (the member in relation to which we are testing the primary member expression) within parentheses to the immediate right of the function. The function takes the member expressions thus appended to it as its arguments, and returns True if the member denoted by the primary member expression is an ancestor of the secondary member expression (or, in other words, if the primary member lies somewhere between the secondary member and the top of the dimensional hierarchy).
If the member specified by the primary member expression is not an ancestor of the secondary member (or if the primary member and the secondary member belong to different dimensions) a False is returned, as we might expect.
The general syntax is shown in the following string:
Employing IsAncestor(), like most of the MDX logical functions, is, in the mechanical sense, straightforward. As we have noted, we simply place the primary and secondary member expressions, respectively, within the parentheses to the right of the function. To use the example from our syntax discussion in IsAncestor(): Conditional Logic within Calculations, within a query executed against the sample Adventure Works cube, for the dimension named Geography (with a hierarchy of the same name), the following pseudo-expression,
IsAncestor([Geography].[Geography].[State-Province].[South Australia], [Geography].[Geography].CurrentMember)
returns True for the current member of the Geography dimension / Geography hierarchy for each of the following:
Each of the listed members is a descendant of South Australia in the cube, as shown in Illustration 1.
Depending upon the structure of the query (and specifically upon whether the syntax defining axes, etc., eliminates nulls), if members of other dimensions, or members of levels higher than South Australia within the Geography hierarchy, were returned in, say, the row axis of the dataset, their values would be null.
NOTE: For information on several of the relative functions, of which .CurrentMember is an example, see my article MDX Member Functions: "Relative" Member Functions, within the Database Journal MDX Essentials series.
We will practice some uses of the IsAncestor() function in the section that follows.