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.
You don't need three different master calendars. You need only one and within this one master calendar you would include a common or canonical week, year and months etc.
And having all the dates in one table should make it easier to set up (a canonical date). As the issue of using the table with the finer grain does not apply. But make sure this table has a field with a different (unique) reference for every row in this table.
If PKey isn't then create one using say rowno() . But then just follow the instructions in HIC's blog post.
Hello, I'm new to Qlik and trying out this code to get a month by month chart with total number of open cases and number of closed cases. So far in my tests I've created a master list for both open and closed dates and tried creating a what I would think a simple chart similar to the "Ordered and Shipped amounts" but to no avail.
My data is simple I feel like I'm almost there but am missing something. Any help would be appreciated.
Hi all, is there any alternative to this? This article has been very helpful but with the amount of data I have, replicating each row multiple times is causing severe performance issues.
But how can I create master calendar with tables are not associate and linked to each other? Based on your example, date that you used was linked which each other using key'Order ID' and 'OrderLineID' where data is related to order.
In my scenario, I have 3 facts table which are not associate to each other with different key. Actually, I created a data model using other tool and load these 3 tables (PPV, Billing and Sales) in qlikview. That's why I have a problem to link the tables and create the master calendar since the data is not related to each other. Hope you can give me an advice.
Code that I've tried as below:
DateBridge: Load PPV_PONO, BUDAT as CanonicalDate, 'Date' as DateType Resident PPV;
Load Bill_Doc, Bill_Date as CanonicalDate, 'BillDate' as DateType Resident Billing;
Load Sales_Doc, Sales_DeliveryDate as CanonicalDate, 'SalesDeliveryDate' as DateType Resident SalesOrder;
QuartersMap: MAPPING LOAD rowno() as Month, 'Q' & Ceil (rowno()/3) as Quarter AUTOGENERATE (12);
Temp: Load min( RangeMin( "BUDAT", "Bill_Date", "Sales_DeliveryDate" )) as minDate, max( RangeMax( "BUDAT", "Bill_Date", "Sales_DeliveryDate" )) as maxDate;
Let varMinDate = Num(Peek('minDate', 0, 'Temp')); Let varMaxDate = Num(Peek('maxDate', 0, 'Temp')); DROP Table Temp;
TempCalendar: LOAD $(varMinDate) + Iterno()-1 As Num, Date($(varMinDate) + IterNo() - 1) as TempDate AutoGenerate 1 While $(varMinDate) + IterNo() -1 <= $(varMaxDate);
CanonicalCalendar: Load
TempDate AS CanonicalDate, week(TempDate) As CanonicalWeek, Year(TempDate) As CanonicalYear, Month(TempDate) As CanonicalMonth, Day(TempDate) As CanonicalDay, YeartoDate(TempDate)*-1 as CurYTDFlag, YeartoDate(TempDate,-1)*-1 as LastYTDFlag, inyear(TempDate, Monthstart($(varMaxDate)),-1) as RC12, date(monthstart(TempDate), 'MMM-YYYY') as CanonicalMonthYear, ApplyMap('QuartersMap', month(TempDate), Null()) as CanonicalQuarter, Week(weekstart(TempDate)) & '-' & WeekYear(TempDate) as CanonicalWeekYear, WeekDay(TempDate) as CanonicalWeekDay Resident TempCalendar Order By TempDate ASC; Drop Table TempCalendar;
One option is to concatenate (or merge) the three tables. With one column named for example DataDate. And have the calendar linked to this field. In this way you don't need a canonical date unless one table has more than one date.