Using WITH in mdx to retrieve row names - mdx

I am executing the following MDX query in SSMS, which I have got from Profiler:
SELECT {
[Measures].[Dollar Amount],
[Measures].[Transaction Count]}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON COLUMNS,
NON EMPTY Hierarchize({DrilldownLevel({[Retail Sales Date].[Month].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM [Retail Sales Cube]
WHERE ([Geography].[Retail Sales Location].[Country Name].&[Australia],[Retail Category].[Retail Category].[All])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
This produces the following results (snippet only):
The rows are months, and because of the method of retrieval, I need the months to also be displayed in a column.
So I tried this:
WITH
MEMBER [TheDate] AS [Retail Sales Date].[Month].CurrentMember.Name
SELECT {
[TheDate],
[Measures].[Dollar Amount],
[Measures].[Transaction Count]}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON COLUMNS,
NON EMPTY Hierarchize({DrilldownLevel({[Retail Sales Date].[Month].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM [Retail Sales Cube]
WHERE ([Geography].[Retail Sales Location].[Country Name].&[Australia],[Retail Category].[Retail Category].[All])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
which would be fine, except that it also shows rows where there are null values, as follows:
I know there is NONEMPTY, but I don't know where I would put it. How can I get rid of the empty rows?

Perhaps the following expression would do the trick:
WITH
MEMBER [TheDate] AS
IIF( !isEmpty( [Dollar Amount] )
[Retail Sales Date].[Month].CurrentMember.Name,
null
)

Related

How do I reformat a measure in a mdx query

I have an MDX query running on SSAS instance. The amount that gets displayed as a currency with spaces and , in the column. eg R 1 200,10.
I do not want to change anything on the data warehouse but want to change the format in the MDX query so that the results are displayed in the column as 1200.10 (a straight numeric)
I have included the following code on top of the exiting MDX query that give the wrong format.
with
member [Measures].[Gross Amt Exc Base] as [Measures].[AMT] , FORMAT_STRING="###0.00"
This is the code i have created:
with
member [Measures].[Gross Amt Exc Base] as [Measures].[AMT] , FORMAT_STRING="###0.00"
SELECT NON EMPTY { [Measures].[Gross Amt Exc Base] } ON COLUMNS,
NON EMPTY {(StrToMember('[Tran Date].[Actual Date].&[' + Format(Now()-1,'yyyy-MM-dd') + 'T00:00:00]')
:StrToMember('[Tran Date].[Actual Date].&[' + Format(Now()-3,'yyyy-MM-dd') + 'T00:00:00]') *
[Dim Store].[Store Number].[Store Number].ALLMEMBERS *
[Dim SKU].[SKU Code].[SKU Code].ALLMEMBERS *
[Dim SKU].[Product Code].[Product Code].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Dolfin Dwh FTS] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I want to see 1200.10 but get the following error message
"Executing the query ...
The 'Gross Amt Exc Base' calculated member cannot be created because a member with the same name already exists.
Run complete"
Please can some one help me.

Calculated measure hurting performance and returning many more rows

I have the following query which should be returning income split by country and date. It should also be including a count of the number of dates in the grouping. This would be useful for a calculation looking at an entire month of income/days. I'm finding that the calculated measure is causing all countries in the dimension to be returned regardless of if there's any income data for it. Is there any way to limit this so only countries with income are returned?
with
MEMBER [Measures].[group_day_count] as
COUNT(
Descendants(
[Date].[Date].currentmember, [Date].[Date].[Date]
)
)
SELECT NON EMPTY Hierarchize({DrilldownLevel({[Date].[Date].[All]},,,INCLUDE_CALC_MEMBERS)})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME,
[Date].[Date].[Date].[Date Sort] ON COLUMNS ,
NON EMPTY CrossJoin(Hierarchize({DrilldownLevel({[Source Location].[Country Code].[All]},,,INCLUDE_CALC_MEMBERS)}),
{[Measures].[group_day_count],[Measures].[income]})
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON ROWS
FROM (SELECT ({[Date].[Date].&[2017-10-04], [Date].[Date].&[2017-10-05], [Date].[Date].&[2017-10-06], [Date].[Date].&[2017-10-07], [Date].[Date].&[2017-10-08], [Date].[Date].&[2017-10-09], [Date].[Date].&[2017-10-10]})
ON COLUMNS FROM [Placeholder])
CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
Try:
IIF([Measures].[Income],[Measures].[Days],NULL)

MDX Query - Select Columns From Same Dimensions

I have a requirement displaying data from same dimension in more than 1 column. For eg. I want to show data Year and Month wise. In my dimension structure, Year and Month belongs to same hierarchy. When I run below query I get error. PFB the query.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members *
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
The error I get is Query (2, 12) The Hierarchy hierarchy is used more than once in the Crossjoin function. I am new to MDX queries. Please help in this regard. Thanks in advance.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members ,
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
Instead of CROSSJOIN have a set as above. In a set, you can put members from same hierarchy
I like Sourav's answer - but it will put the results in one column which is slightly different than the question.
In AdvWorks this is in one column:
SELECT
[State-Province].MEMBERS ON COLUMNS
,{
[Date].[Calendar].[Calendar Year].MEMBERS
,[Date].[Calendar].[Month].MEMBERS
} ON ROWS
FROM [Adventure Works];
It is possible to switch to two columns and use a cross join but you need to find out the details of your Date dimensions Attribute hierarchies (as opposed to User hierarchies):
SELECT
[State-Province].MEMBERS ON COLUMNS
,
[Calendar Year].[All Periods].Children
* [Month].MEMBERS ON ROWS
FROM [Adventure Works];
In your cube maybe something like this:
SELECT
NON EMPTY
{[Measures].[Target Actual Value]} ON 0
,NON EMPTY
[Year Name].MEMBERS
*
[Month Year].MEMBERS ON 1
FROM [Cube_BCG_OLAP];

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.

MDX Query Optimization

I have a pivot table generated in Excel. I need to use the same MDX query which is used in Excel, I have extracted the same query from Excel which is like this -
SELECT {[Measures].[OQ],[Measures].[RQ],[Measures].[SQ],[Measures].[SRQ]}
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,HIERARCHY_UNIQUE_NAME ON COLUMNS ,
CrossJoin(
CrossJoin(
CrossJoin(
CrossJoin(
CrossJoin(
CrossJoin(
CrossJoin(
Hierarchize({DrilldownLevel({[PO].[Date].[All]},,,INCLUDE_CALC_MEMBERS)}),
Hierarchize({DrilldownLevel({[PO].[Priority].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[POL].[Container].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[PO].[Name].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[POL].[Num].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[PO].[Warehouse].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[POL].[Status].[All]},,,INCLUDE_CALC_MEMBERS)})),
Hierarchize({DrilldownLevel({[POL].[Factor].[All]},,,INCLUDE_CALC_MEMBERS)}))
ON ROWS
FROM [Purchases] CELL PROPERTIES VALUE, FORMAT_STRING, LANGUAGE, BACK_COLOR, FORE_COLOR, FONT_FLAGS
This query returns about 3 lac records in excel, when I run this query in SSMS it gives error like "Server: The operation has been cancelled due to memory pressure.".
I am new to MDX, above query uses many CrossJoins, is there any way to optimize this query?
Thanks and Regards,
Amit Thakkar
I think its result maybe unusable almost for statistic reason
because of returning many rows.
but for optimization I suggest using
NONEMPTY
front of your crossjoin