0
votes

I want to define a cube measure in a SSAS Analysis Services Cube (multidimensional model) that calculates ratios for the selection a user makes for a predefined hierarchy. The following example illustrates the desired behavior:

|-City----|---|
| Hamburg | 2 |
| Berlin  | 1 |
| Munich  | 3 |

This is my base table. What I want to achieve is a cube measure that calculates ratios based on a users' selection. E.g. when the user queries Hamburg (2) and Berlin (1) the measure should return the values 67% (for Hamburg) and 33% (for Berlin). However if Munich (3) is added to the same query, the return values would be 33% (Hamburg), 17% (Berlin) and 50% (Munich). The sum of the values should always equal to 100% no matter how many hierarchy members have been included into the MDX query.

So far I came up with different measures, but they all seem to suffer from the same problem that is it seems impossible to access the context of the whole MDX query from within a cell.

My first approach to this was the following measure:

[Measures].[Ratio] AS SUM([City].MEMBERS,[Measures].[Amount])/[Measures].[Amount]

This however sums up the amount of all cities regardless of the users selection and though always returns the ratio of a city with regards to the whole city hierarchy.

I also tried to restrict the members to the query context by adding the EXISTING keyword.

[Measures].[Ratio] AS SUM(EXISTING [City].MEMBERS,[Measures].[Amount])/[Measures].[Amount]

But this seems to restrict the context to the cell which means that I get 100% as a result for each cell (because EXISTING [City].MEMBERS is now restricted to a cell it only returns the city of the current cell).

I also googled to find out whether it is possible to add a column or row with totals but that also seems not possible within MDX.

The closest I got was with the following measure:

[Measures].[Ratio] AS SUM(Axis(1),[Measures].[Amount])/[Measures].[Amount]

Along with this MDX query

SELECT {[Measures].[Ratio]} ON 0, {[City].[Hamburg],[City].[Berlin]} ON 1 FROM [Cube]

it would yield the correct result. However, this requires the user to put the correct hierarchy for this specific measure onto a specific axis - very error prone, very unintuitive, I don't want to go this way.

Are there any other ideas or approaches that could help me to define this measure?

1

1 Answers

0
votes

I would first define a set with the selected cities

[GeoSet] AS {[City].[Hamburg],[City].[Berlin]}

Then the Ratio

[Measures].[Ratio] AS [Measures].[Amount]/SUM([GeoSet],[Measures],[Amount])

To get the ratio of that city to the set of cities. Lastly

SELECT [Measures].[Ratio] ON COLUMNS,
[GeoSet] ON ROWS
FROM [Cube]

Whenever you select a list of cities, change the [GeoSet] to the list of cities, or other levels in the hierarchy, as long as you don't select 2 overlapping values ([City].[Hamburg] and [Region].[DE6], for example).