Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
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
datanibbler
Champion
Champion


very good post! Just a bit hard to understand for "the rest of us". I'll have to read it once more. Right now, I have only one scenario for that - where I currently use two master_calendars - but going forward, there are sure to be more.

Best regards,

DataNibbler

88,961 Views
Not applicable

very clever

Thanks for so good explanation

Fabrice

0 Likes
88,961 Views
Not applicable

As always Great! Useful Past !

Good Explanation.

Regards,

Divya

0 Likes
88,961 Views
peschu123
Partner - Creator III
Partner - Creator III

Thank you for summarizing this in a post.

I have several dates in almost all my reports and it was one of the first (harder) things when I got in contact with QV.

An example how you use it in the user interface would be great. I often experience that users don't understand the usage or the methodology behind it. Perhaps I make it to complicated.

Another point I would add is how to handle forecast values. There are often "Planned shipment date" and/or a "Shipped Date" for example. Then you will have too much calenders. I mostly merge them to a field like "Shipment Date". ( with a Flag field "forecast" (yes,no) )

This lets you use one dimension in the chart even with forecast values.

88,961 Views
Not applicable

Hey HIC,

Great post as alway !

Somehow in most of the discussion with the client, I find they are keen to have just one calendar (rather than having separate calendars for the disparate important dates).

Thanks,

SK

0 Likes
88,961 Views
Not applicable

very good post

0 Likes
88,961 Views
IAMDV
Luminary Alumni
Luminary Alumni

Hi HIC,

Thank you for another great post and your clarity of thought is very good. I just wanted to clarify two things here.

1. Reading the below script you meant "CanonicalDate" instead of "Date" as the field name? Or am I missing something here...

     DateBridge:

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

          Resident OrderLines;

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

          Resident OrderLines;

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

          Resident OrderLines;

2. Do we need the "Distinct" prefix while loading these tables?

Thanks,

DV

73,862 Views
hic
Former Employee
Former Employee

1. You are of course right. It should be "CanonicalDate".

2. It could be useful, but it wouldn't make a big difference. Duplicates can only occur if several order lines are shipped on the same day. And a distinct clause would remove these.

HIC

73,862 Views
IAMDV
Luminary Alumni
Luminary Alumni

Thank you HIC. Cheers!

0 Likes
73,862 Views
sundarakumar
Specialist II
Specialist II

Great Post... Thanks a lot...

73,862 Views