MDX: What is the difference of these both queries - mdx

MDX: What is the difference of these queries, I'm using adventure works database.
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
NON EMPTY ([Product].[Category].[Category] ,
[Date].[Calendar Year].[Calendar Year])ON ROWS
FROM [Adventure Works]
GO
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
NON EMPTY ([Product].[Category].[Category] *
[Date].[Calendar Year].[Calendar Year])ON ROWS
FROM [Adventure Works]

Those two scripts are the same.
This is effectively putting a set of tuples on the rows - you've definied the tuple explicitly using the braces (...)
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,NON EMPTY
( //<<start of tuple
[Product].[Category].[Category]
,[Date].[Calendar Year].[Calendar Year]
) ON ROWS
FROM [Adventure Works];
The second script is a cross join via the star operator *:
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,NON EMPTY
[Product].[Category].[Category] * [Date].[Calendar Year].[Calendar Year] ON ROWS
FROM [Adventure Works];
When you action a crossjoin it returns a set of tuples - so you end up with the same cell set.
You could write crossjoin instead of using the star - although the star is the most used notation in modern mdx scripts:
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,NON EMPTY
CrossJoin
(
[Product].[Category].[Category]
,[Date].[Calendar Year].[Calendar Year]
) ON ROWS
FROM [Adventure Works];

Related

I'm stuck with MDX query

i need sales amount of first month of each quarter
WITH SET [FIRSTMONTHOFQTR] AS
DESCENDANTS(
DESCENDANTS(
[Date].[Calendar].CURRENTMEMBER,
[Date].[Calendar].[Calendar Quarter]
),
[Date].[Calendar].[Month]
)
SELECT {
[Measures].[Sales Amount]
} ON COLUMNS,
{
[FIRSTMONTHOFQTR]
} ON ROWS
FROM [Adventure Works];
With above im getting each month but i need only first month. how can i filter that?
This is one approach:
WITH
SET [Qtrs] AS
[Date].[Calendar].[Calendar Quarter]
SET [FIRSTMONTHOFQTR] AS
Generate
(
[Qtrs] AS s
,Head(Descendants(s.CurrentMember,[Date].[Calendar].[Month]))
)
SELECT
{[Measures].[Sales Amount]} ON COLUMNS
,[FIRSTMONTHOFQTR] ON ROWS
FROM [Adventure Works];
It returns the following:

How to apply Hierarchize and Order by with MDX Query

I need to order Dimension with respect to descending order. without using HIERARCHIZE key word everything works fine. here i need HIERARCHIZE in order to order hierarchy level data.
Select NON EMPTY({[Measures].[Internet Sales Amount]}) dimension properties MEMBER_TYPE,CHILDREN_CARDINALITY, PARENT_UNIQUE_NAME ON COLUMNS ,NON EMPTY(HIERARCHIZE({{ORDER(drilldownlevel([Customer].[Customer Geography]),[Customer].[Customer Geography].CurrentMember.MEMBER_CAPTION,desc)}})) dimension properties MEMBER_TYPE,CHILDREN_CARDINALITY, PARENT_UNIQUE_NAME ON ROWS
Unfortunely I do not have AdvWrks cube to test the following:
SELECT
NON EMPTY
[Measures].[Internet Sales Amount] ON 0
,NON EMPTY
ORDER(
{
HIERARCHIZE([Customer].[Customer Geography].[COUNTRY].MEMBERS)
,[Customer].[Customer Geography].[COUNTRY].&[GERMANY].CHILDREN
}
,[Customer].[Customer Geography].CurrentMember.MEMBER_CAPTION
,BDESC
)
) ON 1
FROM [Adventure Works];
Looks like I had a tested solution to a similar problem here:
Issue with Order function and Crossoins in MDX
Looks like an application of the above to your context is something like this:
SELECT
NON EMPTY
[Measures].[Internet Sales Amount] ON 0
,NON EMPTY
{
Order
(
{
[Customer].[Customer Geography].[COUNTRY].MEMBERS
, [Customer].[Customer Geography].[COUNTRY].&[GERMANY].CHILDREN
}
,(
[Measures].[Internet Sales Amount]
,[Customer].[Customer Geography].[COUNTRY]
)
,BDESC
)
} ON 1
FROM [Adventure Works];
Resolved the issues with below query
SELECT
NON EMPTY [Measures].[Internet Sales Amount] ON 0,
NON EMPTY
Order(
Hierarchize(
[Customer].[Customer Geography].[Country].&[Germany].Children
)
,[Customer].[Customer Geography].CurrentMember.MEMBER_CAPTION
,DESC
)
ON 1
FROM [Adventure works];

Creating a measure by filtering out a set from an existing measure

