Skip to main content
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
Anonymous
Not applicable

Hi All,

How would change the model to handle a second fact table, with a new "datetype"?

Regards

Birkir

0 Likes
1,497 Views
hic
Former Employee
Former Employee

In principle the same way.

First of all, if you have two fact tables, then I would recommend that you concatenate them into one table. That is is most cases the best solution. The primary key of this table can then be used for a canonical date.

If you instead use a link table, you should use the primary key of the link table.

HIC

0 Likes
1,497 Views
Anonymous
Not applicable

Hi HIC,

Thank you for your reply. Yes I could concatenate the tables, would you then just create some sort of sequence (uniqueID per row) as a primary key?  But what if we would add a third facttable and the fourth, just the same ?

Regards

Birkir

0 Likes
1,497 Views
hic
Former Employee
Former Employee

You could use a unique ID, yes. E.g. RowNo() as ID.

But more likely, you know your data so well so you know which keys you need to uniquely specify a date. It could be a combination of the other keys you already have, e.g.

     Autonumber( OrderLine & IsBudget & BudgetMonth ) as Key

Note that some keys (BudgetMonth in my example) may well be NULL for some of the data.

HIC

0 Likes
1,497 Views
Not applicable

Hi Henric,

Thanks for this Post!, Working from long time on QLikView, but things only got a catch when they exactly require. So your post given the idea to show trend line using CanonicalDate for Shipped Qty and Received Qty. Even I am using Master calendar separate which is linked on Order Date. but  Master calendar only set the Variable value and I can take that Date as Current date and easy to show MTD Values.... Hope It will help someone to get what exactly can be done.

Thanks

Regards

0 Likes
1,497 Views
richard_pearce6
Luminary Alumni
Luminary Alumni

Here's a 'AsOf' Calendar script which makes set analysis very simple which may also be of use and can be used in conjunction with this method.

http://community.qlik.com/docs/DOC-6593

Regards

Richard

QlikCentral.Com

0 Likes
1,497 Views
Not applicable

Hi Henric

Simply amazing how you explain those topics in easily, understandable words!

Another great post that gets to my favourites...

Brgds,

Anthony

0 Likes
1,447 Views
yduval75
Partner - Creator III
Partner - Creator III

Hello,

Its' very interesting but I have a problem.

How can I do if I have 2 shipped dates for 1 orderline ?

It doesn't operate and I can't join the orderline table in the orders table because I have amounts in the two tables.

Thanks for your answer.

Yoann

0 Likes
1,447 Views
Not applicable

Hello Henric,

thanks for the explanation.

But how do I proceed if I want to create such a calendar with data that is not "linked"?

In your example you have a Shipping Date, Order Date and Required Date, but everything belongs to the same Order.

I have the following problem as I am trying to connect transaction data of customers with their registration and activation into one calendar:

There are two databases for our customers, one which contains two dates, the one at which the customer registered and the one at which he became active.

In the second database there are the transactions he has been doing over the time.

I am now trying to put this into one calendar so I can see -by filtering-, how many customers registered in a certain month, as well as how many transactions were processed.

I think the problem here is that there is only 1 date of registration resp. activation for a customer but there can be more than one date of transaction.

My code looks like this:

DateLink:

Load

  fk_merchant_id,

  month(DateReg) as Month,

  year(DateReg) as Year,

  'Reg' as DateType

Resident DB1;

Load

  fk_merchant_id,

  month(DateAct) as Month,

  year(DateAct) as Year,

  'Act' as DateType

Resident DB1;

Load

  fk_merchant_id,

  month(transactionDate) as Month,

  year(transactionDate) as Year,

  'TRX' as DateType

Resident DB2;

When I filter for a month it shows me the correct Registrations and activations but the TRx Data is accumulated until that very date.

Thank you for any adivise.

Benjamin

0 Likes
1,447 Views
hic
Former Employee
Former Employee

You need to link your DateLink table to the Transaction table via a Transaction_ID and not via the fk_merchant_id. Then it will work.

HIC

0 Likes
1,447 Views