0% found this document useful (0 votes)
187 views

MDX Query

The Dimensions MDX function returns a hierarchy specified by a numeric or string expression. It takes either a numeric expression representing the hierarchy number or a string expression representing the hierarchy name. The document provides the syntax and an example using the Dimensions function to return properties of the "Product Model Lines" hierarchy, such as its name, number of levels, and number of members.

Uploaded by

Sachitra Khatua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

MDX Query

The Dimensions MDX function returns a hierarchy specified by a numeric or string expression. It takes either a numeric expression representing the hierarchy number or a string expression representing the hierarchy name. The document provides the syntax and an example using the Dimensions function to return properties of the "Product Model Lines" hierarchy, such as its name, number of levels, and number of members.

Uploaded by

Sachitra Khatua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

{ MDX } Query

Category Archives: Hierarchy Functions

Dimensions MDX function

Function Name : Dimensions


Category : Hierarchy
Description : Returns a hierarchy specified by a numeric or string expression.
Syntax : Numeric expression syntax :-
Dimensions ( Hierarchy_Number )

String expression syntax :-


Dimensions ( Hierarchy_Name )
Technet Link : MDX Dimensions function

Example :-

The following example returns the Name, Number of levels and Number of Members for the
Product Model Lines hierarchy in the Product dimension in the AdventureWorks cube.

WITH
Member [Measures].[Dimension Name]
As Dimensions( ' [Product].[Product Model Lines] ' ).Name

Member [Measures].[Levels Count]


As Dimensions( ' [Product].[Product Model Lines] ' ).Levels.Count

Member [Measures].[Members Count]


As Dimensions( ' [Product].[Product Model Lines] ' ).Members.Count

Member [Measures].[First Dimension Name]


As Dimensions( 0 ).Name

Member [Measures].[First Dimension Levels Count]


As Dimensions( 0 ).Levels.Count

Member [Measures].[First Dimension Members Count]


As Dimensions( 0 ).Members.Count

