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

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

Related

Calculating Cumulative Values MDX

I was trying to calculate cumulative values for the measure Closed WIT History
for the Month level, but had no luck
This is the Time dimension I have:
And this is my query:
WITH MEMBER [MEASURES].[Sales To Date]
AS
Sum
(
PeriodsToDate([DWH Dim Date].[(All)]),[Measures].[Closed WIT History]
)
SELECT
{[Measures].[Closed WIT History], [MEASURES].[Sales To Date]} ON COLUMNS,
[DWH Dim Date].[Month Name].[Month Name].Members ON ROWS
FROM [CUBE_Rel_1_AED_SAFe]
where [DWH Dim Date].[Year].&[2018]
But, for some reason, I get these results:
What am I doing wrong? I've also tried this Query:
WITH
MEMBER [MEASURES].[Sales To Date] as
SUM(
{[DWH Dim Date].CurrentMember.Level.Members}.Item(0):[DWH Dim Date].CurrentMember,[Measures].[Closed WIT History]
)
SELECT
{[Measures].[Closed WIT History],[Measures].[Sales To Date]} ON COLUMNS,
[DWH Dim Date].[Month Name].[Month Name].Members ON ROWS
FROM [CUBE_Rel_1_AED_SAFe]
where [DWH Dim Date].[Year].&[2018]
But then I get error like so:
You could click on #Error to see the detailed information. In addition, you also could use expression like below to achieve your goal
with member[Measures].[S2] AS
sum(
{[Date].[Month Name].&[2005]&[January]:[Date].[Month Name].CurrentMember}
, [Measures].[Internet Sales Count])
select nonempty ([Date].[Month Name].[Month Name].members) on rows, nonempty ({[Measures].[S2],[Measures].[Internet Sales Count]}) on columns from [Analysis Services Tutorial]
Zoe

MDX calculation with children and parents (contribution to total)

