Monday, November 2, 2015

MDX LastChild Function

In Multidimensional Expression, LastChild function will return the Last Child member belongs to the specified member. For example, If you know single customer name and if you want to find the Sales of a Last customer then you can use this LastChild function.

TIP: We can use MDX FirstChild Function to find the First Child Member.
MDX LastChild Function Syntax

The basic syntax of the MDX LastChild is:

Member_Expression.LASTCHILD

Member_Expression: Any Multidimensional Expression that returns valid Member.


In this article we will show you, How to write LastChild function in MDX query with examples. For this, We are going to use the below show data


Following screenshot shows the Countries inside the Geography





Following screenshot shows the [State – Provinces] inside the France Country






MDX LastChild Function Example


In this example we are going to find the Last Children present in the France Country. It means Last State present in the France country.


CODE


SELECT
[Measures].[Reseller Sales Amount] ON COLUMNS,
[Geography].[Geography].[Country].[France].LASTCHILD ON ROWS
FROM [Adventure Works];


OUTPUT





Analysis:

In the above MDX Query, We used [Reseller Sales amount] on the columns

[Measures].[Reseller Sales Amount] ON COLUMNS


Below line of code will write the Last child member of the France from all the State-Provices present in the France country.

[Geography].[Geography].[Country].[France].LASTCHILD

For Yveline State, there is no sales at all so, it is displaying Null results.
MDX LastChild Function Example 2

In this example we are going to find the Last Children present in the Countries list and calculate the Reseller Sales Amount of that.


CODE


SELECT
[Measures].[Reseller Sales Amount] ON COLUMNS,
[Geography].[Geography].[Country].[France].PARENT.LASTCHILD ON ROWS
FROM [Adventure Works];


OUTPUT





Analysis:

In the above MDX Query, We used [Reseller Sales amount] on the columns

[Measures].[Reseller Sales Amount] ON COLUMNS

From Below line of code,

[Geography].[Geography].[Country].[France].PARENT.LASTCHILD


MDX will first implement the Parent function to find the parent member of a France, Which is All Members. Please refer MDX Parent Function for further understanding

Next, It will implement LastChild function to find the Last child member of the Countries List Which is United States.
MDX LastChild Function Alternative

In this example we are going to use the LastChild function alternative to achieve the same result. Please refer MDX LastSibling Function to understand the LastSibling function.


CODE


SELECT
[Measures].[Reseller Sales Amount] ON COLUMNS,
[Geography].[Geography].[Country].[France].LASTSIBLING ON ROWS
FROM [Adventure Works];


OUTPUT