SELECT {
[Measures].[Dimension Name],
[Measures].[Levels Count],
[Measures].[Members Count],
[Measures].[First Dimension Name],
[Measures].[First Dimension Levels Count],
[Measures].[First Dimension Members Count]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

Here, as you can see in the above query, we have used the Dimensions function with the
Hierarchy expression Product Model Lines along with 4 functions, Name, Count, Levels and
Members function.

Dimensions MDX function

Dimension MDX function

Function Name : Dimension


Category : Hierarchy
Description : Returns the hierarchy that contains a specified member, level, or hierarchy.
Syntax : Hierarchy syntax :-
Hierarchy_expression.Dimension

Level syntax :-
Level_Expression.Dimension

Member syntax :-
Member_Expression.Dimension
Technet Link : MDX Dimension function

Example :-
The following example returns the Name, Number of levels and Number of Members for the
Product Model Lines hierarchy in the Product dimension in the AdventureWorks cube.

WITH
Member [Measures].[Hierarchy Name]
AS [Product].[Product Model Lines].[Model].&[HL Road Tire].Dimension.Name

Member [Measures].[Levels Count]


AS [Product].[Product Model Lines].[Model].&[HL Road Tire].Dimension.Levels.Count

Member [Measures].[Members Count]


AS [Product].[Product Model Lines].[Model].&[HL Road Tire].Dimension.Members.Count

SELECT {
[Measures].[Hierarchy Name],
[Measures].[Levels Count],
[Measures].[Members Count]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

Here, as you can see in the above query, we have used the Dimension function with the Member
expression for HL Road Tire in the Product Model Lines Hierarchy at the Level Model (
i.e., [Product].[Product Model Lines].[Model].&[HL Road Tire] ) along with 3 functions, Name,
Count and Levels.
Here, it can be said that,
[Product].[Product Model Lines].Members = [Product].[Product Model
Lines].[Model].&[HL Road Tire].Dimension.Members

Dimension MDX function

Hierarchy MDX function

Function Name : Hierarchy


Category : Hierarchy
Description : Returns the hierarchy that contains a specified member or level.
Syntax : Member expression syntax :-
Member_Expression.Hierarchy

Level expression syntax :-


Level_Expression.Hierarchy
Technet Link : MDX Hierarchy function

Example :-

The following example returns all the members of the Fiscal hierarchy in the Date dimension in
the AdventureWorks cube.

SELECT {
[Measures].[Internet Sales Amount]
} ON COLUMNS,
[Date].[Fiscal].[Fiscal Year].&[2006].Hierarchy.Members ON ROWS
FROM
[Adventure Works]

Output :-

Here, as you can see in the above query, we have used the Member expression for year 2006
with the Hierarchy function to go to the Fiscal Hierarchy and Members function to get all
members of the Fiscal Hierarchy in the Date dimension.
So, you can say that,
[Date].[Fiscal].Members = [Date].[Fiscal].[Fiscal Year].&[2006].Hierarchy.Members
Hierarchy MDX function

Category Archives: Level Functions

Levels MDX function

November 14, 2012 12:32 am / 2 Comments / admin


Function Levels
Name :
Category : Level
Description : Returns the level whose position in a dimension or hierarchy is specified by a
numeric expression or whose name is specified by a string expression.
Syntax : Numeric expression syntax :-
Hierarchy_Expression.Levels ( Level_Number )

String expression syntax :-


Hierarchy_Expression.Levels ( Level_Name )
Technet Link MDX Levels function
:

Example :-

Both of the following examples return the Country level :

SELECT {
[Geography].[Geography].Levels( 1 )
} ON COLUMNS,
FROM
[Adventure Works]

SELECT {
[Geography].[Geography].Levels( 'Country' )
} ON COLUMNS,
FROM
[Adventure Works]

Output :-

Levels MDX function

Level MDX function

Function Name : Level


Category : Level
Description : Returns the level of a member.
Syntax : Member_Expression.Level
Technet Link : MDX Level function

Example :-

The following example uses the Level function to return all Fiscal years in the Adventure Works
cube.

SELECT {
[Measures].[Internet Sales Amount]
} ON COLUMNS,
[Date].[Fiscal].[Fiscal Year].&[2006].Level.Members ON ROWS
FROM
[Adventure Works]

Output :-

Here, as you can see in the above query, we have used the year 2006 with the Level function to
go to the Year Level and Members function to get all members at the Year level.

Level MDX function

Category Archives: Logical Functions

IsSibling MDX function

November 13, 2012 1:44 pm / Leave a Comment / admin

Function Name IsSibling


:

Category : Logical

Description : Returns whether a specified member is a sibling of another specified


member.

Syntax : IsSibling ( Member_Expression1 , Member_Expression2 )

Technet Link : MDX IsSibling function

Example :-
The IsSibling function returns true if the first specified member is a sibling of the second
specified member. Otherwise, the function returns false.

WITH
Member [Measures].[IsSibling]
As IsSibling ( [Date].[Fiscal].CURRENTMEMBER , [Date].[Fiscal].[Month].&[2005]&[7]
)

SELECT {
[Measures].[IsSibling]
} ON COLUMNS,
[Date].[Fiscal].[Month].Members ON ROWS
FROM
[Adventure Works]

Output :-

Here, as you can see, the Months July 2005, August 2005 and September 2005 are siblings
because they belong to the same parent i.e., 1st Quarter of Year 2006 [Q1 FY 2006]. That is why
the IsSibling function gives True as output.

IsSibling MDX function

IsLeaf MDX function

Function Name : IsLeaf

Category : Logical

Description : Returns whether a specified member is a leaf member.


Syntax : IsLeaf ( Member_Expression )

Technet Link : MDX IsLeaf function

Example :-

The IsLeaf function returns true if the specified member is a leaf member. Otherwise, the
function returns false.

WITH
Member [Measures].[IsLeaf]
As IsLeaf ( [Date].[Fiscal].CURRENTMEMBER )

SELECT {
[Measures].[IsLeaf]
} ON COLUMNS,
[Date].[Fiscal].Members ON ROWS
FROM
[Adventure Works]

Output :-

IsLeaf MDX function


Posted in: Logical functions / Tagged: IsLeaf, IsLeaf MDX, Logical MDX function, MDX,
MDX function, MDX query, OLAP, SQL Server, SSAS, SSRS

IsGeneration MDX function

November 13, 2012 1:04 pm / Leave a Comment / admin

Function Name : IsGeneration

Category : Logical

Description : Returns whether a specified member is in a specified generation.

Syntax : IsGeneration ( Member_Expression , Generation_Number )

Technet Link : MDX IsGeneration Function

Example :-

The IsGeneration function returns true if the specified member is in the specified generation
number. Otherwise, the function returns false. Also, if the specified member evaluates to an
empty member, the IsGeneration function returns false.

WITH
Member [Measures].[IsGeneration]
As IsGeneration ( [Date].[Fiscal].CURRENTMEMBER , 0 )

SELECT {
[Measures].[IsGeneration]
} ON COLUMNS,
[Date].[Fiscal].Members ON ROWS
FROM
[Adventure Works]

Output :-
IsGeneration MDX function

How is the Generation number defined ?

For the purposes of generation indexing, leaf members are generation index 0. The generation
index of nonleaf members is determined by first getting the highest generation index from the
union of all child members for the specified member, then adding 1 to that index. Because of
how the generation index of nonleaf members is determined, a specific nonleaf member could
belong to more than one generation.

An Example :-

Generation Number
Posted in: Logical functions / Tagged: Analysis services, IsGeneration MDX function, Logical
MDX function, MDX, MDX query, Multi dimensional expression, OLAP, SQL Server, SSAS,
SSRS

IsEmpty MDX function

November 11, 2012 3:01 pm / Leave a Comment / admin


Function Name : IsEmpty

Category : Logical

Description : Returns whether the evaluated expression is the empty cell value.

Syntax : IsEmpty ( Value_Expression )

Technet Link : MDX IsEmpty Function

Example :-

The IsEmpty function returns true if the evaluated expression is an empty cell value. Otherwise,
this function returns false.

WITH
Member [Measures].[Jan 2008 Sales]
As Sum ( [Date].[Fiscal].[Month].&[2008]&[1] , [Measures].[Internet Sales Amount] )

Member [Measures].[Jan 2008 Sales - IsEmpty]


As IsEmpty ( [Measures].[Jan 2008 Sales] )

SELECT {
[Measures].[Jan 2008 Sales],
[Measures].[Jan 2008 Sales - IsEmpty]
} ON COLUMNS,
[Product].[Product Categories].[Category] ON ROWS
FROM
[Adventure Works]

Output :-
IsEmpty MDX function

Practical Application :-

The IsEmpty function is used to avoid DIVIDE BY ZERO ( 1.#INF ) errors.


In the below MDX query, we the get the Sales for a month , [ January 2008 ] , and calculate its
contribution to the Total Year Sales , [ 2008 ] using the formula :

100 * ( [Sales in January 2008 ] / [ Sales in 2008 ] )

WITH
SET Selected_Month
AS StrToMember ( " [Date].[Fiscal].[Month].&[2008]&[1] " )

Member [Measures].[Month Sales]


AS Sum ( Selected_Month.item(0) , [Measures].[Internet Sales Amount] )

Member [Measures].[Year Sales]


AS Sum ( Ancestor ( Selected_Month.item(0) , [Date].[Fiscal].[Fiscal Year] ) ,
[Measures].[Internet Sales Amount] )

Member [Measures].[% of Year]


AS IIF ( IsEmpty ( [Measures].[Year Sales] ) , NULL , ( [Measures].[Month Sales] /
[Measures].[Year Sales] ) )
, FORMAT_STRING = " 0.00 % "

SELECT {
[Measures].[Month Sales],
[Measures].[Year Sales],
[Measures].[% of Year]
} ON COLUMNS ,
{ [Product].[Product Categories].[Category] } ON ROWS
FROM
[Adventure Works]

Output :-

Divide by ZERO errors (From Different Query)

Avoid divide by ZERO errors with IsEmpty MDX function


Posted in: Logical functions / Tagged: Analysis services, IsEmpty MDX function, Logical MDX
function, MDX, MDX query, Multi dimensional expression, SQL Server, SSAS, SSRS

IsAncestor MDX function

November 11, 2012 11:45 am / 2 Comments / admin

Function Name IsAncestor


:

Category : Logical

Description : Returns whether a specified member is an ancestor of another specified


member.

Syntax : IsAncestor ( Member_Expression1 , Member_Expression2 )


Technet Link : MDX IsAncestor Function

Example :-

The IsAncestor function returns true if the first member specified is an ancestor of the second
member specified. Otherwise, the function returns false.
In the below MDX query, we have used the month of January in year 2007 i.e., [ January 2007 ]
whose ancestor is the 3rd Quarter of 2007 i.e., [ Q3 FY 2007 ]

WITH
Member [Measures].[Is_Ancestor]
AS IsAncestor ( [Date].[Fiscal].CurrentMember , [Date].[Fiscal].[Month].&[2007]&[1] )

SELECT {
[Measures].[Is_Ancestor]
} ON COLUMNS,
Descendants ( [Date].[Fiscal].[Fiscal Year].&[2007] , [Date].[Fiscal].[Fiscal Quarter] ) ON
ROWS
FROM
[Adventure Works]

Output :-

IsAncestor MDX function

Category Archives: Member Functions

CurrentMember MDX function

April 20, 2013 11:27 am / Leave a Comment / admin


Function Name : CurrentMember
Category : Member
Description : Returns the current member along a specified hierarchy during iteration.
Syntax : Hierarchy_Expression.CurrentMember
Technet Link : MDX CurrentMember Function

Practical Example :-

WITH
MEMBER [Measures].[CurrMemID]
AS [Product].[Product Line].CurrentMember.UniqueName

MEMBER [Measures].[ProductLine Custom]


AS IIF ( ISEMPTY ( [Product].[Product Line].CurrentMember.MEMBER_KEY ) ,
[Product].[Product Line].CurrentMember.NAME,
[Product].[Product Line].CurrentMember.MEMBER_KEY + " - " + [Product].[Product
Line].CurrentMember.NAME )

SELECT {
[Measures].[ProductLine Custom],
[Measures].[Measures].[CurrMemID]
} ON COLUMNS,
[Product].[Product Line].Members ON ROWS
FROM [Adventure Works]

Output :-

CurrentMember Mdx function


Posted in: Member functions / Tagged: CurrentMember, MDX, MDX function, MDX member
function, Multi dimensional expression, OLAP, SQL Server, SSAS, SSRS

LastChild MDX function


April 20, 2013 11:26 am / 1 Comment / admin
Function Name : LastChild
Category : Member
Description : Returns the last child of a specified member.
Syntax : Member_Expression.LastChild
Technet Link : MDX LastChild Function

Practical Example :-

WITH
Member [Measures].[LAST CHILD]
As [Date].[Fiscal].[Fiscal Quarter].&[2008]&[2].LastChild.MEMBER_CAPTION

Member [Measures].[CLOSING PERIOD (Month)]


As CLOSINGPERIOD ( [Date].[Fiscal].[Month] , [Date].[Fiscal].[Fiscal
Quarter].&[2008]&[2] ).MEMBER_CAPTION

Member [Measures].[CLOSING PERIOD (Day)]


As CLOSINGPERIOD ( [Date].[Fiscal].[Date] , [Date].[Fiscal].[Fiscal
Quarter].&[2008]&[2] ).MEMBER_CAPTION

Member [Measures].[TAIL]
As TAIL ( [Date].[Fiscal].[Fiscal Quarter].&[2008]&[2].CHILDREN, 1 ).ITEM ( 0
).MEMBER_CAPTION

SELECT {
[Measures].[LAST CHILD],
[Measures].[CLOSING PERIOD (Month)],
[Measures].[CLOSING PERIOD (Day)],
[Measures].[TAIL]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

LastChild Mdx function


Difference between LastChild, ClosingPeriod and Tail functions ?

So, LastChild function has a lot of alternatives, which to use ?


As you can see in the output of the above query, all three functions give the same result.
But in what situation are they uniquely applicable and what is preference over another based on
performance ?
Situation 1:
If you need to get Last Non Empty child of a Member then forget about LastChild and
ClosingPeriod functions. Here, only Tail function (along with Children and item function)
can be used.

TAIL (
NonEmpty (
[Date].[Fiscal].[Fiscal Quarter].&[2008]&[2].CHILDREN ,
[Measures].[Internet Sales Amount] ) , 1 ).ITEM ( 0 ).MEMBER_CAPTION

Situation 2:
ClosingPeriod function is designed specifically for Time dimension but can be used with other
dimension also without getting error. But ClosingPeriod has more use than LastChild function as
it can get the Last Child of any level specified, that is, not just Father-Child relationship but
Grandfather-Child also. And if you need to get the Last Child of another Member right above it,
that is, find only Child of a given Father then any of the two functions can be used.

Posted in: Member functions / Tagged: LastChild, MDX, MDX function, MDX member
function, Multi dimensional expression, OLAP, SQL Server, SSAS, SSRS

Parent MDX function

April 20, 2013 11:24 am / Leave a Comment / admin


Function Name : Parent
Category : Member
Description : Returns the Parent of a specified member.
Syntax : Member_Expression.Parent
Technet Link : MDX Parent Function

Practical Example :-

WITH
MEMBER [Measures].[Parent]
As [Date].[Fiscal].[Month].&[2008]&[8].Parent.MEMBER_CAPTION

Member [Measures].[Current]
As [Date].[Fiscal].[Month].&[2008]&[8].MEMBER_CAPTION

SELECT {
[Measures].[Parent],
[Measures].[Current]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

Parent MDX function

PrevMember MDX function

Function Name : PrevMember


Category : Member
Description : Returns the previous member in the level that contains a specified
member.
Syntax : Member_Expression.PrevMember
Technet Link : MDX PrevMember Function

Simple Example :-

WITH
MEMBER [Measures].[Previous]
As [Date].[Fiscal].[Month].&[2008]&[8].PrevMember.MEMBER_CAPTION

Member [Measures].[Current]
As [Date].[Fiscal].[Month].&[2008]&[8].MEMBER_CAPTION

SELECT {
[Measures].[Previous],
[Measures].[Current]
} ON COLUMNS
FROM
[Adventure Works]
Output :-

PrevMember Mdx function

Practical Example :-

The most important use of PrevMember function comes when calculating WTD, MTD, QTD or
YTD where
WTD means Week to Date,
MTD means Month to Date,
QTD means Quarter to Date,
YTD means Year to Date.

Suppose you have to calculate the YTD of 12th June 2008 in Fiscal Calendar.

Here, the easy way is to use the PeriodsToDate MDX function which will take all the dates from
the start of the year to the given date, that is, the set will consist of 347 days from 1st July 2007
to 12th June 2008.
But this query will give the worst performance, as it calculates the measure value for each day
and sums it up for 347 times.
So here we divide it into two parts and sum them later. What is required is we take the ancestor
of the given date at one level up ( month level ), that is, December 2008. Take previous member
of December 2008, that is, November 2008 and take its PeriodsToDate from Jan 2008 to Nov
2008. This makes 1st Part.

Now the 2nd Part consists of days from 1st June 2008 to 12th June 2008.

So the calculation is now reduced from 347 times to 23 times ( 11 for 1st part + 12 for 2nd part ).
Here the most important thing to check before using this technique to improve
performance is :
WHETHER the Previous Member doesnt come in the previous year. For example, the previous
month for the date 9th July 2007 (In FY 2008) will be June 2007 which comes in Fiscal Year
2007, which is wrong.

LinkMember MDX function

Function Name LinkMember


:
Category : Member
Description : Returns the member equivalent to a specified member in a specified
hierarchy.
Syntax : LinkMember ( Member_Expression , Hierarchy_Expression )
Technet Link : MDX LinkMember Function

Practical Example :-

WITH
MEMBER [Measures].[Financial Report 1]
AS SUM ( [Ship Date].[Date].&[20080401] , [Measures].[Amount] )

MEMBER [Measures].[Financial Report 2]


AS SUM ( LinkMember ( [Ship Date].[Date].&[20080401] , [Date].[Date] ) ,
[Measures].[Amount] )

MEMBER [Measures].[Financial Report 3]


AS SUM ( [Date].[Date].&[20080401] , [Measures].[Amount] )

SELECT {
[Measures].[Financial Report 1],
[Measures].[Financial Report 2],
[Measures].[Financial Report 3]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

LinkMember MDX function

Here, the calculated measures [Financial Report 1], [Financial Report 2] and [Financial Report 3]
are based on [Measures].[Amount] which exists in measure group Financial Reporting.
The calculated measure, [Financial Report 1] gives null for 1st April 2008, while measures
[Financial Report 2] and [Financial Report 3] returns the value of $226,070.00. Now, why is that
?

This is because, the measure group Financial Reporting is related only to Date dimension
and not to Ship Date Dimension. So to get the value of [Measures].[Amount] ( in measure
group Financial Reporting ) for a Date in Ship Date Dimension we need to go through Date
dimension. Its like, when you need to know a girl whom you like, you approach the person who
is friends with both of you.

Dimension Usage in Adventure Works cube

The above can be seen in BIDS by following the below steps :-


1) Open BIDS. Go to Start > Microsoft SQL SERVER 2005/2008/2008 R2 > SQL Server
Business Intelligence Development Studio (BIDS).
2) File > Open > Analysis Services Database.
3) In Database section, write down the server and database which you want to explore.
For Ex. The sample OLAP cube database i used is Adventureworks.
4) Write down the solution path where you would like the solution to be created.
5) Click OK.
6) On the right hand side, click on Solution explorer. And then double click on the Adventure
Works cube in the Cubes folder.
7) Go to the Dimension Usage Tab to view the relation between Measures and Dimensions.

