MDX; Get aggregation of measure over all values in a hiercharchy except named ones - ssas

This tuple gets me an aggregation of measure X in the dimension of [MyHierarchy].[MyHierarchy].&[some_value].
([Measures].[X], [MyHierarchy].[MyHierarchy].&[some_value])
But what is the complement - ie Measures.X in the same hierarchy but excluding [MyHierarchy].[MyHierarchy].&[some_value]
I'm sure it's the EXCEPT function but I can't seem to get the syntax right.

The general idea to aggregate over any kind of set is something like:
with member [Measures].[Y] as
aggregate(
except( [hierarchy].members, { excluded } ),
[Measures].[X] )
)
Aggregate() is handling whatever actual aggregation of the [Measures]. The issue is that if [hierarchy].members contains some intermediate (parent) members, you'll have values counted twice.
If the aggregation is SUM and you've an all member, then the following is giving the expected result :
with member [Measures].[Y] as
( [hierarchy].[all], [Measures].[X] )
- sum( { excluded }, [Measures].[X] )
If no all then you can use members of the first level:
with member [Measures].[Y] as
sum( [hierarchy].levels(0), [Measures].[X] )
- sum( { excluded }, [Measures].[X] )
_

The following expresion works with non linear aggregation functions:
WITH MEMBER [Measures].[Y] as
Aggregate(
Except( [MyHierarchy].[MyHierarchy].&[some_value].Level.Members, { [MyHierarchy].[MyHierarchy].&[some_value] } ),
[Measures].[X] )
)
You can generalize it when you have to exclude several members that belong to the same level (the excluded members are in the excludedMembers set):
WITH MEMBER [Measures].[Y] as
Aggregate(
Except( excludedMembers.Item(0).Level.Members , excludedMembers ),
[Measures].[X] )
)

Related

MDX Query aggregate combining crossjoin expression with non-crossjoin/comma-separated expression

I have an issue while retrieving on columns with crossjoin(someMeasure1,someMeasure2) , someMeasure3. Details:
WITH MEMBER [Date].[Custom] AS
'Aggregate({[Date].&[20220101]:[Date].&[20220810]})'
MEMBER [Measures].[Total YTD] AS
'([Date].[Test1], [Measures].[Customer])'
SELECT
crossjoin(
{[Measures].[Customer Form], [Measures].[Customer]},
[Date].[Monthly Key]
),
[Measures].[Total YTD] on 0,
nonempty(
{[Hier Top].[Hier1].[city].members}
) on 1
FROM
[WAREHOUSE]
It gives output:
How do combine someMeasure3 (ie [Measures].[Total YTD]) with the other crossjoined members? (I believe it's similar to SQL's aggregate functions, eg sum()/count()/max()/etc, with group by clause)
Best Wishes
Just realise there's Union() function there, or operator +. I can specify the second member of the tuple as Year key. Solution is
WITH MEMBER [Date].[Custom] AS
'Aggregate({[Date].&[20220101]:[Date].&[20220810]})'
MEMBER [Measures].[Total YTD] AS
'([Date].[Test1], [Measures].[Customer])'
SELECT
crossjoin(
{[Measures].[Customer Form], [Measures].[Customer]},
[Date].[Monthly Key]
)
+
([Measures].[Total YTD], [Date].[Yearly Key]) on 0,
nonempty(
{[Hier Top].[Hier1].[city].members}
) on 1
FROM
[WAREHOUSE]
Note that I am using op () not {} to specify the member dimensionality manually for [Measures].[Total YTD], [Date].[Monthly Key]
All good

MDX Get dimension count for each level of hierarchy

I have an MDX query which drills into a hierarchy - I need the count of a dimension for each level. I have tried a count function but it doesnt seem to work against each level:
member [RefIDCount] as distinctCount([Incident Details].[Process Safety Classification].&[Tier 1])
select
{
[RefIDCount]
} on columns,
{
DESCENDANTS(
[Reporting Hierarchies].[Hierarchy].[Reporting Category].&[49]
-- #RepCat#
)
} on rows
FROM [Monthly Stats]
This gives me the same count for each level :
What am I doing wrong? How do I get the count to be per level?
Take a look at the example below
with member measures.t
as
[Product].[Product Categories].currentmember.level.members.count
select
{[Measures].[Internet Sales Amount],measures.t}
on 0,
descendants([Product].[Product Categories].members
)
on 1
from
[Adventure Works]
Result

