MDX Query
MDX Query
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
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.
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
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
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
Example :-
SELECT {
[Geography].[Geography].Levels( 1 )
} ON COLUMNS,
FROM
[Adventure Works]
SELECT {
[Geography].[Geography].Levels( 'Country' )
} ON COLUMNS,
FROM
[Adventure Works]
Output :-
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.
Category : Logical
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.
Category : Logical
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 :-
Category : Logical
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
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
Category : Logical
Description : Returns whether the evaluated expression is the empty cell value.
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] )
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 :-
WITH
SET Selected_Month
AS StrToMember ( " [Date].[Fiscal].[Month].&[2008]&[1] " )
SELECT {
[Measures].[Month Sales],
[Measures].[Year Sales],
[Measures].[% of Year]
} ON COLUMNS ,
{ [Product].[Product Categories].[Category] } ON ROWS
FROM
[Adventure Works]
Output :-
Category : Logical
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 :-
Practical Example :-
WITH
MEMBER [Measures].[CurrMemID]
AS [Product].[Product Line].CurrentMember.UniqueName
SELECT {
[Measures].[ProductLine Custom],
[Measures].[Measures].[CurrMemID]
} ON COLUMNS,
[Product].[Product Line].Members ON ROWS
FROM [Adventure Works]
Output :-
Practical Example :-
WITH
Member [Measures].[LAST CHILD]
As [Date].[Fiscal].[Fiscal Quarter].&[2008]&[2].LastChild.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 :-
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
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 :-
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 :-
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.
Practical Example :-
WITH
MEMBER [Measures].[Financial Report 1]
AS SUM ( [Ship Date].[Date].&[20080401] , [Measures].[Amount] )
SELECT {
[Measures].[Financial Report 1],
[Measures].[Financial Report 2],
[Measures].[Financial Report 3]
} ON COLUMNS
FROM
[Adventure Works]
Output :-
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.
Posted in: Member functions / Tagged: LinkMember, MDX, MDX function, MDX member
function, Multi dimensional expression, OLAP, SQL Server, SSAS, SSRS
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 :-
Note :- For practical application you can see the ClosingPeriod 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 :-
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
SELECT{
[Measures].[Opening Date],
[Measures].[Closing Date]
} ON COLUMNS
FROM
[Adventure Works]
Output :-
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 :-
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]" )
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
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 :-
Output :-
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]
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 :-
Example :-
SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOMEMBER('[Customer].[Customer Geography].[Country].&[Australia]') ON ROWS
FROM
[Adventure Works]
Output :-
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.
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.
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 ] )
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] )
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.
WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )
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 :-
Category : Numeric
Description : Returns the sum of a numeric expression evaluated over a specified set.
Example :-
WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )
SELECT
{ [Measures].[Internet Sales Amount - Day] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]
Output :-
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]" )
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 :-
Note :- To know the difference between Sum and Aggregate MDX function. Please see
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.
Example :-
WITH
SET Day
AS StrToSet ( "[Date].[Fiscal].[Date].&[20080430]" )
SELECT
{ [Measures].[Customer Count - Day (Agg)] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]
Output :-
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.
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]
}
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 :-
Function Descendants
Name :
Category : Set
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
Category : Set
Syntax : Member_Expression.Children
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 :-
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
Category : Set
Description : Returns the set of the ascendants of a specified member, including the
member itself.
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
Category : Set
Example :-
SELECT {
AddCalculatedMembers ( { [Measures].[Internet Sales Amount] } )
} ON COLUMNS
FROM
[Adventure Works]
Output :-
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 :-
Category : Set
Example :-
SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
[Product].[Product Categories].[Category].Members ON ROWS
FROM
[Adventure Works]
Output :-
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 :-
SELECT
{ [Measures].[Amount] } ON COLUMNS, -- Exists In Measure Group 'Financial Reporting'
[Scenario].[Scenario].Members ON ROWS
FROM
[Adventure Works]
Output :-
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.
Category : Set
Example :-
SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
[Product].[Product Categories].[Category].AllMembers ON ROWS
FROM
[Adventure Works]
Output :-
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 :-
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 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.
Example :-
Output :-
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.
WITH
Member [Measures].[Country Count Customers]
AS [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 :-
Category : Set
Description : The StrToSet function returns a set for the supplied string.
Example :-
SELECT
{ [Measures].[Customer Count] } ON COLUMNS,
STRTOSET('[Customer].[Customer Geography].[Country].Members') ON ROWS
FROM
[Adventure Works]
Output :-
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.
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.