Average of sums in MDX query - mdx

How can I compute average of sums in MDX? I want to compute sum of spending for each person and then compute an average of it. I have the following query so far, but I believe it gives me wrong result:
WITH MEMBER [Measures].[Average] AS
AVG(Measures.cost)
SELECT [Measures].[Average] ON COLUMNS
FROM
( SELECT Measures.cost ON COLUMNS,
{Person.[Last name].MEMBERS}*
{Person.[First name].MEMBERS} ON ROWS
FROM Cube )
Any help would be greatly appreciated.

You just need to put in the first argument of Avg the set of your persons.
WITH MEMBER [Measures].[Average] AS
AVG(Person.[Last name].MEMBERS * Person.[First name].MEMBERS, Measures.cost)
SELECT [Measures].[Average] ON COLUMNS
FROM Cube

Try to create a calculated measure for this function like this
IIF (([Measures.cost] > 0), AVG(Person.Members,Measures.cost),null)
Then use this calculated measure in your MDX query
SELECT Person, 'calculated.measure'
FROM Cube
This is only a sample code to illustrate the function.

Related

Calculated measure to find the datediff using timedimension

I need to find out the number of days in Month based on Time dimension. Time dimension is Month when Jan is selected it has to return 31 as value.
If you have Time dimension and Time hierarchy, this should work:
WITH MEMBER measures.NumOfDays AS
Count
(
Descendants
(
[Time].[Time].CurrentMember,
,LEAVES
)
)
SELECT Measures.NumOfDays ON 0,
[Time].[Time].Month on 1
FROM [MyCube]
The below sample shows how to get the count.
Please note the below query only show the idea how to do this. Your cube will note have these attributes you you need to replace them
with member
measures.t as Count(([Date].[Month of Year].&[1],[Date].[Day of Month].[Day of Month].members))
select {measures.t}
on columns
from [Adventure Works]

How To Have Recursive sum in MDX?

I need to have calculated member in mdx to calculate recursive sum
that should summarize itself with the previous member
You can use the following examples. One is for running total the other one is a bit diffrent.
Cumulative Sum/ Running Total | MDX
MDX Difference between dates non null and of the same dimension

SSAS count of non zero measure

I am newbie to SSAS cube and need some help. I have a cube created from a fact table and one of the measure, lets call it amount, contains zeros. This measure was created as a SUM.
Now I also have the Count measure added by SSAS designer.
What I need is the count of all non zero amount.
I tried to add a calculated measure as
`IIF([Measures].[amount] > 0,[Measures].[RowCount],null)`
also tried
FILTER([Measures].[RowCount],[Measures].[Amount] > 0)
Both these returns the count including the amount=0.
I am validating it via SSMS sql query
SELECT count(*)
FROM [dbo].[Fact_Session]
where SiteKey = 5 and DateKey = 20170201
and Amount >0
Any help is appreciated. My assumption is that the IIF/Filter statement will operate on the individual rows before cubing, as once aggregated into a dimension, the amount will not be 0 due to the aggregation . Please Help.
To count non-zero you can use this approach:
WITH
MEMBER [Measures].[IsNotEmpty] AS
IIf(
Not(IsEmpty([Measures].[Amount]))
,1
,Null
)
But better to do this inside a SUM over a particular set - so the following counts how many subcategories are not empty:
WITH
MEMBER [Measures].[SubCategory_IsNotEmpty] AS
SUM(
[Product].[Product Categories].[SubCategory].members
,IIf(
Not(IsEmpty([Measures].[Amount]))
,1
,Null
)
)

Median function in MDX (Pentaho Mondrian)

I am trying to implement the Median function in MDX. In particular I want to calculate the median of the measure "Amount" for every member of the dimension "Region". For this, I first wrote the following MDX query:
with member [measures].[X] as median([Regions].members, [measures].[Amount])
select {[Regions].members} on rows,
{[measures].[X]} on columns
from [product]
However the above query will calculate the average "Amount" for every member of "Region" and then return the median of these (average) values.
To fix the issue I rewrote the above query using a "Productid" dimension that assigns a unique id to each product of the cube:
with member [measures].[X] as median([productid].members, [measures].[Amount])
select {[Regions].members} on rows,
{[measures].[X]} on columns
from [product]
The above query correctly returned the median "Amount" values for every member of "Region". The problem is that the query seems to be computationally expensive and it hangs. I was wondering if there is a more elegant solution to the problem.

Regarding Max function in MDX

I have a fact table with structure: (objectid, fromLoc, toLoc, duration).
I need the equivalent MDX for the following SQL:
select fromLoc, toLoc, max(duration) from fact group by fromLoc, toLoc;
I tried with the following MDX but it's showing wrong results. It's showing sum(duration) instead of showing max value for each grouping:
WITH
MEMBER [Measures].[MaxValue] AS
max([Measures].[Duration])
select FromLoc.members on axis(1),
toLoc.members on axis(0)
from [cube1]
where [Measures].[MaxValue]
Please help me by providing the correct MDX which can answer the queries like the above SQL.
You have have to use the max function like this:
max(the set of your objectids, [Measures].[Duration])