MDX hierarchy level AVG - ssas

i have a dimension hierarchy:
B - C - D - E
and one measure:
measures.eur
If i make calculated member:
AVG([hierarchy].[All],[Measures].[eur])
i can get single average from all hierarchy members in the top level of hierarchy.
But i want to have different average calculated from every level of the hierarchy.
So that when selecting hierarchy member C it shows the AVG of all the C's in the hierarchy,
when selecting B it's AVG of all B's and so forth..
I tried:
AVG(
[HIERARCHY].PARENT,
[Measures].[eur]
)
But it's not giving the average for all the members on the hierarchy levels, but it's very close to what i want.

You can try the following expression using the level function :
AVG ( [selected-member-of-c].level.members, [Measures].[eur] )

Related

conditional grouping of dimension members

I struggling with conditional grouping of some members within query.
In my simplified example I have two dimensions and one measure. The first dimension contains some categories and the second dimension some units. In all but one category, all categories have a single unit (category A and B in the example). However, one category has multiple units. If I put both dimensions on rows I get multiple results for category C (as in the following example). What I really need is to get one line per category, with the Amount appropriately aggregated but also with the displayed units (in case of category C, they should be replaced either by single "none" or concatenated together if this is not possible ("km, kg" in the example)).
The following example should illustrate what I am trying to achieve.
With the following query:
SELECT
NON EMPTY { [Measures].[Amount] } ON COLUMNS,
NON EMPTY { ([Grouping A].[Category].[Category].ALLMEMBERS *
[Grouping B].[Unit].[Unit].ALLMEMBERS ) } ON ROWS
FROM [ExampleCube]
I get data like this:
Category Unit Amount
A km 10
B km 5
C km 5
C kg 2
C km 5
But what I really need to achieve is to group the dimension Category and treat the dimension Unit and measure Amount appropriately as follows:
Category Unit Amount
A km 10
B km 5
C none 12
Maybe this is really simple but I have been trying to solve this for a while with no results.
Any ideas would be appreciated.
Thanks.
It's not simple. OLAP hasn't been designed for this kind of things.
Nonetheless, you can solve it in the following way:
With
[Measures].[Unit] as
IIF(
NonEmpty(
[Grouping B].[Unit].[Unit].Members,
[Measures].[Amount]
).Count > 1,
"None",
NonEmpty(
[Grouping B].[Unit].[Unit].Members,
[Measures].[Amount]
).Item(0).Name
)
Select
Non Empty { [Measures].[Unit],[Measures].[Amount] } on 0,
Non Empty { [Grouping A].[Category].[Category].Members } on 1
From [ExampleCube]

MDX show all sales until now

