Sunday, January 16, 2011

Retrieving data for current year, current month and current day.


You can retrieve current year, current month and current day by using VBA functions.


MDX for retrieving current Year;

WITH MEMBER [Measures].[Current Year] AS
    VBAMDX.Format(VBAMDX.Now(),"yyyy")
SELECT
  {[Measures].[Current Year]} ON COLUMNS
FROM [Adventure Works];

MDX for retrieving current Month;


WITH MEMBER [Measures].[Current Month] AS
    VBAMDX.Format(VBAMDX.Now(),"MM")
SELECT
  {[Measures].[Current Month]} ON COLUMNS
FROM [Adventure Works];


MDX for retrieving current Date;

WITH MEMBER [Measures].[Current Date] AS
    VBAMDX.Format(VBAMDX.Now(),"dd")
SELECT
  {[Measures].[Current Date]} ON COLUMNS
FROM [Adventure Works];

Sometimes user wants to retrieve data for current year, current month, and current day. You can retrieve data by using VBA function and STRTOMEMBER function.

Check following MDX samples by using Adventure Works sample.

MDX for retrieving current Year;

WITH
  MEMBER [Measures].[Current Year Value] AS
    (
      StrToMember
      (
            "[Date].[Calendar].[Calendar Year].&["
          + VBAMDX.Format(VBAMDX.Now(),"yyyy") + "]"
      )
     ,[Measures].[Internet Sales Amount]
    )
SELECT
  [Measures].[Current Year Value] ON COLUMNS
FROM [Adventure Works];


MDX for retrieving current Month;

WITH
  MEMBER [Measures].[Current Month Value] AS
    (
      StrToMember
        (
            "[Date].[Calendar].[Month].&[" +
             VBAMDX.Format(VBAMDX.Now(),"yyyy") + "]
            &[" + VBAMDX.Format(VBAMDX.Now(),"MM") + "]"
        )
     ,[Measures].[Internet Sales Amount]
    )
SELECT
  [Measures].[Current Month Value] ON COLUMNS
FROM [Adventure Works];

MDX for retrieving current Day;

WITH
  MEMBER [Measures].[Today's Value] AS
    (
      StrToMember
      (
           "[Date].[Calendar].[Date].&[" +
           VBAMDX.Format(VBAMDX.Now(),"yyyyMMdd")
           + "]"
      )
     ,[Measures].[Internet Sales Amount]
    )
SELECT
  [Measures].[Today's Value] ON COLUMNS
FROM [Adventure Works];

Note : AdventureWorks sample does not contain data for current year,so if you execute above mentioned MDX,it results in Null values but if you insert data for current year,it will result the required output.

6 comments:

  1. Thanks for sharing!How could we calculate last year measure?

    ReplyDelete
  2. You can use Lag(1) ....

    WITH
    MEMBER [Measures].[Current Year Value] AS
    (
    StrToMember
    (
    "[Date].[Calendar].[Calendar Year].&["
    + VBAMDX.Format(VBAMDX.Now(),"yyyy") + "]"
    ). Lag(1)
    ,[Measures].[Internet Sales Amount]
    )
    SELECT
    [Measures].[Current Year Value] ON COLUMNS
    FROM [Adventure Works];

    ReplyDelete
  3. Thanks you for this.
    I have a dimension DimDate (Year, month, days, quater) and in this dimension I would like to create a relative calendar corresponding to the relative year and relative month. Eg in: - Relative Year I would like to have a Member : current year, current year-1 - Relative Month i would like to have a member : Current month, current month -1, current month-2...
    how to do that?
    Thanks

    ReplyDelete
  4. Hi,
    I want total no of weekdays in current month including future dates of current month.
    to calculate prediction for one month

    ReplyDelete