Set Functions: The AddCalculatedMembers() Function

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.

Overview

In
this lesson, we will introduce AddCalculatedMembers(), a basic, but
highly useful, set function in the MDX toolset. The general purpose of AddCalculatedMembers()
is to retrieve the base members of a specified set, together with
all sibling calculated members.

AddCalculatedMembers()
can be leveraged
in a wide range of activities, from the generation of simple lists to the
support of sophisticated conditional and other calculations and presentations.
We will introduce the function, commenting upon its operation and touching upon
creative effects that we can employ it to deliver. As a part of our
discussion, we will:

  • Examine the syntax surrounding the function;
  • Undertake illustrative examples of the uses of the function in
    practice exercises;
  • Briefly discuss the results datasets we obtain in the practice
    examples.

The AddCalculatedMembers() Function

Introduction

According to the Analysis Services
Books Online
, the AddCalculatedMembers() function “returns a set generated by adding calculated
members to a specified set.”
AddCalculatedMembers() has numerous applications. For example, the
function can be leveraged within queries to create datasets, in reporting
applications such as MSSQL Server Reporting Services, for the support of
picklists within the reports, for the support of axes within various end
presentations, and so forth. The AddCalculatedMembers()
function provides an intuitive
option anytime we need to present, in a returned dataset, all members
including calculated members – that belong to a specified set. (When
we employ a metadata-based MDX function to specify a set for retrieval,
the default behavior we would expect would be that only base members,
and specifically not calculated members, of the set are returned.

As we have noted to have been the case
with many individual MDX functions we have examined within this series,
combining AddCalculatedMembers()
with other functions allows us to further extend its power. We will get a
taste of this synergy in the practice exercises that follow.

We will examine the syntax for the AddCalculatedMembers()
function after a brief discussion in the next section. We will then explore,
from the straightforward context of MDX queries, and within practice examples
constructed to support hypothetical business needs, some of the uses it offers
the knowledgeable user. This will allow us to activate what we explore in the Discussion
and Syntax sections, where we will get some hands-on exposure in
creating expressions that employ the AddCalculatedMembers() function.

Discussion

To restate our initial explanation of its
operation, the AddCalculatedMembers() function examines a set expression that we specify
and returns its base members, along with sibling calculated members,
contained within the scope of that set expression. AddCalculatedMembers() can be used for a great deal more than simple list
retrieval, as we have intimated. When coupled with other functions or used
within MDX scripts, among other applications, we can leverage AddCalculatedMembers()
to support a wide range of analysis and reporting utility.

Let’s discuss syntax to further clarify the operation of AddCalculatedMembers().

Syntax

Syntactically, in using the AddCalculatedMembers()
function to return a set of members (including sibling calculated
members
), the set expression upon which we seek to apply the
function is specified within the parentheses to the right of the AddCalculatedMembers
keyword. The function takes the set expression (a valid MDX expression
that returns a set) enclosed within the parentheses, and returns a set
representing both the base members and the calculated members contained
within the scope of the set expression.

The general syntax for the
application of AddCalculatedMembers() appears in the following string:

 AddCalculatedMembers( <<Set_Expression>> )

Putting AddCalculatedMembers() to work is
straightforward. When using the function to return the base, and sibling
calculated, members contained within the set expression we
have provided, we simply supply the required set expression
within the parentheses to the right of the AddCalculatedMembers keyword.
As an example, say we specify, within a query executed against the sample Adventure
Works
cube, a column axis containing the Clothing Product Category,
with a row axis such as the following:

 ADDCALCULATEDMEMBERS({[Measures].[Internet Sales Amount]}) 
  ON AXIS (1) 

Moreover,
say that we add a WHERE clause to filter the retrieved data set to Calendar
Year 2004
. Depending upon the calculated members we have defined
within our cube (we might have added calculated members beyond those
that appear in the pristine sample cube), we would expect to retrieve results
similar to those depicted in Illustration 1.



Illustration 1: Example Returned Data:
AddCalculatedMember() Function Employed in Query

We can see, within the dataset
returned above, that, in addition to the single base measure, [Measures].[Internet
Sales]
, contained within the set we have specified above, we also see the calculated
members
appear that lie within the scope of the set. We can verify that
the appropriate calculated members are included by opening the Adventure
Works
cube within the Cube Designer, going to the Calculations
tab, and examining the calculated members that appear, as partially
shown in Illustration 2.



Illustration 2: Calculated Members within the Scope of Our
Specified Set Expression

(Partial View)

Because all calculated
members
that appear within the retrieved data set share the same parent, [Measures],
they are siblings of the base member, and are thus selected through our
use of AddCalculatedMembers().

Because of the relative ease
with which we can employ AddCalculatedMembers(), and because of the
flexibility with which we can exploit it to meet various business needs
(particularly those meeting metadata requirements), the function becomes a
popular member of our analysis and reporting toolsets. We will practice some
uses of the AddCalculatedMembers() function in the section that follows.

William Pearson
William Pearson
Bill has been working with computers since before becoming a "big eight" CPA, after which he carried his growing information systems knowledge into management accounting, internal auditing, and various capacities of controllership. Bill entered the world of databases and financial systems when he became a consultant for CODA-Financials, a U.K. - based software company that hired only CPA's as application consultants to implement and maintain its integrated financial database - one of the most conceptually powerful, even in his current assessment, to have emerged. At CODA Bill deployed financial databases and business intelligence systems for many global clients. Working with SQL Server, Oracle, Sybase and Informix, and focusing on MSSQL Server, Bill created Island Technologies Inc. in 1997, and has developed a large and diverse customer base over the years since. Bill's background as a CPA, Internal Auditor and Management Accountant enable him to provide value to clients as a liaison between Accounting / Finance and Information Services. Moreover, as a Certified Information Technology Professional (CITP) - a Certified Public Accountant recognized for his or her unique ability to provide business insight by leveraging knowledge of information relationships and supporting technologies - Bill offers his clients the CPA's perspective and ability to understand the complicated business implications and risks associated with technology. From this perspective, he helps them to effectively manage information while ensuring the data's reliability, security, accessibility and relevance. Bill has implemented enterprise business intelligence systems over the years for many Fortune 500 companies, focusing his practice (since the advent of MSSQL Server 2000) upon the integrated Microsoft business intelligence solution. He leverages his years of experience with other enterprise OLAP and reporting applications (Cognos, Business Objects, Crystal, and others) in regular conversions of these once-dominant applications to the Microsoft BI stack. Bill believes it is easier to teach technical skills to people with non-technical training than vice-versa, and he constantly seeks ways to graft new technology into the Accounting and Finance arenas. Bill was awarded Microsoft SQL Server MVP in 2009. Hobbies include advanced literature studies and occasional lectures, with recent concentration upon the works of William Faulkner, Henry James, Marcel Proust, James Joyce, Honoré de Balzac, and Charles Dickens. Other long-time interests have included the exploration of generative music sourced from database architecture.

Latest Articles