This article gives a method for ignoring null when applying RANK : http://www.bidn.com/blogs/CraigLove/ssas/2617/mdx-walkthrough
Is there a more elegant way of doing this than using CASE ?
WITH
MEMBER [Measures].[City Rank]
AS
CASE
WHEN
NOT ISEMPTY
(
(
[Geography].[City].CurrentMember
,[Measures].[Reseller Sales Amount]
)
)
THEN
RANK
(
[Geography].[City].CurrentMember
,[Geography].[City].AllMembers
,[Measures].[Reseller Sales Amount]
)
ELSE
NULL
END
SET [OrderedCity]
AS
ORDER
(
[Geography].[City].AllMembers
,[Measures].[Reseller Sales Amount]
,DESC
)
SELECT
{
[Measures].[City Rank]
,[Measures].[Reseller Sales Amount]
} ON COLUMNS
,NON EMPTY [OrderedCity] ON ROWS
FROM [Adventure Works];
You could apply the NonEmpty function within OrderedCity:
WITH
SET [OrderedCity]
AS
NonEmpty(
ORDER
(
[Geography].[City].AllMembers
,[Measures].[Reseller Sales Amount]
,DESC
)
)
MEMBER [Measures].[City Rank]
AS
RANK
(
[Geography].[City].CurrentMember
,[OrderedCity]
)
SELECT
{
[Measures].[City Rank]
,[Measures].[Reseller Sales Amount]
} ON COLUMNS
,[OrderedCity]
ON ROWS
FROM [Adventure Works];
This way the NON EMPTY is applied on the set on the rows before it it put there, so there is no need to apply it on the rows themselves. Furthermore, re-using the set within the definition of City Rank allows Analysis Services to calculate the sorting only once, and cache the ordered set. Otherwise, the Rank would require the ordering to be re- calculated for each row.
Related
I have an existing SSRS report out of T-SQL query and I am trying to recreate it using MDX queries on SSAS cube. I am stuck with rewriting Row num and rank logic to MDX.
It is written as:
SELECT ceil((ROW_NUMBER() OVER (PARTITION BY PRODUCT ORDER BY YEARMONTH))/12)
Rank1 in the SQL. Can someone tell me if this can be done using MDX? In the cube, PRODUCT and YEARMONTH are coming from separate dimensions.
Thank you for your help!
There is the Rank() function. For example:
with
Dynamic Set OrderedSet as
Order(
NonEmptyCrossJoin(
[Date].[Year].[Year].Members,
[Product].[Product].[Product].Members,
[Measures].[Invoice Count],
2
),
[Measures].[Invoice Count],
BDESC
)
Member [Measures].[Rank] as
Rank(
([Client].[Client].Currentmember,[Date].[Year].CurrentMember),
OrderedSet
)
select {[Measures].[Invoice Count],[Measures].[Rank]} on 0,
non empty OrderedSet on 1
from [BI Fake]
where ([Date].[Day].&[20160120])
You can read in details about it from my blog post.
You can use Generate to repeat ranks like this:
WITH
SET [SalesRank] AS
Generate
(
[Customer].[Customer Geography].[State-Province]
,Order
(
NonEmpty
(
(
[Customer].[Customer Geography].CurrentMember
,[Product].[Product Categories].[Subcategory]
)
,{[Measures].[Internet Sales Amount]}
)
,[Measures].[Internet Sales Amount]
,BDESC
)
)
MEMBER [Measures].[CategoryRank] AS
Rank
(
(
[Customer].[Customer Geography].CurrentMember
,[Product].[Product Categories].CurrentMember
)
,Exists
(
[SalesRank]
,[Product].[Product Categories].CurrentMember
)
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[CategoryRank]
} ON 0
,[SalesRank] ON 1
FROM [Adventure Works];
It results in this:
I have a measure, Sales Amount. I want to rank customers within a divison by year for that measure. I need to also display that rank as a measure. The rank needs to start over every year. I am able to do customers by year and customers by division, but I can't seem to figure out how to combine them both so it iterates over both dimensions properly. Below is what I have for the customers by year. I have tried adding another Division set, creating another named set that I GENERATE with the YearsWithCustomers set, and RANK using that new named set. I seem to be super close to figuring this out but I think I am putting something in the wrong place. I got the idea to iterate over a set from one of Chris Webb's blogs, located here.
WITH
SET Years AS
TopPercent
(
[Sales and Forecast Date].[Calendar Year].[Year Number].MEMBERS
,100
,[Measures].[Sales Amount]
)
SET Customers AS
Filter
(
[Customer].[Customer Number].[Customer Number].MEMBERS
,
[Measures].[Sales Amount] > 0
)
SET YearsWithCustomers AS
Generate
(
Years
,Union
(
{[Sales and Forecast Date].[Calendar Year].CurrentMember}
,StrToSet
("
Intersect({},
{order(Customers,([Sales Amount],[Sales and Forecast Date].[Calendar Year].CurrentMember),desc)
as CustomerSet"
+
Cstr(Years.CurrentOrdinal)
+ "})"
)
)
,ALL
)
MEMBER [Measures].[Customer Rank] AS
Rank
(
[Customer].[Customer Number].CurrentMember
,StrToSet
("CustomerSet"
+
Cstr
(
Rank
(
[Sales and Forecast Date].[Calendar Year].CurrentMember
,Years
)
)
)
)
SELECT
{
[Customer Rank]
,[Measures].[Sales Amount]
} ON 0
,Order
(
Filter
(
(
YearsWithCustomers
,Customers
)
,
[Sales Amount] > 0
)
,[Sales Amount]
,desc
) ON 1
FROM [OrdersAndBudgets];
Here is what I currently have. I would expect to see 1, 2, 3, etc for the Rank measure. It should reset for each division for every year.
I like this sort of pattern:
WITH
SET [AllCountries] AS
[Country].[Country].MEMBERS
SET [AllProds] AS
[Product].[Product].[Product].MEMBERS
SET [Top5Prods] AS
Generate
(
[AllCountries] AS a
,{
(
a.CurrentMember
,[Product].[Product].[All]
)
+
//The top x prods
a.CurrentMember
*
TopCount
(
[AllProds]
,5
,[Measures].[Internet Sales Amount]
)
}
)
MEMBER [Product].[Product].[All].[Other Products] AS
Aggregate
(
[Country].CurrentMember * [Product].[Product].[Product].MEMBERS
-
[Top5Prods]
)
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,Hierarchize(
{
[Top5Prods]
,[AllCountries] * [Product].[Product].[All].[Other Products]
}
) ON ROWS
FROM [Adventure Works];
It returns the following:
There is quite an extensive thread here: Top X of Top Y with RestOf member where X and Y are hierarchies from different dimensions
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];
Let me describe the issue giving example from [Adventure Works] cube.
Following MDX returns count of 17473
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works])
WHERE ( [Sales Reason].[Sales Reason].&[1] -- price
and following returns count of 3515
SELECT NON EMPTY { [Measures].[Internet Order Count] } ON COLUMNS ,
[Internet Sales Order Details].[Sales Order Number] on ROWS
FROM [Adventure Works]
WHERE ( [Sales Reason].[Sales Reason].&[2]) -- on promotion
I would like to count [Sales Order Number] which are common in [Sales Reason].&1 and [Sales Reason].&[2]
SQL equivalent would be:
select count(distinct f.SalesOrderNumber)
from FactInternetSales f
join FactInternetSalesReason fs
on f.SalesOrderNumber = fs.SalesOrderNumber and f.SalesOrderLineNumber = fs.SalesOrderLineNumber
where fs.SalesReasonKey = 1 and fs.SalesOrderNumber in
(select SalesOrderNumber from FactInternetSalesReason fs1 where fs1.SalesReasonKey = 2)
-- sales reason 1 = 17473
-- sales reason 2 = 3515
-- common 1689
I got common count using following mdx:
WITH MEMBER [Measures].[common] AS count
( exists ( exists ([Internet Sales Order Details].[Sales Order Number].[Sales Order Number].Members,
[Sales Reason].[Sales Reason].&[1],"Internet Orders"
),
[Sales Reason].[Sales Reason].&[2],"Internet Orders"
)
)
SELECT NON EMPTY [Measures].[common] ON COLUMNS
FROM [Adventure Works]
-- 1689
But use of EXISTS is rather slow for my requirement. Please suggest an alternative.
Also please see related thread here
Thank you
Please try adding both reasons to the WHERE clause as a single set:
SELECT
NON EMPTY
{[Measures].[Internet Order Count]} ON COLUMNS
,[Internet Sales Order Details].[Sales Order Number] ON ROWS
FROM [Adventure Works]
WHERE
{
[Sales Reason].[Sales Reason].&[2]
,[Sales Reason].[Sales Reason].&[1]
};
Here is an alternative that runs faster, only looks at the common orders, and does not use the EXISTS function:
WITH
SET [AllOrders] AS
[Internet Sales Order Details].[Sales Order Number].[Sales Order Number].MEMBERS
SET [OrdersIntersection] AS
Intersect
(
NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[1]
,[Measures].[Internet Order Count]
)
}
)
,NonEmpty
(
[AllOrders]
,{
(
[Sales Reason].[Sales Reason].&[2]
,[Measures].[Internet Order Count]
)
}
)
)
MEMBER [Measures].[commonCount] AS
[OrdersIntersection].Count
SELECT
//NON EMPTY //<<not needed
[Measures].[commonCount] ON COLUMNS
FROM [Adventure Works];
The following is from MSDN: http://msdn.microsoft.com/en-us/library/ms144726.aspx
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
SELECT {[Measures].[City Rank],[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]
Would it be possible to use the GENERATE function to concatenate the rank and the city into a single column. So results would look like the following:
EDIT
So the following must be getting closer:
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
MEMBER [Measures].[memberName] AS
'[Geography].[City].CurrentMember.name'
MEMBER [Measures].[memberValue] AS
'[Measures].[City Rank].value'
MEMBER [Measures].[concat] AS
// [Measures].[memberName] + [Measures].[memberValue]//<<this errors
// '[Measures].[memberName] & [Measures].[memberValue]'//<<this errors
'[Measures].[memberName] + [Measures].[memberValue]'//<<this errors
SELECT
{[Measures].[City Rank],
[Measures].[memberName],
[Measures].[memberValue],
[Measures].[concat],
[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]
the following should work:
MEMBER [Measures].[concat] AS
cstr(RANK( [Geography].[City].currentmember, orderedcities)) +'.'+
[Geography].[City].currentmember.name
You will still have to use the presentation programm to get rid of the first redundant column.
Philip,