Posted in: Member functions / Tagged: LinkMember, MDX, MDX function, MDX member
function, Multi dimensional expression, OLAP, SQL Server, SSAS, SSRS

OpeningPeriod MDX function

February 17, 2013 9:12 pm / Leave a Comment / admin


Function OpeningPeriod
Name :
Category : Member
Description : Returns the member that is the first sibling among the descendants of a
specified member at a specified level.
Syntax : OpeningPeriod ( Level_Expression , Member_Expression )
Technet Link : MDX OpeningPeriod Function
Example :-

SELECT
OpeningPeriod ( [Date].[Fiscal].[Month] , [Date].[Fiscal].[Fiscal Year].&[2007] ) ON
COLUMNS
FROM
[Adventure Works]

The above example returns the value for the default measure for July, 2006 member, which is the
first sibling of the descendant of the year 2007 member at the month level in the Fiscal user-
defined hierarchy.
Here, what happens is, the MDX engine searches for first sibling of year 2007 on month level,
that is July 2006. And now it searches for Default measure in the measures dimension, which is
[Reseller Sales Amount]. Now what it does is makes a tuple like ( [July 2006] , [Reseller Sales
Amount] ) to get the value.
So, above query and following query are one and the same, producing same results.

WITH
MEMBER [Measures].[July 06 Default measure]
AS Sum ( OpeningPeriod ( [Date].[Fiscal].[Month] , [Date].[Fiscal].[Fiscal Year].&[2007] )
,
[Measures].[Reseller Sales Amount] )

