In a number of applications and business reports you want to obtain or return cumulative values for one or more measures. This article illustrates the use of the SUM, PeriodsToDate, YTD, QTD, and ITEM functions to return cumulative values over a period of time.
One approach to calculating cumulative values for a measure during all time requires the addition of an (All) level to the time dimension:
Open Analysis Manager, and then open the FoodMart 2000 database.
Edit the Time dimension and add an All level.
Process the Time dimension and the Sales cube. Adding the All level results in members being summed automatically.
Open the MDX sample application, and then select the FoodMart 2000 sample database.
Test the validity of the formula that you will be using for a calculated measure by executing this Multidimensional Expressions (MDX) query:
WITH MEMBER MEASURES.[Sales To Date] AS 'Sum(PeriodsToDate([Time].[(All)]), [Measures].[Unit Sales])'
SELECT
{[Measures].[Unit Sales],[Measures].[Sales To Date]} ON COLUMNS,
[Time].[Month].Members ON ROWS
FROM [Sales]
Note the values that display in the data grid.
Collapse this tableExpand this table
Month
Unit Sales
Sales to Date
1
21,628.00
21,628.00
2
20,957.00
42,585.00
3
23,706.00
66,291.00
4
20,179.00
86,470.00
5
21,081.00
107,550.00
6
21,350.00
128,901.00
7
23,763.00
152,664.00
8
21,697.00
174,361.00
9
20,388.00
194,749.00
10
19,958.00
214,707.00
11
25,270.00
239,977.00
12
26,796.00
266,773.00
Close the MDX sample application.
Edit the Time dimension and remove the All level. Re-process the Time dimension and the Sales cube.
An alternate approach to calculating cumulative values for a measure requires use of the ITEM function with the colon operator to sum over a range of times.
Open the MDX sample application, and then select the FoodMart 2000 database.
Test the validity of the formulas that you are going to use to calculate measures by executing this MDX query:
WITH
MEMBER MEASURES.[Sales To Date] as 'SUM({[Time].CurrentMember.Level.Members}.Item(0):Time.CurrentMember,[Measures].[Unit Sales])'
-- Use the ITEM() function to return the first member then sum to the currentmember
MEMBER MEASURES.[Sales Year To Date] AS 'SUM(YTD(),[Measures].[Unit Sales])'
-- Use the YTD() function to sum for Year To Date
MEMBER MEASURES.[Sales Quarter To Date] as 'SUM(QTD(),[Measures].[Unit Sales])'
-- Use the QTD() function to sum for Quarter To Date
SELECT
{[Measures].[Unit Sales],[Measures].[Sales To Date],[Measures].[Sales Year To Date],[Measures].[Sales Quarter To Date]} ON COLUMNS,
[Time].[Month].Members ON ROWS
FROM [Sales]
Close the MDX sample application.
Edit the Time dimension and change the All level to NO.
Re-process the dimension and the Sales cube.
Now that you have tested the formulas for the calculated members, you can add the formulas to the Sales cube.
Open the Sales cube for editing.
Add a new calculated member with the following properties:
Parent Dimension: Measures
Name : Sales To Date
Value : SUM({[Time].CurrentMember.Level.Members}.Item(0):Time.CurrentMember,[Measures].[Unit Sales])
Format String : STANDARD
Click OK to save the calculated member. This calculated member returns the value of the Unit Sales measure since the beginning of time.
Add a new calculated member with the following properties:
Parent Dimension: Measures
Name : Sales Year To Date
Value : SUM(YTD(),[Measures].[Unit Sales])
Format String : STANDARD
Click OK to save the calculated member. This calculated member returns the value of the Unit Sales measure for the year to date.
Click OK to save the calculated member. This calculated member returns the value of the Unit Sales measure for the quarter to date.
Parent Dimension: Measures
Name : Sales Quarter To Date
Value : SUM(QTD(),[Measures].[Unit Sales])
Format String : STANDARD
Browse the cube data and note the values that display in the newly added calculated members.