These tables have examples that can be used as models when you are setting up calculated measure items.
Note: If you want to set up views that return YTD data, comparisons of current vs past periods, rolling N periods, previous N periods, etc., do so using regular measure items with time ranges. Examples are in Time Ranges vs. Time Hierarchies for YTD, Trending, and Other Time Analysis. If you want to set up a calculated measure item that displays an image, see Display Images for Measure Items.
More: Click here for a complete list of all example MDX expressions that exist in this online help for all types of Stratum.Viewer items (calculated measure items, measure item captions, filters, and more).
Type of Calculation & Function |
Example |
Achievement Percent Has a built-in divide by zero check to avoid divide by zero errors. |
#AchievementPercent([Measures].[Data1 (Actual Sales Sales Amount Q1 2014 to Q3 2014 )], [Measures].[Data2 (Budget Budget Amount Frozen Q1 2014 to Q3 2014)])
|
Divide with Zero Check Has a built-in divide by zero check to avoid divide by zero errors. |
#DivideWithZeroCheck([Measures].[Data1 (Daily Sales Amount Wk 38 2014 to Wk 38 2014)], [Measures].[Data2 (Daily Sales Units Wk 38 2014 to Wk 38 2014)])
|
Percent of Change Has a built-in divide by zero check to avoid divide by zero errors. |
#PercentOfChange([Measures].[Data1 (Actual Sales Amount Q1 2013 to Q3 2013)], [Measures].[Data2 (Actual Sales Sales Amount Q1 2012 to Q3 2012)])
|
Percent Of Total |
#PercentOfTotal([Measures].[Data1 (Actual Sales Amount Wk 1 2017 to Wk 37 2017)])
|
Percent Of Subtotal |
#PercentOfSubtotal([Measures].[Data1 (Actual Sales Amount Wk 1 2017 to Wk 37 2017)])
|
Cumulative Percent Of Total and ABC Cumulative Percent Of Total |
#CumulativePercentOfTotal([Measures].[Data1 (Daily Sales Amount Jan 2014 to Sep 2014)])
and #ABCCumulativePercent([Measures].[Data1 (Daily Sales Amount Jan 2014 to Sep 2014)],".65;.25")
|
Cumulative Total and ABC Cumulative Total |
#CumulativeTotal([Measures].[Data2 (Budgeted Units Jan 14 to Sep 14)])
and #ABCCumulative([Measures].[Data2 (Budgeted Units Jan 14 to Sep 14)],"75000000.00;35000000.00;10000000.00")
|
The next set of examples show other commonly used calculations including ones that use attributes relationships (also known as attributes) in their expression. Use these expressions to do things like return a text value or numeric value, to segment out some data from a larger pool of data, or to use the value of an attribute relationship in a calculation.
Calculations that use attributes should have validations built into them to ensure they are only performed when certain conditions are met. Validations check for things like whether the levels for the attributes are visible in the view. If conditions are not met, the calculation can instruct Stratum.Viewer to return a null value rather than perform the calculation. Here are examples of validations you should build into expressions that use attributes.
Only perform the calculation if the level of the attribute is displayed in the grid. Include an IIF statement in the expression that checks for the proper level. Calculation is performed if the level is in the grid. A null value is returned if the level is not in the grid.
Only perform the calculation for members (key values) that have measure data. Include an IIF statement in the expression that checks whether data exists for the applicable measure item. Calculation is performed for key values that have data. A null value is returned for key values without data. If your calculation does not have this validation built into it, all key values will be included in view results.
Verify attribute data is the correct type need by the calculation. For example, include an IIF statement that tells Viewer to only perform the calculation if the attribute data is numeric. If it is not, the statement tells Viewer to return a null value for the results.
Type of Calculation & Function |
Example Expression |
Average Uses Average numeric function. |
Avg({[Measures].[Data1 (Sales Units Jan 2014 to Dec 2014)], [Measures].[Data2 (Sales Units Jan 2013 to Dec 2013)]}) · Returns average sales units for 2013 and 2014. The expression syntax includes the names (Data1 and Data2) and captions of two regular measure items that are part of the view. · Recommendation: Set the Type property to Distinct Calculated. |
Difference |
[Measures].[Data22 (Act Gross Margin After Rebate)]-[Measures].[Data21 (Std Gross Margin After Rebate)] Returns difference between the Act Gross Margin After Rebate and Std Gross Margin After Rebate measure items (their captions and the measure item names Data22 and Data21 are part of the MDX syntax in the expression). |
Extended List Price Uses attribute relationship and Val function. |
This calculation determines the extended list price for the year to date by multiplying a measure item in a view by the Prod Current List Price attribute relationship from the Product level. It uses a Val function in its expression. Validations built into the calculation only allow it to proceed if the attribute relationship in it has numeric values and other specified items are active in the view at the time the calculation is performed. If any condition specified by a validation is not met, a null value is returned instead of performing the calculation. The full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. A monetary format is recommended for this type of calculation. Full Expression:// Only do the calculation if the current level is Product AND // if the Prod Current List Price attribute value is numeric AND // if Daily Sales Units exist IIF([Product].[Product].CurrentMember.Level.Name = "Product" AND isnumeric([Product].[Product].Properties("Prod Current List Price")) = True AND [Measures].[Data1 (Daily Sales Units)] <> null, // Here’s the calculation Val([Product].[Product].Properties("Prod Current List Price"))*[Measures].[Data2 (Daily Sales Units)], // Return null value if the validations prevent doing calculation // This syntax closes out the IIF validation null) The following image shows what the expression looks like in Viewer’s Expression window. Here is the view when the Product level is visible. The calculation is performed. Here is the view after some changes were made to the levels for it. Product level is no longer visible, so the calculation is not performed. Null values are returned. |
Number of UPC’s Sold and Total Number of UPC’s Uses Count numeric function and CrossJoin function. |
The first calculation counts the number of UPC’s that have sales for the level visible in the view. The second calculation counts the total number of UPC’s. The UPC level is evaluated against sales amount values for the current quarter. Validations built into each calculation only allow it to proceed if it meets certain conditions. If the condition specified by a validation is not met, a null value is returned instead of performing the calculation. Each full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. For both calculations, Format and the Total properties are set to None. Full Expression For Counting Only UPC’s With Sales:// Only do the calculation for values where Sales Amount Current Qtr is not zero IIF([Measures].[Data1 (Sales Amount Current Qtr)] <> 0, // Here’s the calculation // The EXCLUDEEMPTY tells it to not count UPC’s without sales Count(CrossJoin({[UPC Global Number].[UPC Global Number].[UPC Global Number].members},{[Measures].[Data1 (Sales Amount Current Qtr)]}),EXCLUDEEMPTY), // Return null value if the validation prevents doing calculation // This syntax closes out the IIF validation null) Full Expression For Counting UPC’s With and Without Sales:// Only do the calculation for values where Sales Amount Current Qtr is not zero IIF([Measures].[Data1 (Sales Amount Current Qtr)] <> 0, // Here’s the calculation // The INCLUDEEMPTY tells it to count all UPC’s even those // without sales Count(CrossJoin({[UPC Global Number].[UPC Global Number].[UPC Global Number].members},{[Measures].[Data1 (Sales Amount Current Qtr)]}),INCLUDEEMPTY), // Return null value if the validation prevents doing calculation // This syntax closes out the IIF validation null) The following images shows what each expression looks like in Viewer’s Expression window. The last image shows the results in the view with the calculation results displayed in the marked columns. Here is the view showing calculation results. |
Profit (Sales after Costs) and Sales after Returns Uses Absolute value of a measure item. |
[Measures].[Data2 (Actual Sales Sales Amount)]-[Measures].[Data1 (Actual Sales Ext Standard Cost)] and [Measures].[Data2 (Actual Sales Sales Amount)]-abs([Measures].[Data4 (Actual Sales Sales Return Amount)])
|
Segment the Total Sales For a Level by Specific Members of a Different Level (see more detail)
Uses the SUM function, Members, and Tuples |
This type of calculation lets you segment totals for levels in your view by members of other levels. For example, look at a Ship-To Market and Product Category sales by specific distribution channels. The following gives you the total sales within each Market/Category for only two specific distribution channels and ignores values for all other channels. IIF([Measures].[Data1 (Total Sales Jan CY to Sep CY)]<>0,SUM({[Distribution Channel].[Distribution Channel].[Distribution Channel].[INB],[Distribution Channel].[Distribution Channel].[Distribution Channel].[DIR]},[Measures].[Data1 (Total Sales Jan CY to Sep CY)]),null)
|
Return Text Value if Condition is Met Uses IIF function to check for conditions and determine which results to return. |
IIF([Measures].[Data22 (Actual Sales Sales Units Jan to Dec)]>[Measures].[Data2 (Budget Budget Units Frozen Jan to Dec)],"y",null)
|
Standard Cost Uses Val function and attribute relationship. |
This calculation determines the standard cost for last year by multiplying a measure item in a view by the value of a Std Cost Last Year attribute relationship from the Product level. It uses a Val function in its expression. Validations built into the calculation only allow it to proceed if the attribute relationship in it has numeric values and other specified items are active in the view at the time the calculation is performed. If any condition specified by a validation is not met, a null value is returned instead of performing the calculation. The full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. The Format property for the calculation is set to a monetary format and its Total property is set to None. Full Expression:// Only do the calculation if the current level is Product AND // if the Std Cost Last Year attribute value is numeric AND // if Actual Sales Units exist IIF([Product].[Product].CurrentMember.Level.Name = "Product" AND isnumeric([Product].[Product].Properties("Prod Std Cost Last Year")) = True AND [Measures].[Data2 (Actual Sales Units)] <> null, // Here’s the calculation Val([Product].[Product].Properties("Prod Std Cost Last Year"))*[Measures].[Data2 (Actual Sales Units)], // Return null value if the validations prevent doing calculation // This syntax closes out the IIF validation null) The following image shows what the expression looks like in Viewer’s Expression window. The next image shows the view when the Product level is visible. The calculation is performed because all calculation conditions were met including the condition that Product must be visible. Here is the view when Ship-to Market has been drilled up to and Product is no longer visible. Viewer returns null values instead of performing the calculation because Product is not visible. |
Cost Difference Calculation uses values from a level’s attribute relationship (attributes). |
This example uses a Val function and two Cost attribute relationships for UPC Global Number. The “Cost This Yr” and “Cost Last Yr” attributes are subtracted to find the difference in costs between this year and last year. Validations built into the calculation only allow it to proceed if the attribute relationships in it are numeric values and other specified items are active in the view at the time the calculation is performed. If any condition specified by a validation is not met, a null value is returned instead of performing the calculation. The full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. The Format property for the calculation is set to a numeric format and its Total property is set to None. Full Expression:// Only do the calculation if current level is UPC Global number AND // if the Cost This Yr AND Cost Last Year attribute values are // numeric AND // if Sales Units This Yr exists IIF([UPC Global Number].[UPC Global Number].CurrentMember.Level.Name="UPC Global Number" AND isnumeric([UPC Global Number].[UPC Global Number].Properties("Cost This Yr")) = true AND isnumeric([UPC Global Number].[UPC Global Number].Properties("Cost Last Yr")) =true AND [Measures].[Data1 (Sales Units This Yr)] <> null, // Here's the calculation Val([UPC Global Number].[UPC Global Number].Properties("Cost This Yr")) - Val([UPC Global Number].[UPC Global Number].Properties("Cost Last Yr")), // Return null value if the validations prevent doing calculation // This syntax closes out the IIF validation null) The following image shows what the expression looks like in Viewer’s Expression window. The next image shows results in the view when all calculation conditions were met and Viewer performed the calculation. The calculation results are in the last column. This final image shows results in the view when one of the calculations conditions was not met. The UPC Global Number level is not visible in the view, so Viewer returned a null value instead of performing the calculation. |
Truncate String Of Characters From Level Or Attribute Values The LEFT and RIGHT functions are used in these calculations. |
The following calculations return a string of ‘x’ number of characters from the values of other items in a view. The first calculation returns the first 4 characters of the UPC List Catalog Number attribute relationship from the UPC Global Number level. The second calculation returns the last 10 characters of the key values for that level. Validations built into the calculation only allow it to proceed if the level is visible in the view. If that validation is not met, a null value is returned instead of performing the calculation. The full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. Both calculations have their Format and Total properties set to None. Full Expression Returning First 4 Characters For Attribute Values:// Only do the calculation if current level is UPC Global number AND // if Sales Amount Current Yr / Month exists IIF([UPC Global Number].[UPC Global Number].CurrentMember.Level.Name="UPC Global Number" AND [Measures].[Data1 (Sales Amount Current Yr / Month)] <> null, // Here's the calculation LEFT([UPC Global Number].[UPC Global Number].CurrentMember.Properties("UPC List Catalog Number"), 4), // Return null value if the validations prevent doing calculation // This syntax closes out the IIF validation null) Full Expression Returning Last 10 Characters For Level Values:// Only do the calculation if current level is UPC Global number AND // if Sales Amount Current Yr / Month exists IIF([UPC Global Number].[UPC Global Number].CurrentMember.Level.Name="UPC Global Number" AND [Measures].[Data1 (Sales Amount Current Yr / Month)] <> null, // Here's the calculation RIGHT([UPC Global Number].[UPC Global Number].currentMember.name, 10), // Return null value if the validations prevent doing calculation // This syntax closes out the IIF validation null) The following images shows what each expression looks like in Viewer’s Expression window. The last image shows the results in the view when the UPC Global Number level is visible in the view, which meets the calculation conditions and allows Viewer to move ahead with performing the calculation. Results of the calculations are shown in the last two columns. This variation on the view displays the truncated items (the level value and one of its attributes) to show you the difference between their original states versus their truncated states returned by the calculations. |
Top N Total and Bottom N Total Use Sum function. |
Sum({TopCount([RepBroker].[RepBroker].[RepBroker].members, 4, [Measures].[Data2 (Daily Sales Units Current Yr Month)])}, [Measures].[Data2 (Daily Sales Units Current Yr Month)]) and Sum({BottomCount([RepBroker].[RepBroker].[RepBroker].members, 4, [Measures].[Data2 (Daily Sales Units Current Yr Month)])}, [Measures].[Data2 (Daily Sales Units Current Yr Month)])
|
Variance Percentage
|
Use the Percent of Change function when you want to include a variance percentage calculation in your view. That function is a Stratum.Viewer function that automatically includes a divide by zero check in the calculation to avoid divide by zero errors. See the first table in this topic for an example. |
Days Until Expiration Uses the Date Difference function, Today date function, and an attribute relationship. |
This calculation shows us days until Lots expire. It uses the Date Difference function with a value of "d" to calculate the difference in days between the current date and Lot Expiration Date. A Today function provides the current date while one of the Lot level’s attribute relationships provides the expiration date. A validation built into the calculation only allows it to proceed if the Lot has a valid expiration date. If the condition is not met, a null value is returned instead of performing the calculation. The full expression follows. The text in green, bold font is the core calculation. Text preceded by a // mark are comments that explain each part of the expression. Comments are ignored by Viewer. The Format and Total properties for the calculation are set to None. Notes: This example calculates the "days" until expiration using the parameter of "d" in the Date Difference function. These other parameters can be used to calculate results in other intervals of time: yyyy for year, q for quarter, m for month, y for day of year, w for weekday, ww for week, h for hour, m for minute, and s for second. Full Expression:// Only do the calculation if the Lot Expiration Date for the Lot // is a valid date IIF(IsDate([Lot].[Lot].Properties("Lot Expiration Date")), // Here's the calculation which uses Date Difference // with a value of 'd' to determine how many days // until a Lot expires or how many days ago it expired. // The Today function which has syntax of Now() // determines the current date for use in the calculation. DateDiff("d", Now(), [Lot].[Lot].Properties("Lot Expiration Date")), // Return null value if the validation prevents doing calculation // This syntax closes out the IIF validation null) The following image shows what the expression looks like in Viewer’s Expression window. The next image shows results in the view when the calculation condition was met and Viewer performed the calculation. Notes: Results with negative numbers would indicate that a Lot already expired “x” days ago. For example, something that expired 5 days ago would have a -5 result. This example does not have any expired Lots. |