i have a huge table of cashflows that means there are +int values for income and -int values for outcome.
I have MeasureGroup for Sum the amount of money.
I now want to display not only the sum of money per month but also the sum of all the past time until the current month so like that:
Month MoneyAmount Total
1 20 20
2 -10 10
3 5 15
4 -10 5
So i know for the first part its just like
select [Measures].[Money] on 0,
[Date].[Month].Members on 1
From MyCube
but how can i add the sum column?
i thought about something like SUM( { NULL : [Date].[Month].CurrentMember } , [Measures].[Money] ) but that didnt work as well :(
In MDX, the total is already there. You do not have to do complex calculations to get it.
But it depends on your exact hierarchy structure how the All member is called. If you have a date user hierarchy named [Date].[Date], and it has a month level named [Date].[Date].[Month], then the all member of the hierarchy would probably be called something like [Date].[Date].[All]. If [Month] is an attribute hierarchy of the Date dimension, then the "all member" would probably be called [Date].[Month].[All]. In the latter case, the all member would already be the first member of the set [Date].[Month].Members. As you are asking the question, I am assuming this is not the case, and you are using a user hierarchy. Then you could change your MDX query to
select [Measures].[Money] on 0,
Union([Date].[Month].Members, { [Date].[Date].[All] }) on 1
From MyCube
Please note that you can change the name of the All member in the property settings of a dimension when designing an Analysis Services dimension, hence I cannot know the definitive name without knowing the details of this setting in your cube. So you might have to adapt the name of the all member.
You can find this name out in SQL Server Management Studio in an MDX window as follows: open the hierarchy that you are using, and then open the "Members" node, below which you should find the "All Member". You can drag this into your MDX statement, and the proper name will appear there.
As in a running sum?
You need a calculated measure, like this:
With Member [Measures].[Running Sum] as Sum( ( [Date].[Months].Members.Item(0) : [Date].[Months].CurrentMember ), [Measures].[Money])
Select [Date].[Months].Members on Rows,
{[Measures].[Money], [Measures].[Running Sum] } on Columns
From [MyCube]

using scope with calculated member

I have problem in my calculated member. Whenever this member involve in calculation or query it take large time to execute. I am trying to narrow down execution time.
I have to remove IIF condition from the members and start using scope instead.
CREATE Member CurrentCube.[Measures].[AvgAmount] as
IIF(ISLeaf([Customer].[ParentCustomer].currentmember),
[Measures].[Value],
(SUM([CCube^Customer].[ParentCustomer].CURRENTMEMBER.CHILDREN) /
COUNT([Customer].[ParentCustomer].CURRENTMEMBER.CHILDREN))
) ,
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
I have created hierarchy of customer which is [ParentCustomer] here. I want to see avg amount of all the children under the parent customer but when I am looking child level which does not have any children in it should only show the [Measures].[Amout].
Thanks in advance
Regards,
Sam
From your question, I assume you really want to have the average of the children, and not the average of all leaf level descendants. The latter could be implemented as follows:
Create a new measure group on the customer dimension table which has a single measure 'customer count' which would just be implemented as count - or of your customer dimension as a granularity that is finer than a single customer - countdistinct of the customer key or something like this.
Then just define your measure as
CREATE Member CurrentCube.[Measures].[AvgAmount] as
[Measures].[Value] / [Measures].[customer count],
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
This assumes that the aggregation of [Measures].[Value] is defined as sum or one of the semi additive aggregations, but not max or min or something similar.
However, I assume from your question that this is not what you want. Instead you want to see the average of the children at each level. And I assume that [Customer].[ParentCustomer] is a standard user hierarchy and not a parent child hierarchy. Then, the approach suggested in the title, using SCOPE, would work. Let's assume you have three levels in your [Customer].[ParentCustomer] hierarchy:
The (implicitly defined) All level, just containing the All member
level A, built from attribute A of the dimension
level B, which is the leaf level and built from attribute B of the dimension
Then, under similar assumptions about the [Measures].[Value] aggregation, you could define the AvgAmount measure as follows:
// create the measure as it is correct for level B:
CREATE Member CurrentCube.[Measures].[AvgAmount] as
[Measures].[Value],
Format_String = "#.0000000;-#.0000000;0;0",
Non_Empty_Behavior = [Measures].[Amout];
// overwrite the definition for level A:
SCOPE([Customer].[ParentCustomer].[A].Members);
[Measures].[AvgAmount] = [Measures].[Value] / (EXISTING [Customer].[B].[B].Members).Count
END SCOPE;
// overwrite the definition for the ALl level:
SCOPE([Customer].[ParentCustomer].&[All]);
[Measures].[AvgAmount] = [Measures].[Value] / (EXISTING [Customer].[A].[A].Members).Count
END SCOPE;
This approach, using SCOPE, would not work for a parent child hierarchy, buta syou do not write you have one, I just assume you don`t.

Calculate children values without taking into account grandchildren in a hiearchy

I've got a hierarchy setup in my OLAP-cube and I would like to calculate the child values from a specific node without taking into account the childrens children.
SELECT
[Colset]
,[RowSet] ON 1
FROM [Hierarchy].[Tree].&[24089].Children
If I execute this query I get sum for every child throughout the hierarchy. (Which of course is want you want in 99% of the cases). I instead would like to get the childrens values without the grandchildren.
What I like to do is something like this (pseudo-code)
SELECT
[Colset]
,[RowSet] ON 1
FROM Except( [Hierarchy].[Tree].&[24089].Children, [Hierarchy].[Tree].&[24089].GrandChildren)
Is this possible, and if, how?
Assuming SUM aggregation for your measure, something like that should do it:
with member val as sum( [Hierarchy].[Tree].&[24089], [Measures].currentMember )
- sum( descendants( [Hierarchy].[Tree].&[24089], 1, SELF ), [Measures].currentMember )
select ... where val

How to create a calculated member based on two measures and the hierarchy level of the queried dimension's current member?

I have a cube which has
two measure members: [Measures].[Value] (integer) and [Measures].[EffectiveBelowLevel] (integer).
a dimension called [DimParentChild] with a ragged user hierarchy called [ParentChildHierarchy].
I would like to create a calculated member on the measures dimension ([Measures].[EffectiveValue]) based on [Measures].[Value] which when queried along [DimParentChild] and [ParentChildHierarchy] behaves as follows:
- [Measures].[Value] is used if the hierarchy level of [DimParentChild].[ParentChildHierarchy].CURRENTMEMBER > [Measures].[EffectiveBelowLevel].
- 0 is used if the hierarchy level of [DimParentChild].[ParentChildHierarchy].CURRENTMEMBER <= [Measures].[EffectiveBelowLevel].
Is it possible to achieve this functionaly with a calcuated member on the measures dimension?
If yes then what the formula would look like?
If not then what other way would there be?
I am very interested in any other kind of solution as well (e.g. an mdx query, etc.)
As an example:
[Measures]
[Value] [EffectiveBelowLevel] ParentChildAssociation
10 1 GrandChild1
20 2 GrandChild2
[DimParentChild].[ParentChildHierarchy]
Member HierarchyLevel Description
Parent 1 -
Child 2 first child of Parent
GrandChild1 3 first child of Child
GrandChild2 3 second child of Child
With this data [Measures].[EffectiveValue] should look like this
ParentChild EffectiveValue
Parent 0
Child 10
GrandChild1 10
GrandChild2 20
How about something along the lines (I'm not sure about level ordinal being 0-based):
with member xx as
Sum( [DimParentChild].[ParentChildHierarchy].currentMember as myCurrentMember,
Sum( Descendants( myCurrentMember(0), 64, LEAVES ),
IIF( myCurrentMember(0).level.ordinal > [EffectiveBelowLevel], [Value], 0 )
)
)
select [xx] on 0, [DimParentChild].[ParentChildHierarchy].members on 1 from [...]
You can have a look to this MDX documentation here for more details.
I see you have posted this question here also (saw it originally on ssas msdn forum), so I am providing the link to my answer as it might help other people. thread link on SSAS msdn forum
#Marc - As this is a case of parent child dimension and p/c dimensions can have data associated on nonleaf members your query would not return the correct results. It took me some time to figure out how to aggregate the correct results from children in this case and recommend you have a look at the link. Offtopic: good luck with your product, I hope I'll get the time to evaulate it one day :)
Regards,
Hrvoje