Hello MDX and SSAS people,
I was wondering how to create a time calculation measure that summarizes the selected period/month's total value for the entire year in this case Budget. So if I select 202004 from my time hierarchy it should be the total budget of year 2020 and if I select 201904 it should be the total budget of 2019.
In the picture below I have created [YTD Budget] with the following mdx code:
Aggregate(periodstodate([Time].[Year-Month-Day].[Year],[Time].[Year-Month-Day].currentmember),
[Measures].[Budget Amount])
Now I would like to create [Full year Budget] but have not found a solution yet (novice mdx user). Anyone that has a solution for this?
Best regards,
Rubrix
Here is the example on Adventure Works:
with member Measures.YearName
AS (ANCESTOR([Date].[Calendar].Currentmember, [Date].[Calendar].[Calendar Year])).Name
member Measures.FullYear
AS SUM(ANCESTOR([Date].[Calendar].Currentmember, [Date].[Calendar].[Calendar Year]), [Measures].[Internet Sales Amount])
select {[Measures].[Internet Sales Amount], Measures.FullYear, Measures.YearName } on 0,
{[Date].[Calendar].[Calendar Quarter].&[2012]&[1],
[Date].[Calendar].[Calendar Quarter].&[2012]&[2],
[Date].[Calendar].[Calendar Quarter].&[2012]&[3],
[Date].[Calendar].[Calendar Quarter].&[2012]&[4],
[Date].[Calendar].[Calendar Quarter].&[2013]&[1],
[Date].[Calendar].[Calendar Quarter].&[2013]&[2],
[Date].[Calendar].[Calendar Quarter].&[2013]&[3],
[Date].[Calendar].[Calendar Quarter].&[2013]&[4]
} on 1
from [Adventure Works]
So in your case the member definition should be:
with member Measures.FullYear
AS SUM(ANCESTOR([Time].[Year-Month-Day].Currentmember, [Time].[Year-Month-Day].[Year]), [Measures].[Budget Amount])
Related
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]
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:
I wrote an mdx script showing period on period growth for Internet Sales Amount, and it all works fine.
We are using an interface, where you can place a slicer so that user can choose what dimension of date.Calendar he is interested in, i.e.whatever dimension of Date.Calendar the user decides to choose (Quarter, Month, Year) it will correctly look at previous member. Now, im trying to obtain the same for Measure - i.e. i want it to be flexible dependant on what measure the user will choose in the slicer ( i.e. Internet tax Amount).
I cant think of a way of creating a previous member with measure being flexible...
WITH
MEMBER [Measures].[Internet Sales Amount PP] AS
(
[Date].[Calendar].CurrentMember.Prevmember,
[Measures].[Internet Sales Amount]
)
MEMBER [Measures].[Period on Period Grwth %] AS
IIF (
[Measures].[Internet Sales Amount PP] = 0,
'N/A',
([Measures].[Internet Sales Amount]-[Measures].[Internet Sales Amount PP]) /[Measures]. [Internet Sales Amount PP]
)
,FORMAT_STRING = "percent"
SELECT
[Date].[Calendar].[Month] ON ROWS,
{[Measures].[Internet Sales Amount],
[Measures].[Internet Sales Amount PP],
[Measures].[Period on Period Grwth %]} ON COLUMNS
FROM [Adventure Works]
If you happen to use SSRS, you can add a parameter to absorb the measure selected in the drop down from front end. We host SSRS in SharePoint environment and this is how we do it.
WITH
MEMBER [Measures].[Measure Growth PP] AS
(
[Date].[Calendar].CurrentMember.Prevmember,
strtomember('[Measures].'+ #MeasureSelected)
)
MEMBER [Measures].[Period on Period Grwth %] AS
IIF (
[Measures].[Measure Growth PP] = 0,
'N/A',
(strtomember('[Measures].'+ #MeasureSelected)-[Measures].[Measure Growth PP]) /
[Measures].[Measure Growth PP]
)
,FORMAT_STRING = "percent"
SELECT
[Date].[Calendar].[Month] ON ROWS,
{strtomember('[Measures].'+ #MeasureSelected),
[Measures].[Measure Growth PP],
[Measures].[Period on Period Grwth %]} ON COLUMNS
FROM [Adventure Works]
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]
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)