SELECT
[Measures].[July 06 Default measure] ON COLUMNS
FROM
[Adventure Works]

Output :-

OpeningPeriod mdx function

Note :- For practical application you can see the ClosingPeriod function.

ClosingPeriod MDX function

Function ClosingPeriod
Name :
Category : Member
Description : Returns the member that is the last sibling among the descendants of a
specified member at a specified level.
Syntax : ClosingPeriod ( Level_Expression , Member_Expression )
Technet Link : MDX ClosingPeriod Function

Example :-

SELECT
ClosingPeriod ( [Date].[Fiscal].[Month] , [Date].[Fiscal].[Fiscal Year].&[2007] ) ON
COLUMNS
FROM
[Adventure Works]

The above example returns the value for the default measure for June, 2007 member, which is
the last sibling of the descendant of the 2007 member at the year level in the Fiscal user-defined
hierarchy.
Here, what happens is, the MDX engine searches for last sibling of year 2007 on month level,
that is June 2007. And now it searches for Default measure in the measures dimension, which is
[Reseller Sales Amount]. Now what it does is makes a tuple like ( [June 2007] , [Reseller Sales
Amount] ) to get the value.
So, above query and following query are one and the same, producing same results.

WITH
MEMBER [Measures].[June 07 Default measure]
AS Sum ( ClosingPeriod ( [Date].[Fiscal].[Month] , [Date].[Fiscal].[Fiscal Year].&[2007] ) ,
[Measures].[Reseller Sales Amount] )

SELECT
[Measures].[June 07 Default measure] ON COLUMNS
FROM
[Adventure Works]

Output :-

ClosingPeriod MDX function

