Tuesday, November 3, 2015

Sql "Like" in MDX

If you are looking for a Like keyword in MDX then you are not going to get that but if you want to write MDX which should work like "LIKE" keyword in MDX then you can achieve that by writing MDX in the following way;

Just consider an example wherein you want to show all Members from Calendar hierarchy like "March",  in that case you can write mdx in following way;

WITH
SET CalendarMembers
AS
FILTER(
      DESCENDANTS([Date].[Calendar]),
      vbamdx!INSTR([Date].[Calendar].CURRENTMEMBER.Name,'March',1 >= 1 )
)
MEMBER [Measures].[Caption] AS
    [Date].[Calendar].CURRENTMEMBER.NAME  
SELECT
{[Measures].[Caption]} ON COLUMNS
,
CalendarMembers 
ON ROWS
FROM [Admin]

Output : Above MDX will return all members with caption like "March"



Tuesday, June 9, 2015

MDX Order Function

Order MDX function is used to show the result set in specified order. Generally we set OrderBy property of key attribute as Key. Sometime we want to define the customized order wherein we can create another attribute on different column which store Order like 1,2,3.....and then we can set OrderBy property of KeyAttribute as "AttributeKey" and specify the attribute name (which stores customized ordering) under "OrderByAttribute" property.

Consider an example wherein you have set the OrderBy property of KeyAttribute as "Key" and while displaying result set, you want to show the members in different orders....in such cases you can use Order MDX function. Consider following example build using AdventureWorks sample;

Open AdventureWorks sample and execute following MDX query

SELECT 
{[Measures].[Internet Sales Amount]} 
ON COLUMNS,
DESCENDANTS([Product].[Product Categories],[Product].[Product Categories].[Category],SELF) 
ON ROWS
FROM [Adventure Works]

After execution, you will get following result set;



Above result set is showing Ordering by Name because OrderBy property of Category attribute is set as Name.If you want to show ordering Descending by Name while showing MDX results then you can use Order function in the following way;

SELECT 
{[Measures].[Internet Sales Amount]} 
ON COLUMNS,
Order
  (
DESCENDANTS([Product].[Product Categories],[Product].[Product Categories].[Category],SELF) ,
[Product].[Product Categories].CURRENTMEMBER.MEMBER_NAME,
DESC
  )
ON ROWS
FROM [Adventure Works];

After executing above MDX, you will get following result set Ordered by Name DESC


Now consider you want to show the results as per Key Order, in such case you can modify your MDX in following way;

SELECT 
{[Measures].[Internet Sales Amount]} 
ON COLUMNS,
Order
   (
DESCENDANTS([Product].[Product Categories],[Product].[Product Categories].[Category],SELF) ,
[Product].[Product Categories].CURRENTMEMBER.MEMBER_KEY,
ASC
)
ON ROWS
FROM [Adventure Works];

After executing above MDX, you will get following result set Ordered by KEY ASC


If you specify DESC in above MDX, you will get result set Ordered by KEY DESC

Thursday, April 30, 2015

MDX Scope Statement

Recently someone asked me what is the use of SCOPE statement in SSAS Calcultions tab, I shared one example with that guy to understand the basics of Scope statement. I thought, I should share the same so if anyone wants to understand Scope statement then they can refer this post.

Scope statement is used to limit the scope of specified MDX to a specified subcube i.e. you can specifiy the behaviour for subcube.

Consider Date dimension and Calendar hierarchy from AdvantureWorks sample, Consider, you want to multiply your measure value if user select Month level members. We are taking [Measures].[Internet Tax Amount] and Calendar hierarchy in MDX.

Open SSMS and connect to Anlysis services and execute following MDX query.

SELECT
NON EMPTY [Measures].[Internet Tax Amount] ON COLUMNS,
NON EMPTY
DESCENDANTS([Date].[Calendar].[Calendar Year].&[2008],[Date].[Calendar].[Month],SELF)
ON ROWS
FROM [Adventure Works]

When you execute above query, you will get following results;












Values displayed in above snap are actual values coming from fact. Since Facts are at Date granularity, it is getting aggregated at higher level and displayed at Month level but consider client wants to show double the value if they see at Month level. So in such cases you can use Scope statement so SSAS engine can show expected values at Month level.

Put following Scope statement in Calculations Tab and save changes. We are passing Month level from Calendar hierarchy and [Internet Tax Amount] because we want to show double to same measure at Month level. This returns the current subcube.

Scope 
  ( 
     [Date].[Calendar].[Month],     
     [Measures].[Internet Tax Amount]
  );    

    This = [Measures].[Internet Tax Amount] * 2;    

  End Scope;

Once saved, execute same MDX query and you will find results are doubled up.



Wednesday, April 8, 2015

Date Difference using MDX VBA functions


Recently I came across one project wherein I see lots of calculated measures build on the top of Date difference and I see finding number of days, number of months, number of years is very common in most of the requirements hence thought of sharing the same.

I am demonstrating samples using AdventureWorks sample;

1. If you want to calculate Days between two supplied dates

In this sample, I am passing Date dimension member and current date and finding Days between two dates supplied.

With Member [Measures].[MemberKey] As
[Date].[Calendar].CURRENTMEMBER.MEMBER_KEY
Member [Measures].[DateFormat] as
VBA!Cdate(
VBA!Mid([Measures].[MemberKey],5,2) + '/' +
VBA!Mid([Measures].[MemberKey],7,2) + '/' +
VBA!Left([Measures].[MemberKey],4)
)
Member [Measures].[CurrentDate] As
Format(Now(),"M/d/yyyy")
Member [Measures].[Date Diff] As
DateDiff ("d", [Measures].[DateFormat],[Measures].[CurrentDate])
SELECT
{[Measures].[MemberKey],[Measures].[DateFormat],[Measures].[CurrentDate],[Measures].[Date Diff]} ON 0,
DESCENDANTS([Date].[Calendar],[Date].[Calendar].[Date],SELF) ON 1
FROM [Adventure Works]




2. Finding Number of Months 

You can find the Number of Month between two dates.