Site icon Martin's Blog

Using calculation groups with role-playing dimensions

The 2019 release of Analysis Services (compatibility level 1500) brought about a new feature called Calculation Groups, which makes it easier to apply the same logic to multiple measures without the need to duplicate code. Each calculation group represents an entity (table) with attributes (columns) and attribute values (calculation items), and because of this implementation it can be used to deal with role-playing dimensions as well.

Before we dive into the details, this is what our tabular model looks like with multiple relationships between the Date and Internet Sales entities/tables. The active and default relationship is highlighted.

 
 

To reproduce the solution from the other blog post about role-playing dimensions, we can create a calculation group in the following manner:

 
 

Each calculation item has an Ordinal property which dictates the order in which it will appear in the table and an Expression that will define the formula that should be applied. Calculation groups themselves also have a Precedence property to control which group is applied first when multiple groups exist. What may be a little confusing here, is that a higher precedence (number) is applied first.

This is how we can define the expressions for each of our calculation items:

 
-- Ordinal 1
Order Date := 
CALCULATE
(
    SELECTEDMEASURE()
,   USERELATIONSHIP('Internet Sales'[Order Date Key], 'Date'[Date Key])
) 

-- Ordinal 2
Due Date := 
CALCULATE
(
    SELECTEDMEASURE()
,   USERELATIONSHIP('Internet Sales'[Due Date Key], 'Date'[Date Key])
) 

-- Ordinal 3
Ship Date := 
CALCULATE
(
    SELECTEDMEASURE()
,   USERELATIONSHIP('Internet Sales'[Ship Date Key], 'Date'[Date Key])
) 
 
 

The results shown above are exactly what we expect but there’s a few important things to take into consideration:

 

What about drill-through results? Can we use calculation groups to simplify that?

I hate to the the bearer of bad news…but because of the fact that the drill-through queries are not connected to the originating measure expression in any way, you still have to jump through a few hoops in order to return the correct drill-through results. Implement a similar method to what I describe here to make that work correctly.

 

Want to download the Visual Studio project that shows the solution? Get it from my GitHub repo.

Exit mobile version