Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Suppose we have a Period table which contains some of the typical period columns, like Period_Key, Day, Month, Year.

In the table, we have typical Period rows, like

PeriodTable1

Now we want to create a Period Dimension from it.

We can typically create a datasource and data source view in the Analysis service project. I am not going in detail of it. Now we can create a dimension from this Period table by using the dimension wizard.

This will start the dimension wizard. We can select to create the dimension from an existing table, which is our Period table.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

As the table for the Dimension, we will choose the Period table. For the Key column of this dimension, we choose PeriodKey. For display name also (Name Column), we choose PeriodKey.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

For the Dimension attributes we choose all the four available attributes. We will surely configure them later. For now, we keep them as regular type

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Then we are about to finish creating our Period Dimension with the following step.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

When we are done creating our dimension, we will see this window.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Now we want to go into our own customized that is now provided by the wizard. We will create hierarchies and additionally resolve some of the issues we get creating the hierarchies.

At this stage, we can process the dimension and it will be successfully processed. But we want to create the hierarchies. We want a natural period hierarchy which will be like

Year –> Month –> Day

We can drag the Year attribute in the Hierarchies panel (middle panel). Then we can drag the month attribute and keep it below year and similarly drag Day attribute and put it below Month in the hierarchy. The final hierarchy should look like this.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Here comes one interesting thing. We see a warning in the Hierarchy.

Warning:  Attribute relationships do not exist between one or more levels of this hierarchy.  This may result in decreased query performance.

Now what is attribute relationship. At the right side of the Dimension structure, we see a tab named Attribute relationship.

If we click on that we will see the default relationship between the attributes that is set by the wizard.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

This means how the data will be gathered for all the attributes. PeriodKey is the key of this dimension. So the cube will get the data for PeriodKey attribute from the underlying table. Then if anyone wants to see the data at year level, it will aggregate the data for the PeriodKey and will give the value. Same thing will happen for Month and Day. But we know, to get the Year level value, we done need to aggregate all the  data at PeriodKey level. We can easily aggregate the data at Month level and this should be about 30 times less that the Day level data. So this will increase the query performance. If we need to aggregate the Day level data for Year, this will not be good for the performance. That’s the thing the warning message is mentioning.

So we can set the relation like below. Click on Month and drag it to Year. Again Click on Day, drag it to Month.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Now if we go to the Dimension structure tab where we first were, we will see the warning gone.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Remember: If you don’t set the attribute relationship, then also you will get the correct value, but it may be slow based on your data volume. Sometimes in other dimension, we create the Hierarchy, but we really don’t want to set the attribute relation according to the levels. We want all the levels getting data from attribute key. But that is a different scenario.

Now we can process the Dimension. But here comes another big surprise. The processing fails.

It gives a duplicate key error. Why?

Now we should look into another thing. The properties of each attribute of the dimension.

Click on the Year attribute and look at the properties section.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

See the KeyColumns and NameColumn properties.

KeyColumn is used to get the members of the attribute from the underlying table. It issues a distinct query on the table for the key columns. The returned rows will be the member of this attribute. The name column is used for these members for the display value.

For YEAR, we have Period.Year as key columns. So it will give us the year members, that are typically … ,2008, 2009,2010 , …

This is ok for us.

For MONTH we can will see the following:

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Period.Month is used as the Key Columns. This looks ok, but it is not. If we take distinct Month from Period table, we will get 1,2,3,4,……..,29,30,31. But see that Month is below Year in the Hierarchy. As soon as we create a hierarchy, it will relate the Year members with the Month members as a tree structure, where year members will be parent of month members. One important thing is: one parent member can have multiple children. But one child member cannot have multiple parents. But in our case, Month “1” has more than one parents which is all the years. Same for all other months. So we need to have multiple “1” member at month level, each one for each year. Essentially we want to have a structure like

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

So if issue a distinct query on Period table like this:

Select distinct Year, Month

From Period

Then we will get like:

2008

1

2008

2

2008

3

2009

1

2009

2

2009

3

2010

1

2010

2

2010

3

Thus we will get all the months members replicated for each year. In this way we can eliminate the “one child multiple parent ” problem.

Now if we change the KeyColumns for the Month attribute as below, then we can get this implemented.

Click on the button to open the KeyColumns setting window.

We will see that only Month is in the right panel.

We will add the year column also.

After clicking  OK, we will see an error message in the Month attribute.

The NameColumn should be defined because there are multiple KeyColumns.

Quite Natural, isn’t it?

Now below the KeyColumns,  we can set the NameColumn. We will set the NameColumn as Month. So, we want multiple members to be generated for the same month, but that should be displayed as same, isn’t it?

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Now we have our KeyColumns as a Collection of Year and Month, and NameColumn as Month.

This same thing we should do for the Day attribute. Use Year, Month and Day for the KeyColumns, and Day for the Name Column.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

Now we have already removed the duplicate key error, which in fact occurs mostly for single child, multiple parent case.

Process the dimension and you can see a nice Period Hierarchy in the Dimension browser.

Period Dimension (Time Dimension) Creation with Year, Month, Day Hierarchy

你可能感兴趣的:(time)