Practical application :-
Suppose you need to show the Closing Date and Opening Date of a Fiscal week parameter in the
header of an SSRS report. In this case, OpeningPeriod and ClosingPeriod MDX functions can be
used as below:

WITH
MEMBER [Measures].[Opening Date]
AS OpeningPeriod ( [Date].[Fiscal].[Date] , [Date].[Fiscal].[Month].&[2008]&[7] ).name

MEMBER [Measures].[Closing Date]


AS ClosingPeriod ( [Date].[Fiscal].[Date] , [Date].[Fiscal].[Month].&[2008]&[7] ).name

SELECT{
[Measures].[Opening Date],
[Measures].[Closing Date]
} ON COLUMNS
FROM
[Adventure Works]

Output :-

ClosingPeriod MDX function


Posted in: Member functions / Tagged: ClosingPeriod, MDX, MDX function, MDX member
function, OpeningPeriod, SSRS

ParallelPeriod MDX function

November 10, 2012 11:36 pm / 2 Comments / admin


Function Name ParallelPeriod
:
Category : Member
Description : Returns a member from a prior period in the same relative position as a
specified member.
Syntax : ParallelPeriod ( [ Level_Expression [ , Index [ , Member_Expression ] ] ] )
Technet Link : MDX ParallelPeriod Function

Example :-
The ParallelPeriod function takes the ancestor of the specified member at the specified level,
finds the ancestors sibling with the specified lag, and finally returns the parallel period of the
specified member among the descendants of the sibling.

SELECT {
[Measures].[Internet Sales Amount]
} ON COLUMNS,
ParallelPeriod ( [Date].[Fiscal].[Fiscal Semester] ,
3,
[Date].[Fiscal].[Month].[October 2007] ) ON ROWS
FROM
[Adventure Works]

Output :-

ParallelPeriod MDX function

How does ParallelPeriod function work ?

In the above example, following explanation can help you in visualizing how the ParallelPeriod
MDX function works :-

ParallelPeriod Explanation
Practical Application :-

Suppose in a SSRS report you created a parameter called Fiscal Month. And you need to get the
sales amount for the selected month, say [ July 2008 ] , as well as same month last year , [ July
2007 ] , and check the % Change in sales between the two using the formula :

100 * ( [ Sales in July 2008 ] [ Sales in July 2007 ] ) / [ Sales in July 2007 ]

The ParallelPeriod function can be used to get the data for comparison purposes. It is used
mostly with Time dimension.

WITH
SET Selected_Month
AS StrToMember ( "[Date].[Fiscal].[Month].[July 2008]" )

Member [Measures].[Sales Current Period]


AS SUM ( Selected_Month.item(0) , [Measures].[Internet Sales Amount] )

Member [Measures].[Sales Previous Period]


AS SUM ( ParallelPeriod ( [Date].[Fiscal].[Fiscal Year] , 1 , Selected_Month.item(0) ) ,
[Measures].[Internet Sales Amount] )

Member [Measures].[% Change]


AS IIF ( [Measures].[Sales Previous Period] IS NULL , NULL ,
( [Measures].[Sales Current Period] - [Measures].[Sales Previous Period] ) /
[Measures].[Sales Previous Period] )
, FORMAT_STRING = " 0.00 % "

SELECT {
[Measures].[Sales Current Period],
[Measures].[Sales Previous Period],
[Measures].[% Change]
} ON COLUMNS ,
{ [Product].[Product Categories].[Category] } ON ROWS
FROM
[Adventure Works]

Output :-
ParallelPeriod MDX function Practical Usage

Ancestor MDX function

Function Ancestor
Name :
Category : Member
Description : The Ancestor function returns the ancestor of a specified member at a
specified level or at a specified distance from the member.
Syntax : Level Syntax :- Ancestor(Member_Expression, Level_Expression)
Numeric Syntax :- Ancestor(Member_Expression, Distance)
Technet Link MDX Ancestor Function
:

Example :-

SELECT { [Measures].[Customer Count] } ON COLUMNS,


Ancestor( [Sales Territory].[Sales Territory].[Country].&[United Kingdom],
[Sales Territory].[Sales Territory].[Group] ) ON ROWS
FROM
[Adventure Works]

Output :-

MDX Ancestor function

Practical application :-

Suppose you need to design a SSRS (SQL Server Reporting services) Customers count by
Country & Continent report using OLAP Cube data. And here the report has a single-value
parameter called Country to show the number of customers only for the selected country and
respective continent.
In this case, following MDX query should be used:

WITH
Member [Measures].[Country Count Customers]
AS [Measures].[Customer Count]

Member [Measures].[Continent Count Customers (distance)]


AS Sum(Ancestor([Sales Territory].[Sales Territory].CURRENTMEMBER, 1),
[Measures].[Customer Count])

Member [Measures].[Continent Count Customers (level)]


AS Sum(Ancestor([Sales Territory].[Sales Territory].CURRENTMEMBER, [Sales
Territory].[Sales Territory].[Group]), [Measures].[Customer Count])

SELECT
{
[Measures].[Customer Count],
[Measures].[Continent Count Customers (distance)],
[Measures].[Continent Count Customers (level)] } ON COLUMNS,
{ [Sales Territory].[Sales Territory].[Country].&[United Kingdom] } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Ancestor function practical

StrToMember MDX function

October 23, 2012 12:10 am / 2 Comments / admin


Function Name : StrToMember
Category : Member
Description : The StrToMember function returns a member for the supplied string.
Syntax : STRTOMEMBER( String Expression )
Technet Link : MDX StrToMember Function

Example :-

SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOMEMBER('[Customer].[Customer Geography].[Country].&[Australia]') ON ROWS
FROM
[Adventure Works]

Output :-

MDX StrToMember function

Practical application :-

Suppose you need to design a SSRS (SQL Server Reporting services) Customers count by
Country report using OLAP Cube data. And this report has a single-value parameter called
Country(For multi-value parameter please refer StrToSet function) to show the number of
customers only for the selected country.

In this case, following MDX query should be used:

SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOMEMBER(@Country) ON ROWS
FROM
[Adventure Works]

Note:- StrToSet() function can also be used for single-value parameter instead of StrToMember,
because a set can have 0,1 or more values.

Category Archives: Numeric Functions

Avg MDX function

October 29, 2012 11:28 pm / Leave a Comment / admin

Function Avg
Name :

Category : Numeric

