How to implement "greater than" based on dimension value in MDX - ssas

I would like to query the total sales of each country with the GDP growth rate of more than 3. So far, I can display the sales information of each country like such:
SELECT
{[Measures].[Sales]} ON 0,
{[Dim Company Info].[LOC].ALLMEMBERS} ON 1
FROM [Database]
But then I am still blank on how to query all GDP growth rate of more than 3. I have searched SO and found filter to be the answer, however, I do not know where to include it in my code above. How do I go about this?
Edit: I have tried the followings, but I do not think that is what I am supposed to do:
WHERE ([Dim Company Info].[Gdp Growth].&[3] : [Dim Company Info].[Gdp Growth].&[3].LastChild)
and
SELECT
{[Measures].[SALES]} ON 0,
{FILTER([Dim Company Info].[LOC].MEMBERS, [Dim Company Info].[Gdp Growth] > 3)} ON 1
FROM [766 Database]
and
SELECT
{[Measures].[SALES]} ON COLUMNS,
{[Dim Company Info].[LOC].MEMBERS} ON ROWS
FROM [766 Database]
WHERE FILTER([Dim Company Info].[Gdp Growth], [Dim Company Info].[Gdp Growth] > 2)

You can use your below expression when the dimension members are numericly arranged
WHERE ([Dim Company Info].[Gdp Growth].&3 : [Dim Company Info].[Gdp
Growth].&3.LastChild)
with a slight correction
WHERE ([Dim Company Info].[Gdp Growth].&[3] : [Dim Company Info].[Gdp Growth].[Gdp Growth].LastChild)
However if that is not the case, then you need to follow the below method.
I am reporting the product with the max quantity.
select {[Measures].[Internet Sales Amount]} on 0,
non empty
([Product].[Product].[Product],[Promotion].[Max Quantity].[Max Quantity]) on 1
from [Adventure Works]
Now lets manipulate the max quantity to make it behave like a measure.
with
member measures.test
as
case when [Measures].[Internet Sales Amount]>0 then
(nonempty(([Product].[Product].currentmember,[Promotion].[Max Quantity].[Max Quantity]),[Measures].[Internet Sales Amount])).item(0).item(1).name
else
null end
select
{[Measures].[Internet Sales Amount],measures.test}
on 0,
non empty ([Product].[Product].[Product] )
on 1
from [Adventure Works]
Now you can use the filter, but we need to cast(note the use of CInt) the data too
with
member measures.test
as
case when [Measures].[Internet Sales Amount]>0 then
Cint((nonempty(([Product].[Product].currentmember,[Promotion].[Max Quantity].[Max Quantity]),[Measures].[Internet Sales Amount])).item(0).item(1).name)
else
null end
select
{[Measures].[Internet Sales Amount],measures.test}
on 0,
non empty (
filter(
[Product].[Product].[Product]
,measures.test>0)
)
on 1
from [Adventure Works]

Related

MDX order doesn't work

I'm using the AdventureWorks Cube and MS SQL 2017 to learn MDX.
I'd like to return the top five selling subcategories of products, based on Internet Sales Amount:
SELECT Subset
(Order
([Product].[Product Categories].[SubCategory].members
,[Measures].[Internet Sales Amount]
,DESC
)
,0
,5
)
ON 0
FROM [Adventure Works]
The returned results are not sorted properly. Is there anything I did wrong?
Road Bikes Mountain Bikes Touring Bikes Tires and Tubes Helmets
$29,358,206.96 $26,492,684.38 $10,451,490.22 $925.21 $258,712.93
I also tried BDESC, seems to get the same result.
This query will help explain what happened. It was sorting correctly, but then it was displaying the default measure on the screen since you didn't specify the measure to display, only the measure to sort by:
SELECT {[Measures].[Internet Sales Amount], [Measures].DefaultMember} on 0,
Subset
(Order
([Product].[Product Categories].[SubCategory].members
,[Measures].[Internet Sales Amount]
,DESC
)
,0
,5
)
ON 1
FROM [Adventure Works]
I would recommend using that query but removing [Measures].DefaultMember and you should get the results you expect.
You want the TOPCOUNT MDX function
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
, TOPCOUNT([Product].[Subcategory].[Subcategory].MEMBERS,5,[Measures].[Internet Sales Amount]) ON ROWS
FROM
[Adventure Works]

MDX query for bottomcount function

SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
NonEmpty([Customer].[Customer].[Customer].MEMBERS)
,10
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[Calendar Year].&[2005];
The above query is showing me result having last 10 customer names with NULL measure value. I am using Adventure works Cube.
As per my understanding "bottomcount" is not working fine with where clause in "mdx" query. I want to fetch last 10 customer names for 2005 with measure value and hopefully i am looking for solution without using keyword "DESCENDANTS".
Please let me know in case, I am doing something wrong.
Give this a try:
bottomcount(
nonempty(
[Customer].[Customer].[Customer].Members
,[Measures].[Internet Sales Amount]
)
,10
,[Measures].[Internet Sales Amount]
)
You were getting the Customer names with NULL because in OLAP you are doing the cartesian product between the 2 dimensions (Measures is a special dimension, but it still acts as a dimension) it does not matter that you don't have values it will still crossjoin the members in your 2 sets and unless you request just the nonempty ones it will still give you the NULLs.
And if you'd like their full internet sales amount then move some of the logic into the WITH clause:
WITH
SET bot AS
NonEmpty
(
[Customer].[Customer].[Customer].MEMBERS
,(
[Measures].[Internet Sales Amount]
,[Date].[Calendar].[Calendar Year].&[2005]
)
)
SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,BottomCount
(
bot
,10
,[Measures].[Internet Sales Amount]
) 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]

SSAS - Sum of Top or Bottom

SELECT
[Measures].[Internet Sales Amount] ON COLUMNS
,Tail
(
[Date].[Calendar Year].[Calendar Year].MEMBERS
,2
) ON ROWS
FROM [Adventure Works];
Above MDX query gives me output as :
Year Internet Sales Amount
CY 2003 $9,791,060.30
CY 2004 $9,770,899.74
I understood how this query worked but I want to create Calculated measure in a cube which will always give me sum of bottom 2 years. How to do this? I am a newbie to SSAS. I am good at designing simple measures and dimensions but when it comes to using MDX, I am mostly stuck.
PS: I tried using TopCount, BottomCount etc but here I want to order by "Year", which is a dimension.
Any help would be appreciated.
Thanks,
Parry
The following query calculates a measure for sum the the last 2 years of the Date dimension:
WITH
MEMBER [Measures].[Sales from the last 2 Years]
AS Aggregate( Tail( [Date].[Calendar Year].[Calendar Year].Members, 2)
, [Measures].[Internet Sales Amount]
)
SELECT { [Measures].[Sales from the last 2 Years]
} ON COLUMNS
, { Tail( [Date].[Calendar Year].[Calendar Year].Members, 2)
} ON ROWS
FROM [Adventure Works]
Other interesting query, would be calculating a measure for the sum of each year and its previous:
WITH
MEMBER [Measures].[Sales from 2 years]
AS Aggregate( { [Date].[Calendar Year].CurrentMember.PrevMember
: [Date].[Calendar Year].CurrentMember }
, [Measures].[Internet Sales Amount]
)
SELECT { [Measures].[Internet Sales Amount]
, [Measures].[Sales from 2 years]
} ON COLUMNS
, NON EMPTY
{ [Date].[Calendar Year].[Calendar Year]
} ON ROWS
FROM [Adventure Works]
It does a sum, because the aggregation type of the measure [Measures].[Internet Sales Amount] is Sum, and the Aggregate function aggregates according to the measure's aggregation type.
Key Concepts in MDX
Aggregate function reference
MDX is a hard topic to grasp; if you're starting out, I recommend you read the book MDX Solutions, 2nd edition.

Comparison in Where clause in MDX Queries

In MDX Queries, How can i compare whether a level value is less than a certain value or not.
e.g.
SELECT NON EMPTY
[Sales Territory].[Sales Territory Country].Members ON 0,
[Product].[Category].[Clothing] ON 1
FROM
[Adventure Works]
WHERE
([Measures].[LowGPM] > 120)
This MDX query didn't work.
Also, i tried WITH Clause also.
WITH Member [Measures].[Calculated Measures] as
IIF ([Measures].[LowGPM] < 120, [Measures].[LowGPM], null)
SELECT NON EMPTY
[Sales Territory].[Sales Territory Country].Members ON 0,
[Product].[Category].[Clothing] ON 1
FROM
[Adventure Works]
WHERE
([Measures].[LowGPM] > 120)
It shows Mondrian Error:MDX object '[Measures].[Calculated Measures]' not found in cube.
You can filter the axis like this:
SELECT
[Measures].[Internet Sales Amount] ON 0,
Filter(
[Customer].[Country].Members,
([Measures].[Internet Sales Amount] > 2000000)
AND ([Measures].[Internet Sales Amount] < 5000000)
) ON 1
FROM [Adventure Works]