MDX: Moving average calculation only gives (null) results

I am trying to compute a moving average using MDX in SSAS 2014,but all results come out (null).
Reading multiple web references and blog posts makes me think that this should work, giving a 3-month moving average:
With
MEMBER [Measures].[MA3] AS
Avg(
[Date].[Calendar Years].[Month].CurrentMember.Lag(2)
: [Date].[Calendar Years].[Month],
[Measures].[Project Views]
)
SELECT { [Measures].[Project Views], [Measures].[MA3] } ON 0,
[Date].[Calendar Years].[Month] ON 1
FROM [ProjectAccesses]
However, (null) appears in each column.
I'd expect the count of members to be 3 in every case using:
MEMBER [Measures].[C3] AS
Count(
[Date].[Calendar Years].[Month].CurrentMember.Lag(2)
: [Date].[Calendar Years].[Month],
INCLUDEEMPTY
)
but again, no syntax error is detected but all values are (null), leading me to believe that maybe the month-range sets are empty.
I've been around and around this for hours now and have run out of things to try.
Can anyone suggest what might be going wrong please; I'm a newcomer to MDX so it's possible that my query is wrongly structured or there is some error in my cube configuration that I have not been able to find.
Thanks
I believe you need to change
": [Date].[Calendar Years].[Month],"
To
": [Date].[Calendar Years].currentmember,"
[Date].[Calendar Years].[Month] -> should give the level, not the current member which is what you want
I'm surprised that you cannot use the Calendar Years hierarchy throughout. I've added a couple of extra diagnostic measures so you can see what the currentmember function is returning:
WITH
MEMBER [Measures].[MA3] AS
AVG(
[Date].[Calendar Years].CurrentMember.Lag(2)
: [Date].[Calendar Years].CurrentMember,
[Measures].[Project Views]
)
MEMBER [Measures].[C3] AS
COUNT(
[Date].[Calendar Years].CurrentMember.Lag(2)
: [Date].[Calendar Years].CurrentMember,
INCLUDEEMPTY
)
MEMBER [Measures].[Diagn1] AS
[Date].[Calendar Years].CurrentMember.member_caption
MEMBER [Measures].[Diagn2] AS
[Date].[Calendar Years].CurrentMember.Lag(2).member_caption
SELECT
{
[Measures].[Project Views]
, [Measures].[MA3]
, [Measures].[C3]
, [Measures].[Diagn1]
, [Measures].[Diagn2]
} ON 0,
[Date].[Calendar Years].[Month].MEMBERS ON 1
FROM [ProjectAccesses];
I found the solution. I had assumed that i would need to form my set based on a member within my [Calendar Years] hierarchy. However the following, without the hierarchy, works fine (although I can't explain why the original version did not).
With
MEMBER [Measures].[MA3] AS
Avg(
[Date].[Month].CurrentMember.Lag(2)
: [Date].[Month].CurrentMember,
[Measures].[Project Views]
)
MEMBER [Measures].[C3] AS
Count(
[Date].[Month].CurrentMember.Lag(2)
: [Date].[Month].CurrentMember,
INCLUDEEMPTY
)
SELECT { [Measures].[Project Views], [Measures].[MA3] , [Measures].[C3] } ON 0,
[Date].[Calendar Years].[Month] ON 1
FROM [ProjectAccesses]

Pentaho Mondrian- Hide a dimension value from a role

I am using Pentaho Mondrian to build a billing cube with the dimensions "country" and "reference".
To one of my roles, I just want to show the reference value when country is equal to "Brazil". If it is not Brazil, reference should be null.
How it is:
http://i.stack.imgur.com/yipPJ.png
How it should be:
http://i.stack.imgur.com/PoF2w.png
How can I do this?
I tryed to use a calculated member with CASE WHEN, but it didnĀ“t work.
Should I use IIF instead?
I code this, but it is not working too:
WITH
MEMBER [dim_reference.Reference].[reference].Members AS
IIF
(
[dim_country.country].[country].CurrentMember = 'Brazil'
,[dim_reference.Reference].[reference].MEMBERS
,''
)
SELECT
NON EMPTY
{Hierarchize({[dim_country.country].[country].MEMBERS})} ON COLUMNS
,NON EMPTY
Order
(
{
Hierarchize({[dim_reference.Reference].[reference].MEMBERS})
}
,[dim_reference.Reference].CurrentMember.Name
,BASC
) ON ROWS
FROM [billing_entry];
Does someone knows how to do this?
A few pointers:
1
So you need to use the IS operator if you want to check for equality with a member.
Rather than this:
[dim_country.country].[country].CurrentMember = 'Brazil'
You use the following - you might need to amend the RHS with the exact full name of your Brazil member:
[dim_country.country].[country].CurrentMember
IS [dim_country.country].[country].[country].[Brazil]
2
Rather than using '' you should use NULL.
Rather than this:
IIF(
<condition>
,<if condition true>
,''
)
You should use this:
IIF(
<condition>
,<if condition true>
,NULL
)
3
You're correct to use IIF rather than CASE if at all possible - IIF generally performs better.
From your screenshots it looks to me that you want Countries ON ROWS rather than ON COLUMNS:
SELECT
NON EMPTY
{} ON COLUMNS
,NON EMPTY
[dim_country.country].[country].MEMBERS ON ROWS
FROM [billing_entry];
Now using SSAS against the AdvWrks cube I can do this:
WITH
MEMBER [Measures].[X] AS
IIF
(
[Customer].[Customer Geography].CurrentMember
IS
[Customer].[Customer Geography].[Country].&[Australia]
,[Product].[Product Categories].CurrentMember.Name
,null
)
SET [s] AS
[Customer].[Customer Geography].[Country]
*
[Product].[Product Categories].[category]
MEMBER [Product].[Product Categories].[All].[calc] AS
[Product].[Product Categories].CurrentMember.Name
SELECT
{[Measures].[X]} ON 0
,[s] ON 1
FROM [Adventure Works];
Which results in this:

MDX - Calculated Member with EXCEPT will not roll-up at all?

I have a Calculated Member were I want to aggregate a measure by YTD. I also want the measure to roll-up on all dimensions. There is one dimension value that I dont want to be part of the aggregation. The problem is when I try to filter this out the aggregation doesnt roll-up on the other dimensions.
This works and returns the correct value for all values in the [EK DIM OBJ KONTO] dimension, and rolls-up fine on all dimensions:
Aggregate
(
YTD([DATUM D].[Period].CurrentMember)
,[Measures].[UTFALL V]
)
When I try to add an EXCEPT-statement to exclude one value in the [EK DIM OBJ KONTO] dimension I get the wrong result. The Calculated Member always returns the same answer, it does not take any notice of the dimensions I am trying to slice it with.
Aggregate
(
{YTD([DATUM D].[Period].CurrentMember)}
*
{
Except
(
[EK DIM OBJ KONTO].[KTOB2 ID].[KTOB2 ID]
,[EK DIM OBJ KONTO].[KTOB2 ID].&[-]
)
}
,[Measures].[UTFALL V]
)
This is against the AdvWorks cube but seems to be working:
WITH
MEMBER [Measures].SumYtd AS
Aggregate
(
{YTD()}
,[Measures].[Internet Sales Amount]
)
MEMBER [Measures].SumException AS
Aggregate
(
{YTD()}
*
{
Except
(
[Customer].[Customer Geography].[Country].MEMBERS
,[Customer].[Customer Geography].[Country].&[United States]
)
}
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].SumException
,[Measures].SumYtd
} ON 0
,[Date].[Calendar].[Month].&[2007]&[11].Children ON 1
FROM [Adventure Works];