# Working with Time in Microsoft SQL Server 2008 MDX

• 2/25/2009

## Combining Time-Based Metrics

Throughout this chapter, you have explored the various time-based functions and how they can be used to enhance business analysis and solve business problems. Although each of these functions is valuable on its own, they are often used in combination to provide even greater insight and clarity into the analysis of business data. These may seem like very challenging metrics to assemble, but in reality they are no more complex than most other metrics calculated throughout this book. The trick is to remember tuple and expression basics.

Calculate year-to-date and prior period year-to-date sales

1. Open the MDX Query Editor to the MDX Step-by-Step database.

2. Enter the following query to retrieve reseller sales for the months of calendar year 2003:

```SELECT
{
([Measures].[Reseller Sales Amount])
} ON COLUMNS,
{
Descendants(
[Date].[Calendar].[CY 2003],
[Date].[Calendar].[Month],
SELF
)
} ON ROWS
FROM [Step-by-Step]```
3. Execute the query and review the results.

The query returns reseller sales by month for calendar year 2003. Using the PeriodsToDate function, you can calculate year-to-date sales just like before.

4. Alter the query to calculate a year-to-date sales:

```WITH
MEMBER [Measures].[Year to Date Reseller Sales] AS
Aggregate(
PeriodsToDate(
[Date].[Calendar].[Calendar Year],
[Date].[Calendar].CurrentMember
),
([Measures].[Reseller Sales Amount])
)
,FORMAT="Currency"
SELECT
{
([Measures].[Reseller Sales Amount]),
([Measures].[Year to Date Reseller Sales])
} ON COLUMNS,
{
Descendants(
[Date].[Calendar].[CY 2003],
[Date].[Calendar].[Month],
SELF
)
} ON ROWS
FROM [Step-by-Step]```
5. Execute the query and review the results.

Using the Year To Date Reseller Sales calculated member in a tuple, you can easily calculate year-to-date sales for the prior period.

6. Alter the query to calculate the prior period year-to-date sales:

```WITH
MEMBER [Measures].[Prior Period Year to Date Reseller Sales] AS
(
ParallelPeriod(
[Date].[Calendar].[Calendar Year],
1,
[Date].[Calendar].CurrentMember
),
[Measures].[Year to Date Reseller Sales]
)
,FORMAT="Currency"
MEMBER [Measures].[Year to Date Reseller Sales] AS
Aggregate(
PeriodsToDate(
[Date].[Calendar].[Calendar Year],
[Date].[Calendar].CurrentMember
),
([Measures].[Reseller Sales Amount])
)
,FORMAT="Currency"
SELECT
{
([Measures].[Reseller Sales Amount]),
([Measures].[Year to Date Reseller Sales]),
([Measures].[Prior Period Year to Date Reseller Sales])
} ON COLUMNS,
{
Descendants(
[Date].[Calendar].[CY 2003],
[Date].[Calendar].[Month],
SELF
)
} ON ROWS
FROM [Step-by-Step]```
7. Execute the query and review the results.

This exercise demonstrates a very simple approach to combining calculated members that use time-based functions. When formulating complex metrics, you can easily lose sight of the basic techniques allowing logic in one calculated member to be leveraged for another. As easily as you combined a period-to-date calculation with a prior period calculation, you could extend this query to include the difference, variance, or percent growth of the current year year-to-date values compared to the prior year year-to-date values or any flavors thereof.