I got a cube with cadastral information of some neighborhoods of a city. One of the measures is 'cadastral value': the value of the land of a neighborhood.
Now, the city is divided into districts and a district, into neighborhoods (hierarchy with two levels).
The question is: I need to calculate the percentage that the value of each neighborhood represents compared with the parent (the district) and with the total (city), and also the same about the district compared with the city.
How can I do it?
I tried:
WITH MEMBER
[Measures].[Percentage] AS
([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember
/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
SELECT
{[Measures].[Percentage]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre
Where the dimensions are:
Nbh : Neighborhood
Cad value: Cadastral value
What I get is "error" in every result value, and the explanation is "Current member () : argument (0) type mismatch: expected:'level|hierarchy|dimension', got:'measure'"
What am I doing wrong?
WITH MEMBER [Measures].[Percentage] AS
([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember
/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
There are two problems with this:
One, you are using a static set to compute the value of calculated member namely:
[Nbh].[Nbh].[Nbh]
This is NOT [Nbh].[Nbh].CURRENTMEMBER
Second and the bigger mistake: Using the currentmember on a measure. You can't do that. You can use Measures.CURRENTMEMBER but that's about it. It's best used on hierarchies which are used on axes.
That said, your approach is almost legit. You need to fetch the measure value for the parent.
This should work:
WITH MEMBER
[Measures].[Percentage] AS
DIVIDE
(
[Measures].[Cad value],
([Nbh].[Nbh].CURRENTMEMBER.PARENT,[Measures].[Cad value]) //Cad value for parent
)
SELECT
{[Measures].[Percentage]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre
Try the following:
WITH MEMBER [Measures].[Parent %] AS
IIF(
[Nbh].[Nbh].CURRENTMEMBER IS [Nbh].[Nbh].[All]
,1
,[Measures].[Cad value] /
(
[Measures].[Cad value],
[Nbh].[Nbh].CurrentMember.Parent
)
), FORMAT_STRING = 'Percent'
SELECT
{[Measures].[Parent %]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre;
Including the IIF is a defensive measure. If you switch to showing AllMembers of the complete hierarchy then the All member will be included. Here is an example using AdvWrks:
WITH
MEMBER [Measures].[Parent %] AS
IIF
(
[Product].[Subcategory].CurrentMember
IS
[Product].[Subcategory].[All Products]
,1
,
[Measures].[Internet Sales Amount]
/
(
[Measures].[Internet Sales Amount]
,[Product].[Subcategory].CurrentMember.Parent
)
)
,FORMAT_STRING = 'Percent'
SELECT
NON EMPTY
{[Measures].[Parent %]} ON 0
,NON EMPTY
[Product].[Subcategory].ALLMEMBERS ON 1
FROM [Adventure Works];
The above returns the following:
Without the IIF the expression is the following:
WITH
MEMBER [Measures].[Parent %] AS
[Measures].[Internet Sales Amount]
/
(
[Measures].[Internet Sales Amount]
,[Product].[Subcategory].CurrentMember.Parent
)
,FORMAT_STRING = 'Percent'
SELECT
NON EMPTY
{[Measures].[Parent %]} ON 0
,NON EMPTY
[Product].[Subcategory].ALLMEMBERS ON 1
FROM [Adventure Works];
We get the following because of lack of foresight:
The error is certainly because of this statement is incorrect:
WITH MEMBER
[Measures].[Percentage]
AS ([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
Try to make it like this
WITH MEMBER
[Measures].[Percentage]
AS ([Nbh].[Nbh].[Nbh],[Measures].[Cad value])/([Nbh].[Nbh].[Nbh],[Measures].[Cad value])

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];

How to combine measures with different dimensionality into single select statement using MDX & Analysis Services (SSAS)?

I would like to add a measure that has different dimensionality to the same SELECT mdx statement. In general, most of my measures are linked to [Customer Creation Date].[Date Hierarchy].[Month Name].
However, Anonymous customers do not have the link to customer creation date; therefore, I have to link anonymous to Enquiry Creation Date. How to combine two measures into single select query.
> WITH MEMBER [Measures].[Allow Contact] as
([Measures].[# CRM Customers],[Customer].[Customer Allow Contact].&[Yes],[Customer].[Customer Status].&[Active] )
MEMBER [Measures].[Total Identified Customers] AS
([Measures].[# CRM Customers],[Customer].[Customer Status].&[Active] )
MEMBER [Measures].[Anonymous Customers] AS
([Measures].[# Unique Distinct Customers on Contact],[Enquiry].[Anonymous].&[Yes])
SELECT NON EMPTY { Measures].[Allow Contact],
[Measures].[Total Identified Customers]
//,[Measures].[Anonymous Customers]
} ON COLUMNS
,NON EMPTY ([Customer Creation Date].[Date Hierarchy].[Month Name]
//,[Enquiry Creation Date].[Date Hierarchy].[Month Name] //How to add different dimensionality
) ON ROWS
FROM [Cube]
If both time dimensions have the same structure (which should obviously be the case if they are implemented as role playing dimensions on the same dimension object), you can use the LinkMember function in the definition of [Measures].[Anonymous Customers] to use the [Customer Creation Date].[Date Hierarchy] in the rows, but use these dates as the [Enquiry Creation Date].[Date Hierarchy] in the measure calculation:
WITH MEMBER [Measures].[Allow Contact] as
([Measures].[# CRM Customers],[Customer].[Customer Allow Contact].&[Yes],[Customer].[Customer Status].&[Active] )
MEMBER [Measures].[Total Identified Customers] AS
([Measures].[# CRM Customers],[Customer].[Customer Status].&[Active] )
MEMBER [Measures].[Anonymous Customers] AS
([Measures].[# Unique Distinct Customers on Contact],[Enquiry].[Anonymous].&[Yes],
LinkMember([Customer Creation Date].[Date Hierarchy].CurrentMember, [Enquiry Creation Date].[Date Hierarchy])
SELECT NON EMPTY { Measures].[Allow Contact],
[Measures].[Total Identified Customers]
,[Measures].[Anonymous Customers]
} ON COLUMNS
,NON EMPTY ([Customer Creation Date].[Date Hierarchy].[Month Name]
) ON ROWS
FROM [Cube]

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

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] )
)