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.
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:
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:
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.
You may need to use ApplyMap() to create this table, e.g. using the following script:
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:
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.
Unfortunately even after changing the min and max dates the CanonicalMonth is not linking to the MasterTicketDetails table. I'm getting the same issue as I had listed in my last screenshot (multiple CanonicalDates show values while CanonicalMonth is grey when a ticket is selected). Could difference in date format play any role here? I tried a lot of permutations and combinations of changing my code, but none came to my rescue.
Hence I'm inserting the my code, as I'm sure you could pick up where I'm going wrong much easily.(unable to attach the file hence pasting the long code below, apologies for the untidy work.)
The tables were not linking because of the time stamp, it contained fractional time. Using the steps in the document I was able to convert them into integer.
Thank you so much for helping me out in this. For a two weeks old QlikView newbie your timely inputs have been very encouraging and helped me discover a lot and I'm falling in love with this tool.
Looking forward to learning more from you out here.
hic Thanks a lot. This was a solution I was looking for.
But if we don't have all the OrderIDs in OrderLines table, then those OrderDates and RequiredDates will not get added to the DateBridge table right? Is there a way to solve it too?
If you don't have the OrderID in the OrderLines table, then you have other problems: Then you don't have the necessary link...
First, you need to find the table with the finest grain. This does not have to be the OrderLines table. There could be a table with even finer grain, e.g. the InvoiceLines table. Then you should use this table.
Secondly, you may need to use Applymap() to move a date into the Load statement of the data bridge table. This way you can move a field from any table into this Load statement.
Would it be able to construct a canonical calendar in this setup:
The utilization calendar gives the utilization of different aircraft. Whereas the CalenderTaskPerf gives the dates on which a maintenance task has been performed on the aircraft. Now I came across an issue when constructing graphs, that I want to plot the utilization in relation to performed tasks in one graph.
But of course they have both two different dimensions, since there are two different calendars. I cannot directly link the calendars because it will create a loop. What links both these calendars is the event_perfno_ii. For a certain date where there is a task performed there doesnt have to be a utilization date, but it is possible..
So is it even possible to make use of canonical calendar?
If an "ac_registr" can have several "UtilizationDates", or an "event_transferno_i" can have several "TaskPerfDates", then you will indeed have problems creating one single Canonical Date. There just is no field that you can use as key.
It could perhaps be possible if you change the data model, though. If you join the tables and create a composite key based on the primary keys of Utilization and TASK_CHECK_LIST, then it should be possible. (However, if you have a many-to-many relationship between the two, this model would be inefficient.)