Description : Evaluates a set and returns the average of the non empty values of the cells in
the set, averaged over the measures in the set or over a specified measure.
Syntax : Avg ( Set_Expression [ , Numeric_Expression ] )

Technet Link MDX Avg Function


:

Example :-

Here we are calculating the Average of the Day Sales in the Month of April, 2008.
That is, taking the total of the Internet Sales during the month of April, 2008 and dividing it by
total number of days in that month excluding the days when no Sales were made ( like days
when the store was closed ).

WITH
Member [Measures].[Internet Sales - Month (Sum)]
AS Sum ( Descendants ( [Date].[Fiscal].[Month].&[2008]&[4], [Date].[Fiscal].[Date] ) ,
[Measures].[Internet Sales Amount] )

Member [Measures].[Count NonEmpty Days in Month]


AS Count ( NonEmpty ( { Descendants ( [Date].[Fiscal].[Month].&[2008]&[4],
[Date].[Fiscal].[Date] ) } , [Measures].[Internet Sales Amount] ) )

Member [Measures].[Internet Sales - Month (Avg)]


AS Avg ( Descendants ( [Date].[Fiscal].[Month].&[2008]&[4], [Date].[Fiscal].[Date] ) ,
[Measures].[Internet Sales Amount] )

SELECT {
[Measures].[Internet Sales - Month (Sum)],
[Measures].[Count NonEmpty Days in Month],
[Measures].[Internet Sales - Month (Avg)]
} ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

You can see here how the Avg function works. The value of $1,608,750.53, when divided by 30,
gives $53,625.02
MDX Avg function

Practical Application :-

Suppose you need to design an SSRS (SQL Server reporting services) report which has a
parameter called Fiscal Date . And you have to show the Average of MTD ( Month-to-Date )
and YTD ( Year-to-Date ) calculation of Internet Sales Amount measure for all the Product
Categories.

That is, you need to show following data on the report:


1) Average of the Sales on the Selected Date ( 30/04/2008 )
2) Average of the Sales during MTD period ( 01/04/2008 30/04/2008 )
3) Average of the Sales during YTD period ( 01/01/2008 30/04/2008 )

WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )

Member [Measures].[Internet Sales - Day (AVG)]


AS Avg ( Day , [Measures].[Internet Sales Amount] )

Member [Measures].[Internet Sales - MTD (AVG)]


AS Avg ( PeriodsToDate ( [Date].[Fiscal].[Month], Day.item(0) ), [Measures].[Internet
Sales Amount] )

Member [Measures].[Internet Sales - YTD (AVG)]


AS Avg ( PeriodsToDate ( [Date].[Fiscal].[Fiscal Year], Day.item(0) ), [Measures].[Internet
Sales Amount] )

SELECT {
[Measures].[Internet Sales - Day (AVG)],
[Measures].[Internet Sales - MTD (AVG)],
[Measures].[Internet Sales - YTD (AVG)]
} ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Avg function Practical

Sum MDX function

Function Name : Sum

Category : Numeric

Description : Returns the sum of a numeric expression evaluated over a specified set.

Syntax : Sum ( Set_Expression [ , Numeric_Expression ] )

Technet Link : MDX Sum Function

Example :-

WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )

Member [Measures].[Internet Sales Amount - Day]


AS Sum ( Day , [Measures].[Internet Sales Amount] )

SELECT
{ [Measures].[Internet Sales Amount - Day] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Sum function

Practical Application :-

Suppose you need to design an SSRS (SQL Server reporting services) report which has a
parameter called Fiscal Date . And you have to show the MTD ( Month-to-Date ) and YTD (
Year-to-Date ) calculation of Internet Sales Amount measure for all the Product Categories.

WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )

Member [Measures].[Internet Sales Amount - Day]


AS Sum ( Day , [Measures].[Internet Sales Amount] )

Member [Measures].[Internet Sales Amount - MTD]


AS Sum ( PeriodsToDate ( [Date].[Fiscal].[Month], Day.item(0) ), [Measures].[Internet
Sales Amount] )

Member [Measures].[Internet Sales Amount - YTD]


AS Sum ( PeriodsToDate ( [Date].[Fiscal].[Fiscal Year], Day.item(0) ), [Measures].[Internet
Sales Amount] )

SELECT {
[Measures].[Internet Sales Amount - Day],
[Measures].[Internet Sales Amount - MTD],
[Measures].[Internet Sales Amount - YTD]
} ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Sum function practical

Note :- To know the difference between Sum and Aggregate MDX function. Please see
Aggregate MDX function.

Aggregate MDX function

Function Aggregate
Name :

Category : Numeric

Description Returns a number that is calculated by aggregating over the cells returned by
: the set expression. If a numeric expression is not provided, this function
aggregates each measure within the current query context by using the default
aggregation operator that is specified for each measure. If a numeric
expression is provided, this function first evaluates, and then sums, the
numeric expression for each cell in the specified set.

Syntax : Aggregate ( Set_Expression [ , Numeric_Expression ] )

Technet MDX Aggregate Function


Link :

Example :-

WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )

Member [Measures].[Customer Count - Day (Agg)]


AS Aggregate ( Day , [Measures].[Customer Count] )

SELECT
{ [Measures].[Customer Count - Day (Agg)] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Aggregate function

Practical Application :-

My requirement was to calculate the total number of unique customers from the date selected to
the start of month.
That is, if a random date was selected, say 30th April, 2008, i wanted the count of customers
from 1st April 2008 to 30th April 2008 (called Month-to-Date (MTD)).

So here i used the SUM function, which gave me some data (as seen in the image (red box)
below). Then i used Aggregate function & i got different data and didnt know which was right.
Take a look at the data within the red box in the Image and the MDX query below, and tell me
which is correct ?
Both are MTD calculation of the Customer Count measure, but the values are different. Why ?
So, here comes the obvious question.

What is the difference between SUM and Aggregate function in MDX ?

Aggregate function will use the aggregation function of the measure in the fact table irrespective
if it is a COUNT() or SUM().
While Sum function will always give you a sum of the measure in the fact table.
That is, if a new customer visits a shop, then the count of unique customers for that shop should
increase by one. And thereafter visit by the same customer shouldnt affect the count, right ?
If we used SUM function here, it will just do the addition and increase the total by two or more
depending on the number of visits. But Aggregate function will increase the count by only one,
thus giving us the right answer.

That is why, in such cases the value of SUM will always be equal or greater than the value of
Aggregate function.

To prove this, i used the Customer Count measure (without any time constraint) directly in
the select statement of the MDX query to get total count of customers.
And then, i created a SET called AllDates containing all fiscal time periods just to compare
with Customer Count measure using Aggregate and Sum function.
You can see the result in the Green box in the image below. Here Aggregate function gave me
the right answer when compared with Main measure but the Sum function gave me a wrong
answer.

WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )

SET AllDates
AS {
[Date].[Fiscal].[Fiscal Year].&[2006],
[Date].[Fiscal].[Fiscal Year].&[2007],
[Date].[Fiscal].[Fiscal Year].&[2008],
[Date].[Fiscal].[Fiscal Year].&[2009],
[Date].[Fiscal].[Fiscal Year].&[2011]
}

Member [Measures].[Customer Count - MTD (Agg)]


AS Aggregate ( PeriodsToDate ( [Date].[Fiscal].[Month], Day.item(0)),
[Measures].[Customer Count] )

Member [Measures].[Customer Count - MTD (Sum)]


AS Sum ( PeriodsToDate ( [Date].[Fiscal].[Month], Day.item(0) ), [Measures].[Customer
Count] )

Member [Measures].[Customer Count (Sum)]


AS Sum ( AllDates , [Measures].[Customer Count] )

Member [Measures].[Customer Count (Agg)]


AS Aggregate ( AllDates , [Measures].[Customer Count] )

SELECT {
[Measures].[Customer Count - MTD (Agg)],
[Measures].[Customer Count - MTD (Sum)],
[Measures].[Customer Count],
[Measures].[Customer Count (Sum)],
[Measures].[Customer Count (Agg)]
} ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Aggregate function

Note :- Aggregated Functions cannot be used on Calculated Members in the Measures


Dimension.

Category Archives: Set Functions

Descendants MDX function

Function Descendants
Name :

Category : Set

Description : Returns the set of descendants of a member at a specified level or distance,


optionally including or excluding descendants in other levels.

Syntax : Member expression syntax using a level expression


Descendants ( Member_Expression [ , Level_Expression [ ,Desc_Flag ] ] )

Member expression syntax using a numeric expression


Descendants ( Member_Expression [ , Distance [ ,Desc_Flag ] ] )

Set expression syntax using a level expression


Descendants ( Set_Expression [ , Level_Expression [ ,Desc_Flag ] ] )

Member expression syntax using a numeric expression


Descendants ( Set_Expression [ , Distance [ ,Desc_Flag ] ] )
Technet Link MDX Descendants Function
:

Example :-

The Descendants function returns a set of members at any level ( 2nd argument ) below the
parent member ( 1st argument ).
That is, in the below example, you can get a list of states ( or cities ) in USA by changing the
level in the 2nd argument of Descendants function.

SELECT {
[Measures].[Reseller Sales Amount]
} ON COLUMNS,
Descendants ( [Geography].[Geography].[Country].&[United States] ,
[Geography].[Geography].[State-Province] ) ON ROWS
FROM
[Adventure Works]

Output :-
MDX Children function

Children MDX function

Function Name : Children

Category : Set

Description : Returns the set of children of a specified member.

Syntax : Member_Expression.Children

Technet Link : MDX Children Function

Example :-

The Children function returns a naturally ordered set that contains the children of a specified
member. If the specified member has no children, this function returns an empty set.

SELECT {
[Measures].[Reseller Sales Amount]
} ON COLUMNS,
{ [Geography].[Geography].[Country].&[United States].Children } ON ROWS
FROM
[Adventure Works]
Output :-

MDX Children function

What is the difference between Members and Children function in MDX ?

The Children function requires a Member expression, while Members function needs a
dimension, level, or hierarchy expression.

Suppose you need to design an SSRS (SQL Server reporting services) report which has a
parameter called Country . And you have to show the Internet Sales for all the states of the
selected country.

In this case, the simplest MDX query can be formed using Children function as shown in above
MDX query.
Here, the Members function cant be used. But an alternative to using Children function is the
Descendants function as shown in below example.

SELECT {
[Measures].[Reseller Sales Amount]
} ON COLUMNS,
Descendants (
[Geography].[Geography].[Country].&[United States] ,
[Geography].[Geography].[State-Province]
) ON ROWS
FROM
[Adventure Works]
This query gives the same output as the previous query with Children function.

And what is the difference between Descendants and Children MDX function ?

Descendants returns a set of members at any level ( 2nd argument ) below the parent member (
1st argument ).
That is, in the above example, you can get the states or cities or postal codes in USA by changing
the level in the 2nd argument of Descendants function. But using Children function you can only
get the children, that is, states of USA.

Posted in: Set functions / Tagged: Analysis services, MDX, MDX function, MDX query, MDX
Set function, OLAP, SQL Server, SSAS, SSRS

Ascendants MDX function

October 28, 2012 11:00 pm / Leave a Comment / admin

Function Name Ascendants


:

Category : Set

Description : Returns the set of the ascendants of a specified member, including the
member itself.

Syntax : Ascendants ( Member_Expression )

Technet Link : MDX Ascendants Function

The Ascendants function returns all of the ancestors of a member from the member itself up to
the top of the members hierarchy; more specifically, it performs a post-order traversal of the
hierarchy for the specified member, and then returns all ascendant members related to the
member, including itself, in a set. This is in contrast to the Ancestor function, which returns a
specific ascendant member, or ancestor, at a specific level.

Example :-

SELECT {
Measures.[Reseller Order Count] } ON COLUMNS
Ascendants ( [Sales Territory].[Northwest]
) ON ROWS
FROM
[Adventure Works]

Output :-

Ascendants Traversal

MDX Ascendants function

AddCalculatedMembers MDX function

Function Name : AddCalculatedMembers

Category : Set

Description : Returns a set generated by adding calculated members to a specified set.


Syntax : AddCalculatedMembers ( Set_Expression )

Technet Link : MDX AddCalculatedMembers Function

Example :-

SELECT {
AddCalculatedMembers ( { [Measures].[Internet Sales Amount] } )
} ON COLUMNS
FROM
[Adventure Works]

Output :-

MDX AddCalulatedMembers function

Practical Application :-

