Microsoft Developer Network

Home > Developer Network > Community > Articles > SQL Server Analysis Services > Calculating YTD, MTD, and QTD

Calculating Year-to-Date (YTD), Month-to-Date (MTD), and Quarter-to-Date (QTD) in SQL Server Analysis Services

A common requirement in business intelligence reporting is to analyze data on a Year-to-Date (YTD), Month-to-Date (MTD), or Quarter-to-Date (QTD) basis. SQL Server Analysis Services (SSAS) provides powerful tools to implement these calculations efficiently, typically using MDX (Multidimensional Expressions). This article will guide you through the common methods for creating these time-intelligence calculations within your SSAS multidimensional models.

Understanding the Concepts

Before diving into the MDX, let's clarify what each term means:

Prerequisites

To implement these calculations, your SSAS model must have a well-defined Date dimension. This dimension should include attributes such as:

Crucially, the Date dimension should be marked as a Date dimension in your cube.

MDX Calculations for Time Intelligence

1. Month-to-Date (MTD)

The MTD calculation sums values from the beginning of the current month up to the current period. We can use the ParallelPeriod function combined with PeriodsToDate or a direct SUM with slicing.

MDX Example for MTD Sales


WITH MEMBER [Measures].[Sales MTD] AS
    SUM(
        PeriodsToDate([Date].[Calendar].CalendarMonth, [Date].[Calendar].CurrentMember),
        [Measures].[Sales Amount]
    )
SELECT
    {[Measures].[Sales Amount], [Measures].[Sales MTD]} ON COLUMNS,
    [Date].[Calendar].[Date].MEMBERS ON ROWS
FROM [YourCubeName]
WHERE ([Date].[Calendar].CurrentDateMember)
            

Alternatively, using ParallelPeriod and Tail is a common pattern:


WITH MEMBER [Measures].[Sales MTD - ParallelPeriod] AS
    SUM(
        Tail(
            PeriodsToDate([Date].[Calendar].CalendarMonth, [Date].[Calendar].CurrentMember),
            1
        ),
        [Measures].[Sales Amount]
    )
SELECT
    {[Measures].[Sales Amount], [Measures].[Sales MTD - ParallelPeriod]} ON COLUMNS,
    [Date].[Calendar].[Date].MEMBERS ON ROWS
FROM [YourCubeName]
            

2. Quarter-to-Date (QTD)

Similar to MTD, QTD calculates the sum from the beginning of the current quarter.

MDX Example for QTD Sales


WITH MEMBER [Measures].[Sales QTD] AS
    SUM(
        PeriodsToDate([Date].[Calendar].CalendarQuarter, [Date].[Calendar].CurrentMember),
        [Measures].[Sales Amount]
    )
SELECT
    {[Measures].[Sales Amount], [Measures].[Sales QTD]} ON COLUMNS,
    [Date].[Calendar].[Date].MEMBERS ON ROWS
FROM [YourCubeName]
            

3. Year-to-Date (YTD)

YTD calculations sum values from the beginning of the current year.

MDX Example for YTD Sales


WITH MEMBER [Measures].[Sales YTD] AS
    SUM(
        PeriodsToDate([Date].[Calendar].CalendarYear, [Date].[Calendar].CurrentMember),
        [Measures].[Sales Amount]
    )
SELECT
    {[Measures].[Sales Amount], [Measures].[Sales YTD]} ON COLUMNS,
    [Date].[Calendar].[Date].MEMBERS ON ROWS
FROM [YourCubeName]
            

Using the Date Hierarchy

The key to these calculations is correctly referencing the date hierarchy. For example, [Date].[Calendar].CalendarMonth refers to the month level within your 'Calendar' hierarchy in the 'Date' dimension. Ensure your dimension and hierarchy names match your SSAS model.

Best Practices and Considerations

By leveraging MDX and a well-structured Date dimension, you can provide powerful time-intelligence capabilities to your users, enabling deeper insights into business performance.