In this ending part of the tri series posts let’s check out the third option in the calculations tab which is “Script Commands”. Here, I am focusing on the SCOPE statement with which we can apply a calculation to a specific portion of the cube. I have already demonstrated the first two options i.e. Calculated Members and Named Sets in my previous posts which are available in the Part I and Part II of this series.
Any calculation or script command that we add inside a cube applies to the entire cube i.e. the default scope of any calculation or script command is the whole cube. Scope assignment is used to limit the scope of a calculation so as to apply it only to a part of the cube. To achieve this, a SCOPE statement is used to define a subcube, which defines the scope of all subsequent MDX expressions and statements within the calculation script until the current scope ends or is re-scoped. The THIS function is then used, which applies the MDX script to the current scope i.e. to a particular cube space.
When we create a cube, a default MDX script is also created which defines calculations in the cube. Open the calculation tab in the cube designer and you will find the calculate command which is the very first statement inside the script.
/* The CALCULATE command controls the aggregation of leaf cells in the cube. If the CALCULATE command is deleted or modified, the data within the cube is affected. You should edit this command only if you manually specify how the cube is aggregated. */ CALCULATE;
Each time we create a new calculated member or a named set, the code gets added to this script. The script informs the server to calculate the cube. These MDX statements, expressions etc. then processed in the same order they appear in the Script Organizer.
Confused…Ok, let’s understand this step by step with the help of a simple example to know how it exactly works using Adventure Works cube. Consider the following view where I am browsing the cube by slicing the Sales Amount measure by Sales Territory Group, Sales Territory Country and Order Date Year.
You can see the Sales by Sales Territory Country for the Orders placed in the year 2001, 2003 and 2004. Now suppose we want to have a forecasted Sales figure for the year 2006 and that too for the Sales Territory group Europe. We can achieve this by writing an MDX calculation script using the SCOPE statement. As we need to apply this calculation only to a specific portion of the cube, we need to first define our subcube using the SCOPE statement as follows,
SCOPE ( [Measures].[Sales Amount], [Order Date].[Calendar].[Year]., [Sales Territory].[Sales Territory Group].&[Europe] ); THIS = [Order Date].[Calendar].[Year]. * 1.1; End Scope;
The THIS function is applying the calculation to the subcube i.e. the Sales figure for the Year 2006 is arrieved at by adding 10% margin to the Sales value of 2004. Deploy and process the cube and click on Reconnect to see the results of the calculation as follows,
The forecasted Sale for 2006 is highlighted in green. Notice that the rest of the values are intact. But why we can not see the Sales Amount value against each Country within Europe. This is because it depends on how we define the subcube. To have a country wise breakup of the Sales value, modify the SCOPE statement as follows,
SCOPE ( [Measures].[Sales Amount], [Order Date].[Calendar].[Year]., [Sales Territory].[Sales Territory Group].&[Europe], [Sales Territory].[Sales Territory Country].[Sales Territory Country].members ); THIS = [Order Date].[Calendar].[Year]. * 1.1; End Scope;
Deploy and process the cube and you can see the country wise uplifted Sales as highlighted in green,
So now, if a user asks you to manipulate some measures for specific dimension members, you will definitely give it a try. Also good knowledge of MDX will definitely help you to write the calculations in a better and innovative way. Whatever I have shown in my series is just a tip of an iceberg and there are many more things to explore. Hope the series will serve as a starting point to help you understand the concept behind using the Calculations tab to define calculated members, named sets, or scoped assignments in a cube.