Thursday, 28 August 2014

Filtering MDX Query based on date selected through calender control exposed by SSRS DateTime Filter/parameter.


  • Selected date from calender control in SSRS cannot be directly applied on MDX Query for filtering the data. But, We can do this by using following technique.
  • Add new filter to your RDL with name "Date" & datatype "DateTime".
  • As, MDX need Member name for filtering data we will first write an Expression in SSRS to get Unique name of member.
    • Date format Comming out of Filter: dd-mm-yyyy
    • Targeted format: [Date].[Date].&[yyyymmdd]  --{This may vary from cube to cube}
    • SSRS Expression: 
      • =Format("[Date].[Date].&["
                      & DatePart("yyyy",Parameters!Date.Value) 
                      & right("0" & DatePart("m",Parameters!Date.Value),2)
                      & right("0" & DatePart("d",Parameters!Date.Value),2)
                      & "]")
  •  Now, Add new dataset to your RDL with query below.
    • Query:
      • WITH MEMBER [Measures].[WTDOrderCount]
            AS
            SUM
            (
                WTD([Date].[Calendar Weeks].currentmember)
                ,[Measures].[Order Count]
            )
            MEMBER [Measures].[TDOrderCount]
            AS
            SUM
            (
                YTD([Date].[Calendar Weeks].currentmember)
                ,[Measures].[Order Count]
            )
            MEMBER [Measures].[YTDOrderCount]
            AS
            SUM
            (
                YTD([Date].[Calendar Weeks].currentmember)
                ,[Measures].[Order Count]
            )
            SELECT
                {[Measures].[WTDOrderCount],[Measures].[YTDOrderCount]} ON 0
                ,{
                    [Date].[Calendar Weeks].[Calendar Week].Members
                  
                } on 1
            FROM
            (
                SELECT { STRTOMEMBER(@Date)} ON 0
                FROM [Adventure Works]
            )
    •  Go to parameters tab and manually add new parameter with Parameter Name as  "Date" and below SSRS Expression for getting Member Unique Names as "Paramter Value".
      • =Format("[Date].[Date].&["
                      & DatePart("yyyy",Parameters!Date.Value) 
                      & right("0" & DatePart("m",Parameters!Date.Value),2)
                      & right("0" & DatePart("d",Parameters!Date.Value),2)
                      & "]")
  • Now, you should be effectively able to filter you data based on selection made through calender control exposed by SSRS DateTime Filter/parameter.

No comments:

Post a Comment