MDX: Recursion over propery - mdx

When having a dimension [Date] containing dates. The members of this dimension have a property „LastRelevantDate“. I would like to write a mdx query returning the member names which starts at a given Date and iteratively looks up the next member given in the „LastRelevantDate“ property etc. I‘d like this to be repeated x–times. Is this possible with mdx? How could such a query look like?

These functions are iterative in MDX:
GENERATE
FILTER
An example from MSDN for Generate:
SELECT
{[Measures].[Internet Sales Amount]}
ON 0,
GENERATE(
[Date].[Calendar Year].[Calendar Year].MEMBERS
, TOPCOUNT(
[Date].[Calendar Year].CURRENTMEMBER
*
[Product].[Product].[Product].MEMBERS
,10, [Measures].[Internet Sales Amount]))
ON 1
FROM [Adventure Works]

Related

Parser: The syntax for 'SCOPE' is incorrect

I'm learning MDX and having trouble with SCOPE.
As a test I'm trying to create a new measure only for Fiscal Year 2011 but
have a syntax error.
It isn't a Create Calculated Measure in SSAS Script but a query in SSMS.
Any help or pointers would be great.
WITH MEMBER [Measures].[Test Scope]
AS 1
(
SCOPE ([Measures].[Test Scope]);
SCOPE ([Date].[Fiscal Year].&[2011]);
THIS = [Measures].[Reseller Order Quantity] + 10000;
END SCOPE;
END SCOPE;
)
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]
Query (4, 2) Parser: The syntax for 'SCOPE' is incorrect.
You can’t use a scope statement in a query. It has to be defined in the MDX script of the cube or run in the context of a session. The most common way to handle this is the following.
WITH MEMBER [Measures].[Test Scope] AS
IIF(
[Date].[Fiscal Year].CurrentMember is [Date].[Fiscal Year].&[2011],
[Measures].[Reseller Order Quantity] + 10000,
NULL
)
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]
There is also very old MDX syntax CELL CALCULATION in an MDX query which I would recommend not using without through testing since it is infrequently used. Off the top of my head it would look like this. It could “scope” over any measure or calculated measure or tuple. Here we have scoped over a null calculated measure.
WITH MEMBER [Measures].[Test Scope] AS
NULL
CELL CALCULATION [Test Scope Calc]
FOR '([Measures].[Test Scope],[Date].[Fiscal Year].&[2011])'
AS '[Measures].[Reseller Order Quantity] + 10000'
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]

Calculate maximum value for a measure column without using dimension in mdx query

I would like to calculate maximum value for a measure column without using dimension in mdx query. I tried the following query to achieve this.
WITH MEMBER [Measures].[Max key] AS
Max([Internet Sales Amount].Members,[Internet Sales Amount].currentmember.MEMBER_KEY)
SELECT {[Measures].[Max key]} on COLUMNS
FROM [Adventure Works]
But I got the error in result.
Can anyone suggest me how to achieve my requirement using mdx query?
You've written this:
WITH
MEMBER [Measures].[Max key] AS
Max
(
[Internet Sales Amount].MEMBERS
,[Internet Sales Amount].CurrentMember.Member_Key
)
SELECT
{[Measures].[Max key]} ON COLUMNS
FROM [Adventure Works];
[Internet Sales Amount] is a measure and therefore a numeric expression so you cannot apply the functions MEMBERS or CurrentMember. [Internet Sales Amount] will not even have keys - so why are you trying to find a max key?
Max is defined here: https://learn.microsoft.com/en-us/sql/mdx/max-mdx
You have no choice but to include a set for the max to work over:
Max( Set_Expression [ , Numeric_Expression ] )
This is a valid use of max:
WITH
MEMBER [Measures].[Max Sales] AS
Max
(
[Product].[Category].[Category]
,[Internet Sales Amount]
)
SELECT
[Product].[Category].[Category] ON ROWS
,{
[Internet Sales Amount]
,[Measures].[Max Sales]
} ON COLUMNS
FROM [Adventure Works];
It returns this:

MDX - active in current period, and not active in previous period

What is the most efficient way to do this in MDX? I know you can create 2 calculated measures, having active with the time dimension current period selected, and another having the previous period, and then do a filter with a complex condition.
However what about other functions which may be more efficient? Any recommendations?
My goal is to create a set of calculated measures, which would help customer analysts. The main 2 dimensions, for these are [Calendar], and obviously [Customer]. I have a fact, which contains activity for customers. These are the 3 entities that our calculations would be based on.
This is from an article by Chris Webb: http://cwebbbi.wordpress.com/2010/10/08/counting-new-and-returning-customers-in-mdx/
With a follow up article on how to optimize it: http://cwebbbi.wordpress.com/2013/06/28/optimising-returning-customers-calculations-in-mdx/
WITH
MEMBER MEASURES.[Returning Customers] AS
COUNT(
NONEMPTY(
NONEMPTY(
[Customer].[Customer].[Customer].MEMBERS
, [Measures].[Internet Sales Amount])
, {[Measures].[Internet Sales Amount]}
* {NULL : [Date].[Calendar].CURRENTMEMBER.PREVMEMBER}
)
)
MEMBER MEASURES.[New Customers] AS
[Measures].[Customer Count] – MEASURES.[Returning Customers]
SELECT
{[Measures].[Customer Count]
, MEASURES.[Returning Customers]
, MEASURES.[New Customers]} ON 0,
[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works]

ORDER BY last column

I have this:
SELECT
NONEMPTY(
[Date].[Calendar].[Calendar Year].MEMBERS
)
*
[Measures].[Internet Sales Amount] on 0,
ORDER(
[Customer].[Customer Geography].[State-Province].MEMBERS
,[Measures].[Internet Sales Amount]
,BDESC)
on 1
from [Adventure Works]
What is the most efficient way of ordering descending by the last date column - this should be dynamic so that it still works when we go over month end.
I would use
ORDER(
[Customer].[Customer Geography].[State-Province].MEMBERS
,([Measures].[Internet Sales Amount]
,Tail([Date].[Calendar].[Calendar Year]).Item(0).Item(0)
)
,BDESC)
and assume that the added term Tail([Date].[Calendar].[Calendar Year]).Item(0).Item(0) can be calculated by Analysis Services just checking the time dimension, and does not need any dynamic context, and hence should be fast.
If you want to have an expression which is independent of the content of the columns, you can use:
ORDER([Customer].[Customer Geography].[State-Province].MEMBERS
,Tail(Axis(0)).item(0)
,BDESC)

NONEMPTY behaviour seemingly not limiting results

The following script is from this MSDN article http://msdn.microsoft.com/en-us/library/ms145988.aspx
SELECT
[Measures].[Internet Sales Amount] ON 0,
NONEMPTY(
[Customer].[Customer].[Customer].MEMBERS,
{([Date].[Calendar].[Date].&[20010701], [Measures].[Internet Sales Amount])}
) ON 1
FROM [Adventure Works]
I basically took the script and replaced the elements from the above with equivalent elements from our cube.
What would be the reason that my script is not limiting it's results to the date specified in the NONEMPTY function but is returning all customers and their lifetime sales amount?
Try this:
SELECT
[Measures].[Internet Sales Amount] ON 0,
NONEMPTY(
[Customer].[Customer].[Customer].MEMBERS
) ON 1
FROM [Adventure Works]
where
[Date].[Calendar].[Date].&[20010701]
All the customers might have a non-null internet sales amount for the date you have specified. Moreover the nonempty will just filter out those members where the condition is not met. In your case as all the customers has non-null Internet Sales value, none of the customers are filtered out. And the Internet sales amount in the axis 0 will not be limited to the date that is specified, instead it returns the sales amount for each customer across all the dates.