Sum Function in MDX when using SSAS

2. April 2011 15:15

SSAS has quite a few functions that have different purposes and can make a lot of potential calcuatlions a lot easier.  A recent issue I was faced with is I had a new measure group to build based off a fact table.  This measue group had quite a few Calculated Members that needed to be built in order to complete the all the measures that would be needed.

 

I came across an interesting problem where I needed to use a measure from a seperate measure group, and apply a dimension to the measure to get a total. To get down to details, I needed the Gross USD (United States Dollars), only when applied by dimenions of food and beverages...not when involving merchandise.   Now, the identifier for Food, Beverages, and Merchandise were all part of one dimension named "Family Grouping".  So I knew in the end I needed the sum of Gross USD, and I needed the value of it only against Food and Beverage. 

 

So, at first I thought, maybe I would use the Scope Function, but that seemed like it wasn't really answering my issue, as the SCOPE function seemed to apply values across a particular range of a given measure. 

No, what I wanted was to be sure that whenever I got my GROSS USD total, it always gave me the amount when directly related to my particular dimensions...and the answer to that was to use the SUM function.

So my first stab at the sum function was to use code similar to this in my calculated memeber, minus all the other "who-ha".

SUM({null:[Dim Type Grouping].[Type Grouping].&[2]}, [Measures].[Gross USD])

What this did is it gave me the sum of the Measure Gross USD across my Dimension of type grouping 2...but I was surprised to see it included the type Grouping 1 as well.  This wasn't what I wanted as the the type grouping of 1 had no bearing on my calculated member that I was trying to achieve.  The Null statment previous my Dimension is what caused it to pull in my first dimension as well.  Admittedly I still need to figure out why that is the case.


So to fix my calculation I made a slight modificaiton, and got this.

SUM({[Dim Type Grouping].[Type Grouping].&[2]}, [Measures].[Gross USD])

 

The query above in my calculated member, properly returns only the Gross USD when related to the Dimension of typeGrouping 2.

In the end, the Sum function is quite powerful, and can be used in a variety of ways, and should not be confused with the AGGREGATE function, which works a bit differently.

 

About the author

My name is Brian, I'm A .NET programmer for Hard Rock. Causing trouble one line of code at a time.

Month List