How to combine measures with different dimensionality into single select statement using MDX & Analysis Services (SSAS)? - 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]

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

Create Calculate Measures in SSAS

Please consider this scenarios:
I have a cube with one Fact table and one measure called SalesAmount . Now I want to create a measure based on these 2 Selects:
Select 1:
Select [Measures].[SalesAmount]
From MyCube
Where [Product].[Color].[Gray]
and Select 2:
Select [Measures].[SalesAmount]
From MyCube
Where [Dates].[Calendar Year].&[2015]
The problem is in Add Calculate Member there is a box for simple formula. How can I create a measure for Select 1 + Select 2?
Thanks
I am not sure where are you looking at this Add Calculate Member option, but you could try something like this (Adventure Works database).
with member measures.[MyMeasure1]
as
([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].&[2006])
member measures.[MyMeasure2]
as
([Measures].[Internet Sales Amount], [Product].[Color].&[Grey])
member measures.[MyMeasure12]
as
measures.[MyMeasure1] + measures.[MyMeasure2]
select {[Measures].[Internet Sales Amount], measures.[MyMeasure1], measures.[MyMeasure2] , measures.[MyMeasure12]} on 0
from [Adventure Works]
You can also create those members from Visual Studio Data Tools - in Calculations tab:

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

Mondrian dont use aggregate tables for average measure

I found that mondrian dont use aggregate table for average measure if WHERE is exist. How to be with it?
SELECT [Measures].[Avg measure] on COLUMNS,
VisualTotals(Hierarchize({Set of dimension #1})) om ROWS
FROM [Cube name]
WHERE {set of dimension #2}
I'm not sure, but you may give a try to the sub query:
SELECT [Measures].[Avg measure] on COLUMNS,
VisualTotals(Hierarchize({Set of dimension #1})) om ROWS
FROM [Cube name]
WHERE (select from [Cube name] where {set of dimension #2})

MDX to normalize set of measures

I am trying to create a dataset for an SSRS report as documented here:
http://sqlblog.com/blogs/stacia_misner/archive/2010/10/08/29249.aspx
The challenge is that I have multiple measures who's data I want to include in the measure column and I want to include the name of the measure in the RowValue column. So where the following query returns only data for measure "Sales Amount":
with
member [Measures].[Measure] as [Measures].[Sales Amount]
member [Measures].[RowValue] as [Product].[Category].CurrentMember.Name
member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
select {[Measures].[Measure], [Measures].[RowValue], [Measures].[ColumnValue]} on columns,
non empty ([Product].[Category].[Category].Members, [Date].[Calendar Year].[Calendar Year].Members) on rows
from [Adventure Works]
What I want to do is run the following type of query but have the data returned in the structure of the query above which would allow me to plug it into an SSRS report matrix:
WITH
MEMBER measures.SalesAmount AS [Measures].[Sales Amount]
MEMBER measures.CustomerCount AS [Measures].[Customer Count]
MEMBER measures.InternetFreightCost AS [Measures].[Internet Freight Cost]
SELECT [Date].[Calendar Year].[Calendar Year].Members ON COLUMNS,
{measures.SalesAmount,measures.CustomerCount,measures.InternetFreightCost} ON ROWS
FROM [Adventure Works]
Do any of the MDX ninjas know if this is even possible with MDX?
with member [Geography].[City].[Sales Amount] as 1
member [Geography].[City].[Customer Count] as 1
member [Geography].[City].[Freight Cost] as 1
member [Measures].[RowValue] as [Geography].[City].CurrentMember.Name
member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
member [Measures].[Measure] as
CASE
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Sales Amount]
THEN ([Measures].[Internet Sales Amount], [Geography].[City].[All Geographies])
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Customer Count]
THEN ([Measures].[Customer Count], [Geography].[City].[All Geographies])
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Freight Cost]
THEN ([Measures].[Internet Freight Cost], [Geography].[City].[All Geographies])
END
select {[Measures].[RowValue], [Measures].[ColumnValue], [Measures].[Measure]}
on columns,
{ [Geography].[City].[Sales Amount], [Geography].[City].[Customer Count], [Geography].[City].[Freight Cost]}
*
[Date].[Calendar Year].[Calendar Year].Members
having [Measures].[Measure] <> null
on rows
from [Adventure Works]
should deliver what you want. I used [Geography].[City] as an utility hierarchy. This can be any hierarchy unused in the query. I chose this one, as it is unrelated to both measure groups used in the query, and hence very unlikely to be used in any query. Some Cube designers create one or two one-member dummy dimensions in their cubes that are unrelated to any measure group, and can be used just like here in order to create calculated members on them.
One of the difficulties with ReportingServices queries is that measures must always be in the columns, and no other hierarchy may be in the columns. Hence, if we want to have the measures in the rows, we must move them to another hierarchy. This is done in two steps: First, we create dummy members on the utility hierarchy, and then map these to the measure needed in the CASE construct of the [Measures].[Measure] definition, where we need to use the default member of the utility dimension (in most cases the All member) in order to get something different than the 1 that I used for the dummy value.
Finally: non empty does not work properly with this construct, as [Measures].[RowValue] and [Measures].[ColumnValue] are never null. Hence I replaced it by HAVING, which can look at specific column values within the row.