I am just starting to use SSAS and I'm trying to produce a data cube that allows me to produce a chart of the number of people in a particular age range per quarter spanning a number of years. So for example:
| $ @$ $
Number of people | # $ @$ #@$
| #@$ #@$ #@$
+------------------------------
2010 Q1 2010 Q2 ... 2014 Q1
where # are people aged 10-20, @ are people aged 20-30 and $ are people aged 30-40.
The problem I'm finding is that in 2010 someone might have been 29 and so would fall into the 20-30 age range, but in 2014 I want that same person to be counted in the 30-40 age range because he would now be 33. I don't know how to (or if it is even possible to) create a dimension that would be date and time sensitive?
Build it into your model
Just model it correctly: Assuming you have a fact table containing the measures you analyze and foreign keys to a person dimension as well as a date dimension, add a new table for the "age group" dimension containing the ranges you want to analyze. This table would contain e. g. one record "0-9", one "10-19", etc.
Then add a foreign key column referencing the age group dimension to the fact table. Setting the value of this foreign key would then be a SQL update statement on the fact table that calculates the difference between the date referenced by the foreign key to the date dimension and the birth date of the person referenced by the foreign key to the person dimension. This difference is the age at that date. From this, determine the age group and use its primary key as the value of the foreign key column.
And finally, just make an attribute from your age group column of the age group dimension.
Calculate it on the fly
If you really want to calculate that on the fly, I am not sure the performance is good, but you could try it as follows: You would need the "age group" dimension as well. But there would be no need to link it to your fact table. Then define a calculated member like this:
member [Measures].[Age at Date] AS
DateDiff('yyyy',
[Person].[BirthDate].CurrentMember.Properties("Key0", TYPED),
Measures.[Date]
)
Member [Measures].[Person Count per Age Group] AS
CASE
WHEN [Age Group].[Age Group].CurrentMember is [Age Group].[Age Group].[0-9] THEN
Filter([Person].[Person Id].[Person Id].Members,
[Measures].[Age at Date] >= 0 AND [Measures].[Age at Date] < 10
).Count
WHEN [Age Group].[Age Group].CurrentMember is [Age Group].[Age Group].[10-19] THEN
Filter([Person].[Person Id].[Person Id].Members,
[Measures].[Age at Date] >= 10 AND [Measures].[Age at Date] < 20
).Count
...
END
This assumes that you have the date of each record in a measure named Measures.[Date]
directly in your fact table. If that would not be the case, you could get that date as a date data type possibly from the current member of the date dimension similarly how the birth date is derived via the Key0
property. I am assuming that you really use a Date data type in the dimension for the birth date attribute key, otherwise, a type conversion might be necessary. For purposes like that, there are many VBA functions available in Analysis Services (like the DateDiff
that I used above), as documented here.