I am trying to implement something as follows:
WITH MEMBER Measures.Test2 AS
Sum
(
{
[Date].[Calendar].[Calendar Year].&[2006]
,[Date].[Calendar].[Calendar Year].&[2007]
}
,[Measures].[Internet Sales Amount]
)
SELECT Measures.Test2 ON COLUMNS
FROM [Adventure Works];
But i want the new measure Test2 to be sliceable according to the Calendar Year dimension. So i want something like
SELECT {Measures.Test2} ON 0,
{[Date].[Calendar].[Calendar Year].[Calendar Year].MEMBERS} ON 1
FROM [Adventure Works];
This is giving the same value for both the years 2006 and 2007.
In essence i want to create a member by taking a subset of an existing measure and then using it for further calculations
This script is not valid mdx:
SELECT (Measures.Test2,[Date].[Calendar].[Calendar Year].[Calendar Year] ON COLUMNS
FROM [Adventure Works];
You have a single ( before Measures.
Also you look like you're about to add a tuple ON COLUMNS which is not allowed. Only sets are allowed on rows and columns:
SELECT
{Measures.Test2} ON 0,
{[Date].[Calendar].[Calendar Year].[Calendar Year].MEMBERS} ON 1
FROM [Adventure Works];
Try the following:
WITH
MEMBER Measures.Test2 AS
Sum
(
Intersect
(
{[Date].[Calendar].CurrentMember}
,{
[Date].[Calendar].[Calendar Year].&[2006]
,[Date].[Calendar].[Calendar Year].&[2007]
}
)
,[Measures].[Internet Sales Amount]
)
SELECT
[Measures].test2 ON COLUMNS
,{[Date].[Calendar].[Calendar Year].MEMBERS} ON ROWS
FROM [Adventure Works];
The above returns this:
Or maybe all you want is a subselect:
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,{[Date].[Calendar].[Calendar Year].MEMBERS} ON ROWS
FROM
(
SELECT
{
[Date].[Calendar].[Calendar Year].&[2006]
,[Date].[Calendar].[Calendar Year].&[2007]
} ON 0
FROM [Adventure Works]
);

How to get top 2 count record and rest total(others) using two dimension in same mdx

i have this query, i need to implement for two dimension
WITH
SET [TCat] AS
TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
MEMBER [Product].[Subcategory].[Other] AS
Aggregate([Product].[Subcategory].[Subcategory] - TCat)
SELECT { [Measures].[Sales Amount] } ON COLUMNS,
TCat + [Other] ON ROWS
FROM [Adventure Works]
I try but it is not working for two dimension
WITH
SET FIPS as
[Geography].[State-Province].[State-Province]
//TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
SET [TCat] AS
Generate( {FIPS}, CrossJoin( {[Geography].[State-Province].CurrentMember}, topsum( ([Product].[Subcategory].[Subcategory]), 2, [Measures].[Sales Amount] ) ))
MEMBER [Product].[Subcategory].[Other] AS
Aggregate(Except( [Product].[Subcategory].[Subcategory].Members, TCat))
SELECT
{ [Measures].[Sales Amount] } ON COLUMNS,
Union(
TCat , {[Geography].[State-Province].[State-Province],[Product].[Subcategory].[Other]} ) ON ROWS
FROM [Adventure Works];
Try using Except.
WITH
SET [TCat] AS
TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
MEMBER [Product].[Subcategory].[Other] AS
Aggregate(Except( [Product].[Subcategory].[Subcategory].Members, TCat)
SELECT { [Measures].[Sales Amount] } ON COLUMNS,
Union( TCat , {[Product].[Subcategory].[Other]} ) ON ROWS
FROM [Adventure Works]

How to get an "(all)" value for a level in a user defined hierarchy in SSAS/MDX

I am trying to work out how I can get a total for a level in a user defined hierarchy. For example if I do (using Adventure works here):
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].ALLMEMBERS * [Date].[Month of Year].ALLMEMBERS) ON 1
FROM [Adventure Works]
I get totals for each pair of year/month values.
How can I rewrite this query using the Calendar user hierarchy in the Date dimensions? Eg something like:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar].[Year] * [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
You can use
SELECT [Measures].[Internet Sales Amount] ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
This will show you each year containing its value (which is the sum of the months contained in it) before the months.
The + is the short form of Union in MDX, and Hierarchize sorts a set in hierarchical order, which means parents before children.
And if you want something similar to your first result, the closest thing I can imagine would be via calculated measures like this:
WITH Member Measures.Year AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Calendar Year],
[Date].[Calendar].CurrentMember.Name,
Ancestor([Date].[Calendar].CurrentMember, [Date].[Calendar].[Calendar Year]).Name
)
Member Measures.Month AS
IIf([Date].[Calendar].CurrentMember.Level IS [Date].[Calendar].[Month],
[Date].[Calendar].CurrentMember.Name,
'All Months'
)
SELECT {
Measures.Year,
Measures.Month,
[Measures].[Internet Sales Amount]
}
ON 0,
Hierarchize([Date].[Calendar].[Calendar Year] + [Date].[Calendar].[Month]) ON 1
FROM [Adventure Works]
Note that calculated members need not return numbers, they can as well return strings.
Using allmembers as you have it should return you the ALL line and then each year and each month. If you are interested in only the all line you can change you query to:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year] * [Date].[Month of Year]) ON 1
FROM [Adventure Works]
Notice how it is specifying only the Dimension and the hierarchy.
If you wanted to get the data without the all line use the following:
SELECT [Measures].[Internet Sales Amount] ON 0,
([Date].[Calendar Year].children * [Date].[Month of Year].children) ON 1
FROM [Adventure Works]