Get running sum using MDX based at the only first attributes change in the set

258 views Asked by At

I have the following data structure

age     range    sex    population_segment
1        0-4      1       100
2        0-4      1       100
3        0-4      1       100
4        0-4      1       100
5        5-9      1       150
6        5-9      1       150

I want to get running sum based at the only first range and sex attributes change in the set. For example, correct answer for me is to summarize only record 1 and record 5 (i.e. 250). My groupers are range and sex.

Currently my measure is just summing all values, getting me the wrong sum of population: 750 I know how to do it in standard SQL (posted here for clarification), but I need MDX solution, calculated member.

Sample sql:

WITH cte AS 
(
select 
*,
RowNum = row_number() over(partition by range, sex order by range, sex)
FROM myPopulationData
)
SELECT  SUM(population_segment) FROM  cte WHERE  RowNum = 1
1

There are 1 answers

0
FrankPl On BEST ANSWER

This is a case that can be resolved with a many-to-many relationship:

  • Build a table/view containing the unique combinations of range and sex as well as the population_segment, and a primary key column. This will be used as a fact table for the measure group containing the population_segment measure. It will also be used as a dimension table for the range-sex combination.
  • Build a table/view containing age used as dimension table for age.
  • Build a bridge table/view containing a foreign key to the age dimension table, as well as one to the first table. This will get the base of another measure group, which just contains count as a measure, which you probably want to make invisible.
  • On the "Dimension Usage" tab, set up the relationship between the main measure group and the age dimension as a many-to-many relationship using the bridge measure group.

Once you have set up everything, all the calculation logic is handled automatically by Analysis Services.

It is a matter of usability for the users if you leave range and sex in the age dimension in addition to the range-sex dimension and make the range-sex dimension (built from the first table and linked from the bridge table) invisible, or if you keep the age in on and range in another dimension. I personally would prefer the first choice, at least for the range.