The PeriodsToDate() Shortcut Functions
As we learned above, the PeriodstoDate() function is used within the following basic syntax:
PeriodsToDate([«Level»[, «Member»]])
Several "shortcut functions" are available in MDX to allow us to perform time-based summaries on period-to-date levels. Among these levels are the following, displayed in Table 1, that correspond to common levels of measurement in the business environment, together with the associated, specialized shortcut function that exists in MDX.
|
Level of Measurement |
Shortcut Function |
|
Year to Date |
YTD() |
|
Quarter to Date |
QTD() |
|
Month to Date |
MTD() |
|
Week to Date |
WTD() |
Table 1: The PeriodToDate() Shortcut Functions
Discussion
The above functions are equivalent to the PeriodsToDate() function, with the appropriate levels fixed for the specialized context within which each might be used. A composite of the syntax that each uses in the sample cube is as follows:
PeriodsToDate([Year] | [Quarter] | [Month] | [Week] , [«Member»]])
Because the syntax is similar to all the shortcut functions, and because the operation of each is identical, with the exception of the specific levels involved, we will examine one sample of the group to confirm our understanding of general concepts. To this end, let's examine the QTD() function specifically.
Syntax - QTD() Function
As is the case with each member of the set of shortcut functions listed in Table 1 above, the QTD () function is a shortcut function derived from the PeriodsToDate () function. The QTD() function defines the PeriodsToDate () function's «Level» argument to be Quarter. If no member is specified, the default is Time.CurrentMember. In short, therefore:
Qtd ([«Member»])
is equivalent to
PeriodsToDate(Quarter, «Member»)
The following example returns the set of members from the beginning of the Quarter level in the Time dimension, from the ancestor of Time.CurrentMember , through Time.CurrentMember:
Qtd()
Characteristically for all PeriodsToDate() derivatives, if no member is specified, the default is Time.CurrentMember.
Practice
Let's practice with the QTD() function, combining its use with that of a calculated member, in a manner that assists us in performing analysis of performance from one year to the next. We will rejoin the MDX Sample Application, creating a new query for the purposes of this exercise.
8. Select File --> New from the top menu of the MDX Sample Application.
9. Ensure that FoodMart 2000 is selected as the database name in the DB box of the toolbar, and that the HR cube remains selected in the Cube drop-down list box.
10. Type the following query into the Query pane:
-- MDX09-3: Tutorial Query No. 3
WITH
MEMBER [Measures].[QTD Salaries] AS
'Sum(QTD(),[Org Salary])'
SELECT
{[Measures].[Org Salary] , [Measures].[QTD Salaries]} ON COLUMNS,
[Time].[Month].Members ON ROWS
FROM HR
11. Select Query --> Run from the top menu to execute the query.
The query runs and the result dataset appears, as shown in Illustration 5.
Illustration 5: The Query Result Dataset
The query delivers the results as requested; we see the results above to be identical to those shown in Illustration 4 above. The reason is simple - we have substituted the QTD() function for
(PeriodsToDate([Time].[Quarter])
in the previous example, primarily to illustrate that the two functions deliver the identical result. QTD() is, as we have learned, a shortcut function for the PeriodsToDate() function with the «Level» argument set to Quarter. We thus prove the fact that the functions are equal by substituting the shortcut QTD() for PeriodsToDate().
The remaining shortcut functions behave in a similar manner.
We can easily see the utility of the shortcut functions for cases where the <<level>> argument, such as Quarter in our last example, can be predefined. Again, if no member is specified in the argument, the default is fixed as Time.CURRENTMEMBER by MDX.
Next in Our Series ...
In this lesson, we introduced the time series functions group, a specialized group of time series functions that MDX provides to perform analysis within the context of time. After discussing the common business need to analyze data over time, we overviewed the PeriodsToDate() function in detail, illustrated the syntax that is appropriate for its effective use, and then tested our understanding of how to leverage this important time series function by undertaking a relevant practice exercise.
As part of our use of the PeriodsToDate() function, we revisited the construction of a calculated member, using a calculated measure as a vehicle for illustration of the operation of the PeriodsToDate() function. We introduced the Sum statement as a part of building the calculated member, and discussed its constituent parts, as well. (We will revisit the Sum function many times as we progress in our series.) We then discussed the specialized "shortcut" functions that are based upon PeriodsToDate(), including the YTD(), QTD(), MTD(), and WTD() functions.
In our next lesson, we will introduce two additional time series functions, OPENINGPERIOD() and CLOSINGPERIOD(). Next, we will conclude our three-article time series functions group with an examination of the LASTPERIODS() and PARALLELPERIODS() functions.
» See All Articles by Columnist William E. Pearson, III
Discuss this article in the MSSQL Server 2000 Analysis Services and MDX Topics Forum.