๐ Calendar Dimension
This article introduces how to create a calendar-type dimension for a semantic model and how to use it in a story dashboard.
Creating a Calendar Shared Dimensionโ
When it comes to time-related analysis, data often needs to be aggregated at different time granularities, such as year, quarter, month, and day. Therefore, we need to specify the time dimension for the multidimensional model during modeling. The data for the time dimension should be fixed data unrelated to actual business data, such as a list of dates where the month and year do not vary between companies. Thus, we only need to generate the corresponding table data as needed.
In this article, we use the time_by_day
table as the calendar table to create a shared time dimension. The previous article Shared Dimension has already introduced how to create a shared dimension, so we won't repeat it here.
Steps are as follows:
- Create a new shared dimension based on the
time_by_day
table, name itTime
, label: Calendar; - After creation, it defaults to the default hierarchy, open the property settings interface, and modify the Primary Key:
time_id
- In the dimension tab, modify the dimension Type: Time
- Create the following level fields:
- Year
- Label: Year,
- Name: Year,
- Field:
the_year
, - Unique Members:
true
, - Time Level Type:
Year
- Semantic Type: Calendar Year
- Quarter
- Label: Quarter,
- Name: Quarter,
- Field:
quarter
, - Unique Members:
false
, - Time Level Type:
Quarter
- Semantic Type: Calendar Quarter,
- Time Formatter:
[yyyy].['Q'Q]
- Month
- Label: Month,
- Name: Month,
- Field:
month_of_year
, - Unique Members:
false
, - Caption Field:
the_month
, - Time Level Type: Month
- Semantic Type: Calendar Month,
- Time Formatter:
[yyyy].['Q'Q].[M]
- Day
- Label: Day,
- Name: Day,
- Field:
the_date
, - Unique Members:
true
, - Time Level Type: Day
- Semantic Type: Calendar Day,
- Time Formatter:
[yyyy].['Q'Q].[M].[yyyy-MM-dd]
- Year
- Save
Points to note:
- Only the Year and Day fields have the Unique Members attribute set to
true
, while Quarter and Month fields arefalse
, because the values ofquarter
andmonth_of_year
cannot independently serve as summary dimensions, otherwise the summary results would not be meaningful. - We specified the Caption Column for Month as
the_month
, so that when displayed, the month description is not just in numeric form but in language text (here, English words). - The dimension type is set to Time, and each level must have a Level Type set, so the analysis engine can correctly parse calendar-related functions, such as:
ParallelPeriod([level[, index[, member]]])
PeriodsToDate([level[, member]])
WTD([member])
MTD([member])
QTD([member])
YTD([member])
LastPeriod(index[, member])
- The semantic type corresponds to the Time Level Type, and the Formatter attribute is for the front-end program to convert the system's current Date into the actual value of the multidimensional model, referring to the date formatting format of https://date-fns.org/.
Creating Multiple Hierarchiesโ
In addition to the default hierarchy, we can also create multiple hierarchies for a dimension, for example, creating a hierarchy based on the week convention:
Click the Dimension menu in the menu, click New Hierarchy, go to the new tab, and enter the following information in the Hierarchy property settings interface:
- Name: Weekly
- Caption: Weekly Calendar
- Dimension Table, Table Name:
time_by_day
- Primary Key:
time_id
Create the following level fields:
- Year
- Field:
the_year
, - Unique Members:
true
, - Time Level Type: Year,
- Semantic Type: Calendar Year
- Field:
- Week
- Field:
week_of_year
, - Unique Members:
false
, - Time Level Type: Week,
- Semantic Type: Calendar Week,
- Time Formatter:
[yyyy].[W]
- Field:
- Day
- Field:
day_of_month
, - Unique Members:
false
, - Time Level Type: Day,
- Semantic Type: Calendar Day,
- Time Formatter:
[yyyy].[W].[Do]
- Field:
Adding the Calendar Dimension to the Multidimensional Modelโ
Open the Sales model created in the Multidimensional Data Model, and drag the calendar dimension (Time) into the dimension area. Select the Time dimension and modify its Foreign Key: time_id
. After saving, you can use the calendar dimension of the model.