Period on Period growth for a dynamic measure - mdx

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]

Related

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 query - Average

I would like to calculate average using a numeric value in dimension instead of using a measure. Is that possible?
Also I would like to know whether average can be calculated with a dimension that is categorized with another dimension.
Here is the Avg function onMSDN: https://msdn.microsoft.com/en-us/library/ms146067.aspx
It gives several examples of using it with numeric expressions such as the following:
WITH MEMBER Measures.[Avg Gross Profit Margin] AS
Avg(
Descendants(
[Ship Date].[Fiscal].CurrentMember,
[Ship Date].[Fiscal].[Date]
),
Measures.[Gross Profit Margin]
)
SELECT
Measures.[Avg Gross Profit Margin] ON COLUMNS,
[Ship Date].[Fiscal].[Fiscal Year].[FY 2003].Children ON ROWS
FROM
[Adventure Works]

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]

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.

MDX Calculating 12 month average unit price and applying to forecast quantity

I have a requirement to show a report which calculates the average selling unit price of each product and then multiplies this average by the number of units forecasted to sell in the next year (for each product).
At this point my main issue is getting this set up so the totals roll up correctly when viewing at product category level. Using AdventureWorks as an example (and sales orders instead of forecasts) I've got this far...
with
member [Measures].[Sales Order Value]
as sum(descendants([Product].[Product Categories].currentMember,
[Product].[Product Categories].[Product]),
[Measures].[Average Unit Price] * [Measures].[Order Count]),
format_string = "Currency"
select ([Date].[Calendar].[Calendar Year].&[2008],
{[Measures].[Sales Order Value] }) on columns,
[Product].[Product Categories].[Subcategory].members on rows
from [Adventure Works]
I think this is about right, I believe this is going down to product level to apply the calculation between product average unit price and any sales orders for the product.
My issue is that I think the average unit price is being calculated over all data, I need to alter this to pick up an average based on the last 12 months only.