Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
hic
Former Employee
Former Employee

 

A common situation when loading data into a Qlik document is that the data model contains several dates. For instance, in order data you often have one order date, one required date and one shipped date.

 

Base model.png

 

This means that one single order can have multiple dates; in my example one OrderDate, one RequiredDate and several ShippedDates - if the order is split into several shipments:

 

Logic 1.png

 

So, how would you link a master calendar to this?

 

Well, the question is incorrectly posed. You should not use one single master calendar for this. You should use several. You should create three master calendars.

 

The reason is that the different dates are indeed different attributes, and you don’t want to treat them as the same date. By creating several master calendars, you will enable your users to make advanced selections like “orders placed in April but delivered in June”. See more on Why You sometimes should Load a Master Table several times.

 

Your data model will then look like this:

 

Model with spec calendars.png

 

But several different master calendars will not solve all problems. You can for instance not plot ordered amount and shipped amount in the same graph using a common time axis. For this you need a date that can represent all three dates – you need a Canonical Date. This is how you create it:

 

First you must find a table with a grain fine enough; a table where each record only has one value of each date type associated. In my example this would be the OrderLines table, since a specific order line uniquely defines all three dates. Compare this with the Orders table, where a specific order uniquely defines OrderDate and RequiredDate, but still can have several values in ShippedDate. The Orders table does not have a grain fine enough.

 

This table should link to a new table – a Date bridge – that lists all possible dates for each key value, i.e. a specific OrderLineID has three different canonical dates associated with it. Finally, you create a master calendar for the canonical date field.

 

Full model.png

 

You may need to use ApplyMap() to create this table, e.g. using the following script:

 

     DateBridge:
     Load
          OrderLineID,
          Applymap('OrderID2OrderDate',OrderID,Null()) as CanonicalDate,
          'Order' as DateType
          Resident OrderLines;

     Load
          OrderLineID,
          Applymap('OrderID2RequiredDate',OrderID,Null()) as CanonicalDate,
          'Required' as DateType
          Resident OrderLines;

     Load
          OrderLineID,
          ShippedDate as CanonicalDate,
          'Shipped' as DateType
          Resident OrderLines;

 

If you now want to make a chart comparing ordered and shipped amounts, all you need to do is to create it using a canonical calendar field as dimension, and two expressions that contain Set Analysis expressions:

 

     Sum( {$<DateType={'Order'}>} Amount )
     Sum( {$<DateType={'Shipped'}>} Amount )

 

Bar chart.png

 

The canonical calendar fields are excellent to use as dimensions in charts, but are somewhat confusing when used for selections. For this, the fields from the standard calendars are often better.

 

Summary:

  • Create a master calendar for each date. Use these for list boxes and selections.
  • Create a canonical date with a canonical calendar. Use these fields as dimension in charts.
  • Use the DateType field in a Set Expression in the charts.

 

A good alternative description of the same problem can be found here. Thank you, Rob, for inspiration and good discussions.

 

HIC

192 Comments
cbushey1
Creator III
Creator III

hic‌,

I am hoping you might be able to quickly assist. I like your method here for creating one calendar when it is necessary and can certainly see the benefits of having the individual ones. With that said, I think something is off with my Canonical Calendar because when I select CanonicalYear = 2014, I show my three date fields  (Expired, FocusDate, CodeDate) as (03/23/2014,02/09/2015,03/23/2014). I wouldn't have expected the FocusDate to show since its year is 2015.

Any thoughts on why this might be?

Chase

0 Likes
1,163 Views
hic
Former Employee
Former Employee

This is most likely completely correct.

By selecting CanonicalYear=2014, you pick out all the OrderIDs (or whatever you use as key) where one of the three days belongs to 2014 - but not necessary all three. So, if you have an OrderID with ExiredDate in 2014, it may well have a FocusDate that belongs to 2015.

HIC

0 Likes
1,163 Views
Anonymous
Not applicable

Anyone know why it is called a "Canonical Date" ?

0 Likes
1,163 Views
mountaindude
Partner Ambassador
Partner Ambassador

"Canonical" is a term used in computer science, physics, mathematics and other fields to explain that something is on a standardised form, where each object has a unique representation.

In the case of a calendar, a canonical calendar would be one that has a entry date for each date, like HIC described.

Think of a canonical calendar as "one calendar to rule them all", with only one record for each unique date.

A "normal" form is similar, with the difference that objects (e.g. dates) do not need to have unique representations.

It's been oh-so-many years since I learnt this stuff at university... so I am sure someone will correct me and/or provide additional details.

1,163 Views
hic
Former Employee
Former Employee

Because I chose to call it this when I wrote the blog.

The concept of an official, proper, unified, generalized date in the data model existed before the blog post - but it didn't have a name. In one discussion I had, an English-speaking user described it as a "canonical form" of the date. This term stuck in my head, and I used it when writing the blog.

HIC

0 Likes
1,163 Views
mountaindude
Partner Ambassador
Partner Ambassador

...and of course Wikipedia has a pretty extensive article on the topic...

Canonical form - Wikipedia, the free encyclopedia

0 Likes
1,163 Views
Anonymous
Not applicable

That explains why I could find no suitable explanation of "Canonical Date" on the web.

Thanks for the clarification, I was starting to worry the web had broken.

0 Likes
1,117 Views
tiago_hubner
Partner - Contributor III
Partner - Contributor III

Great Post.

0 Likes
1,117 Views
jkampmeijer
Contributor II
Contributor II

Thanks for the post.

I will use the Canonical Date solution in my dashboard.

0 Likes
1,117 Views
nizamsha
Specialist II
Specialist II

Hi Henric,

I am little bit confused about the mapping in the bridge table here is your code.

BridgeTable:

    Load OrderLineID, Applymap('OrderID2OrderDate',OrderID,Null()) as CanonicalDate, 'Order' as DateType

          Resident OrderLines;

     Load OrderLineID, Applymap('OrderID2RequiredDate',OrderID,Null()) as CanonicalDate, 'Required' as DateType

          Resident OrderLines;

     Load OrderLineID, ShippedDate as CanonicalDate, 'Shipped' as DateType

          Resident OrderLines;

In Bridge table you have 3 columns and they are DateType,OrderLineID,CanonicalDate

In Order Table we dont have OrderLineID then how did you created the 'Order' as DataType and 'Required' as DataType my Question how did you take these column if i am not wrong  Your first   mapping is from OrderLineTable

and the column are orderID and orderLineITem based on this you might have used the applymap in ordertable and then you might have created 2 mapping table one is OrderID2OrderDate and the second one is OrderID2RequiredDate if it so you might have created a bridge table using these mapping tables right orders and required is from OrderTable but how did you done from OrderLine  i am little bit confused how you have taken the mapping kindly clarify where was the gap

0 Likes
1,117 Views