Using AddCalculatedMembers function, you can get a list of all the calculated members from the
measures dimension in the Adventure Works cube. Here the AddCalculatedMembers function
checks the siblings of each member in the SET expression for Calculated members. If a Member
expression is specified as an argument in the AddCalculatedMembers function, then all its
siblings are checked and are included if they are calculated members along with the Member
specified.

SELECT {
AddCalculatedMembers ( { [Measures].Members } )
- [Measures].Members
} ON COLUMNS
FROM
[Adventure Works]

Output :-

MDX AddCalulatedMembers function


Members MDX function

October 24, 2012 10:54 pm / 4 Comments / admin

Function Name : Members

Category : Set

Description : Returns the set of members in a dimension, level, or hierarchy.

Syntax : Hierarchy syntax :- Hierarchy_Expression.Members


Level syntax :- Level_Expression.Members

Technet Link : MDX Members Function

Example :-

SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
[Product].[Product Categories].[Category].Members ON ROWS
FROM
[Adventure Works]

Output :-

MDX Members function

So whats the difference between Members and AllMembers MDX function

Suppose you need to get all the members of a specific level or Hierarchy including calculated
members.
In this case, following MDX query should be used :

SELECT
{ [Measures].[Amount] } ON COLUMNS, -- Exists In Measure Group 'Financial Reporting'
[Scenario].[Scenario].AllMembers ON ROWS
FROM
[Adventure Works]

Output :-

MDX AllMembers function

The same example using Members function :-

SELECT
{ [Measures].[Amount] } ON COLUMNS, -- Exists In Measure Group 'Financial Reporting'
[Scenario].[Scenario].Members ON ROWS
FROM
[Adventure Works]

Output :-

MDX Members function

In the above example it can be seen that, Budget Variance and Budget Variance % were
included in the AllMembers MDX function ( and not in Members MDX function ), because they
are calculated members defined in the Cube.

AllMembers MDX function


Function AllMembers
Name :

Category : Set

Description The AllMembers function evaluates either a hierarchy or a level expression


: and returns a set that contains all members of the specified hierarchy or level,
which includes all calculated members in the hierarchy or level.

Syntax : Hierarchy syntax :- Hierarchy_Expression.AllMembers


Level syntax :- Level_Expression.AllMembers

Technet MDX AllMembers Function


Link :

Example :-

SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
[Product].[Product Categories].[Category].AllMembers ON ROWS
FROM
[Adventure Works]

Output :-

MDX AllMembers function

Practical application :-

Suppose you need to get all the members of a specific level or Hierarchy including calculated
members.
In this case, following MDX query should be used (Please check the Note section to know this
functions real use):
SELECT
{ [Measures].[Amount] } ON COLUMNS, -- Exists In Measure Group 'Financial Reporting'
[Scenario].[Scenario].AllMembers ON ROWS
FROM
[Adventure Works]

Output :-

MDX AllMembers function

In the above example it can be seen that, Budget Variance and Budget Variance % were
included, because they are calculated members defined in the Cube.

Note :- Here you would like to know that, whats the difference between AllMembers and
Members MDX function. So check this same practical example in Members MDX function.

Ancestors MDX function

What is the difference between Ancestor and Ancestors MDX function ?

Ancestors function :-
A function that returns the set of all ancestors of a specified member at a specified level or at a
specified distance from the member. With Microsoft SQL Server Analysis Services, the set
returned will always consist of a single member Analysis Services does not support multiple
parents for a single member.

To answer the question, in SSAS database, there is only one difference till now, Ancestors
function returns a set while Ancestor function returns a member.
The specification for MDX has been developed and written somewhat independently of SSAS.
There are a number of other products like Essbase and Mondrian that support MDX.

So, maybe in the future, if SSAS database supports multiple parents for a single member,
Ancestors MDX function will be put to use.
Function Ancestors
Name :

Category : Set

Description : The Ancestors function returns the set of members at a specified level or at a
specified distance from the member.

Syntax : Level Syntax :- Ancestors(Member_Expression, Level_Expression)


Numeric Syntax :- Ancestors(Member_Expression, Distance)

Technet Link MDX Ancestors Function


:

Example :-

SELECT { [Measures].[Customer Count] } ON COLUMNS,


Ancestors( [Sales Territory].[Sales Territory].[Country].&[United Kingdom],
[Sales Territory].[Sales Territory].[Group] ) ON ROWS
FROM
[Adventure Works]

Output :-

MDX Ancestors function

Practical application :-

Suppose you need to design a SSRS (SQL Server Reporting services) Customers count by
Country & Continent report using OLAP Cube data. And here the report has a single-value
parameter called Country to show the number of customers only for the selected country and
respective continent.

In this case, following MDX query should be used:

WITH
Member [Measures].[Country Count Customers]
AS [Measures].[Customer Count]

Member [Measures].[Continent Count Customers (distance)]


AS Sum(Ancestors([Sales Territory].[Sales Territory].CURRENTMEMBER, 1 ) ,
[Measures].[Customer Count] )

Member [Measures].[Continent Count Customers (level)]


AS Sum (
Ancestors ( [Sales Territory].[Sales Territory].CURRENTMEMBER , [Sales
Territory].[Sales Territory].[Group] ) ,
[Measures].[Customer Count] )

SELECT
{
[Measures].[Customer Count],
[Measures].[Continent Count Customers (distance)],
[Measures].[Continent Count Customers (level)]
} ON COLUMNS,
{ [Sales Territory].[Sales Territory].[Country].&[United Kingdom] } ON ROWS
FROM
[Adventure Works]

Output :-

MDX Ancestors function practical

StrToSet MDX function

Function Name : StrToSet

Category : Set

Description : The StrToSet function returns a set for the supplied string.

Syntax : STRTOSET( String Expression )

Technet Link : MDX StrToSet Function

Example :-
SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOSET('[Customer].[Customer Geography].[Country].Members') ON ROWS
FROM
[Adventure Works]

Output :-

MDX StrToSet Function

Practical application :-

Suppose you need to design a SSRS (SQL Server Reporting services) Customers count by
Country report using OLAP Cube data. But here we will consider that the report has a multi-
value parameter called Country to show the number of customers only for the selected
countries.

In this case, following MDX query should be used:

SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOSET(@Country) ON ROWS
FROM
[Adventure Works]

Note:- In this case, StrToSet() function can also be used for single-value parameter instead of
StrToMember, because a set can have 0, 1 or more values.

You might also like