Question

Suppose I have a dimension called library books and a fact called days overdue. How do I write an mdx expression that will return the number of books that exist at each num days overdue value? In other words:

Days Overdue | Num books
1            | 3498
2            | 237
3            | 57

In SQL, the answer is simple:

select days_late,count(*) from days_overdue_fact group by days_late

In mdx, I have tried this:

with member [Measures].[Overdue Count] as 'count([Book].[System Id].members) '
select  [Measures].[Overdue Count] on 0,
[Measures].[Days Late] on 1
FROM [myCube] 

But I get a complaint from Mondrian: '[Measures]' appears in more than one independent axis.

I simply want to group by the fact values.

Was it helpful?

Solution

The solution is to recognize that this overdue measure is simply part of an accumulating snapshot fact (see The Data Warehouse Toolkit, 2n ed., Ralph Kimball or http://www.kimballgroup.com/2008/11/05/fact-tables/), that tracks the status of a book over time as it moves through its checkout/in process.

There's nothing wrong with having an accumulating snapshot fact like this in a cube. You just have to understand what it is and how to query it. In this case I want to count the occurrences of each value that appears in this fact and the easiest way to do that is with a sql group by rather than mdx.

The answer is he sql in the original question. There is no mdx solution for grouping by a measure value that I am aware of. MDX is the wrong tool for this query.

OTHER TIPS

In my opinion the way you have constructed your cube is wrong. [Days Overdue] doesn't need to (maybe even best not to) be a measure. Measure most frequently is a DEPENDENT numeric value which shall be calculated for each possible combination of dimension values. It most definitely can take a value of null but very scarcely. I don't really see how you relate [Days Overdue] to ALL possible combination of dimension values.
What you have here as measure could easily be a calculated dimension field in which you deduct the borrowing date from NOW(). There is only one question left to be answered in this case and that's to know if Borrowing date (the date a book is borrowed) belongs to a fact table or not. If that's true then you have to define a new fact dimension and define your relationship there as explained here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top