MDX query - Average - mdx

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]

Related

Cumulative Sum | Exclude attribute from calculation

I am trying to do Cumulative Sum/ Running Total using the function below so the user can use any date level, but the requirement is to ignore some of the attributes from the calculation.
Function used:
SUM(NULL:Axis(1).Item(0).Item(Axis(1).Item(0).Count-1).Hierarchy.CurrentMember, [Measures].[ Number of Ticket])
Example:
The table below shows the Cumulative Sum as expected
Example - 1
Here by adding another attribute, Program Remaining, as shown below, its changes the Cumulative behavior, Because Excel will add that attribute to the grouping so it reset the cumulative sum:
Example - 2
Is there a way that I can exclude the Program Remaining attribute from the calculation (I have another 4 attributes that I want to exclude) so that the cumulative can be increased just like the first table even with adding these attribute.
I really appreciate any help
Try using the below sample query
with
member
[Measures].[Internet Sales AmountRunningtotal]
as
case when [Measures].[Internet Sales Amount] = null then null
else
sum({[Product].[Subcategory].firstchild:[Product].[Subcategory].currentmember},[Measures].[Internet Sales Amount])
end
select {[Measures].[Internet Sales Amount],
[Measures].[Internet Sales AmountRunningtotal]
} on columns,
non empty
([Date].[Calendar Year].[Calendar Year],[Date].[Calendar Quarter of Year].[Calendar Quarter of Year],
[Product].[Category].[Category],[Product].[Subcategory].[Subcategory])
on
rows
from
[Adventure Works]
So I explored the sample sent, Your only solutions is to keep the date in the inner most position, your query qouted above will break if you are not using entire members of an attribute.

Get grand total by column

I want to calculate the grand total of a given measure in MDX by a column dimension (time dimension)
This is my MDX code:
SUM(
([DIM_TIME].[PERIOD]),
[Measures].[VALUE]
)
Basically, this is what I want to get :
But this is what I get:
Thanks for any possible help
Take a look at the sample query
with member
[Measures].[Internet Sales AmountTotal]
as
case when [Product].[Category].currentmember is [Product].[Category].defaultmember then null
else ([Product].[Category].defaultmember,[Measures].[Internet Sales Amount])
end
select
({[Date].[Calendar Year].&[2012],[Date].[Calendar Year].&[2013]},
{[Measures].[Internet Sales Amount],[Measures].[Internet Sales AmountTotal]})
on columns,
{[Product].[Category].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:

How to filter measure multiple times in cube

I need to product a report from my cube that looks something like the following.
(dummy data)
Where it lists sales and gross profit for today, this week, the period and year to date across the products category.
My cube is setup as follows
A date dimension
And the cube itself
Currently I have not implemented the product category pieces.
I'm struggling with how to write an MDX query that can return the sales/gross profit for a single day and then week and so on.
I can return it by itself like so
SELECT {[Measures].[Gross Profit],[Measures].[Price]} ON COLUMNS
From [Cube]
WHERE [Date].[Date Key].[2015-04-22];
and so on for the other various types (week etc), but I'm unsure as how to apply the where filter to the columnn itself rather than the overall query, or if this is even the correct way to do it and I should be making multiple MDX calls that I then compose in my app that will use this.
Can anyone give me a pointer in the right direction here?
EDIT: Seems to mostly work using the approach #Akshay Rane described however I cannot get one of my measures to work
MEMBER [This Week] as
(PeriodsToDate([Date].[Fiscal Week Date].[Fiscal Week],StrToMember('[Date].[Fiscal Week Date].[Date Key].&[' + '20140401' + ']'))
,[Measures].[Merchandise Gross Profit])
Gives me an error:
The function expects a string or numeric expression for the argument. A tuple set expression was used.
Any pointers here?
You will have to create separate Calculated Members for each time interval you want to aggregate the data upon.
This can be done in [Adventure Works] cube as follows.
WITH
MEMBER [Today] as
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER)
MEMBER [Last Week] as
AGGREGATE (
{ [Date].[Date].CURRENTMEMBER.lag(6) : [Date].[Date].CURRENTMEMBER }
, [Measures].[Internet Sales Amount]
)
SELECT
{ [Today], [Last Week] } ON 0,
{ ([Product].[Product Categories].[Category], [Date].[Date].[Date]) } ON 1
FROM
(
/*FILTERING ON SPECIFIC DATE USING SUBCUBE*/
SELECT [Date].[Date].&[20070715] ON 0
FROM [Adventure Works]
)
If you can take the different levels of date from the same user hierarchy then something like this is possible:
SELECT
{
[Date].[Calendar].[Month].&[2006]&[7]
,[Date].[Calendar].[Date].&[20050220]
}
*
{
[Measures].[Order Quantity]
,[Measures].[Internet Sales Amount]
} ON COLUMNS
,{[Product].[Category].Children} ON ROWS
FROM [Adventure Works];
The above produces results like this:

Period on Period growth for a dynamic measure

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]