Wednesday, August 6, 2014

PeriodsToDate, YearToDate (YTD), QuarterToDate (QTD), MonthToDate (MTD), WeekToDate (WTD)

PeriodsToDate, YearToDate (YTD), QuarterToDate (QTD), MonthToDate (MTD), WeekToDate (WTD) are all MDX functions which aggregate date from de first date of a chosen period up and an including a given end date.

If you look a the standard MSDN example:

WITH MEMBER [Date].[Calendar].[First8Months2013] AS
    Aggregate(
        PeriodsToDate(
            [Date].[Calendar].[Calendar Year],
            [Date].[Calendar].[Month].[August 2013]
        )
    )
SELECT
    [Date].[Calendar].[First8Months2013] ON COLUMNS,
    [Product].[Category].Children ON ROWS
FROM
    [Adventure Works]
WHERE
    [Measures].[Order Quantity]

you will get:

image

My problem with this method is that I can’t directly see which measure is used, because it’s determined by the SLICER / WHERE statement.

I personally like to have some more control by making separated calculated members for each measures and period to date variety.

Year To Date:

MEMBER [Measures].[Sales Amount PeriodsToDate Year]
AS
    SUM(PERIODSTODATE( [Date].[Calendar].[Calendar Year]),[Measures].[Sales Amount])

or

MEMBER [Measures].[Sales Amount YearToDate]
AS
    SUM(YTD( [Date].[Calendar].CurrentMember),[Measures].[Sales Amount])

Semester To Date:

Member [Measures].[Sales Amount PeriodsToDate Semester]
AS
    SUM(PERIODSTODATE( [Date].[Calendar].[Calendar Semester]),[Measures].[Sales Amount])

or

There is no STD MDX functionality. This is probably because STD has a completely different meaning in the American English dictionary

Quarter To Date:

MEMBER [Measures].[Sales Amount PeriodsToDate Quarter]
AS
    SUM(PERIODSTODATE( [Date].[Calendar].[Calendar Quarter]),[Measures].[Sales Amount])

or

MEMBER [Measures].[Sales Amount QuarterToDate]
AS
    SUM(QTD( [Date].[Calendar].CurrentMember),[Measures].[Sales Amount])

Month To Date:

MEMBER [Measures].[Sales Amount PeriodsToDate Month]
AS
    SUM(PERIODSTODATE( [Date].[Calendar].[Month]),[Measures].[Sales Amount])

or

MEMBER [Measures].[Sales Amount MonthToDate]
AS
    SUM(MTD( [Date].[Calendar].CurrentMember),[Measures].[Sales Amount])

Week To Date:

MEMBER [Measures].[Sales Amount PeriodsToDate Week]
AS
    SUM(PERIODSTODATE( [Date].[Calendar Weeks].[Calendar Week]),[Measures].[Sales Amount])

or

MEMBER [Measures].[Sales Amount WeekToDate]
AS
    SUM(WTD( [Date].[Calendar Weeks].CurrentMember),[Measures].[Sales Amount])

This will get us:

SELECT
{
     [Measures].[Sales Amount]
    ,[Measures].[Sales Amount PeriodsToDate Year]
    ,[Measures].[Sales Amount YearToDate]
    ,[Measures].[Sales Amount PeriodsToDate Semester]
    ,[Measures].[Sales Amount PeriodsToDate Quarter]
    ,[Measures].[Sales Amount QuarterToDate]
    ,[Measures].[Sales Amount PeriodsToDate Month]
    ,[Measures].[Sales Amount MonthToDate]
    ,[Measures].[Sales Amount PeriodsToDate Week]
    ,[Measures].[Sales Amount WeekToDate]
} ON COLUMNS,
{
    [Date].[Calendar].[Date].&[20120101]:[Date].[Calendar].[Date].&[20120216]
}
ON ROWS
FROM [Adventure Works]

image

Have Fun And Till Next Time

No comments:

Post a Comment