Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
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

3) How do I obtain only the tickets created for a particular month based on the Opened date? I used to get this calculated using set analysis while using canonical calendar with the DateType field..what is the kind of set analysis that I can use to calculate the count of tickets created for a particular month and resolved for a month?

0 Likes
631 Views
robert99
Specialist III
Specialist III

Thanks for the reply

I can understand if you have a table without an expression (only dimensions) this issue might arise. But as soon as a expression is added count({<CanonType = {OrderDate}>}OrderNum) then this problem disappears doesn't it

" number of orders by each of the above dates."

I have never come across an issue using canonical dates. in your example I would simple set up three expressions

count({<CanonType = {OrderDate}>}OrderNum)  = OrdersRec

count({<CanonType = {ActualShip}>}OrderNum) = OrdersShipDateEst

count({<CanonType = {PlannedShip}>}OrderNum) = OrdersShepDateAct

If the user wants to show the actual shipping dates for orders received in Aug 2015. All I would do is use this expression count({<CanonType = {OrderDate}>}OrderNum)  and use a dimension of

month (ActualShippingDate)

Maybe Im missing the point but I have yet to come across an issue as explained above.

0 Likes
631 Views
robert99
Specialist III
Specialist III

Thanks for your reply

"user has selected a specific quarter/month/week but he will still see orders that were placed before his selection"

Not if he/she uses an expression as explained above.

I see you as a Qlik Guru as the way you think re Qlik and do things is usually 100% in alignment with me. I have followed totally many of you recommendations. Including the basics of your BoM recommendation although I changed it a lot. This canonical calendar was just what I was looking for. Its brilliant. Perfect for me (I used date Islands a lot before).

But on this I still think you are not only wrong. But its a bad way to do this (although I may see the light one day). I have seen users produce the wrong figures by using the wrong calendar. And as  a user I have done it myself. Its just far too confusing with lots of calendars. Its a pain when on most occasions thanks to Canonical Dates it can be avoided

I overcame this issue in VIEW by using triggers. By removing all calendar selections when entering and leaving a sheet. And only giving users access to the right calendar for a sheet. But using triggers is a pain.

But this is not possible in SENSE. Just yesterday I had a user ring up and ask why a count was wrong. Reason. even though I gave a non CanonDate calendar an odd name he still used it. And he is a very enthusiastic user of Qlik.

0 Likes
631 Views
hic
Former Employee
Former Employee

Sanjyot

It seems as if your question demands a long, elaborate answer, so I suggest you post it in a thread of its own. That way you will increase visibility and get many good answers and suggestions. 

Concerning the Master Calendar: There are many good posts about master calendars in the forum, so I suggest you search there. I usually the following structure - but with more fields in the preceding Load:

// ========= First load your table with the Date field


MinMaxDate:
Load   Num(Min(Fieldvalue('Date',RecNo()))) as MinDate
   Num(Max(Fieldvalue('Date',RecNo()))) as MaxDate 
   Autogenerate FieldValueCount('Date');

Let vMinDate  = Peek('MinDate',0,'MinMaxDate');
Let vMaxDate  = Peek('MaxDate',0,'MinMaxDate');

Let vStartDate = YearStart(vMinDate)-1;
Let vEndDate  = Floor(YearEnd(vMaxDate));

Calendar:
Load   Year(Date)         as Year,
  
Ceil(Month(Date)/3)   as Quarter,
   Month(Date)           as Month,
   Date;
Load  Date($(vStartDate)+RecNo()) as Date
   Autogenerate vEndDate - vStartDate ;

0 Likes
631 Views
hic
Former Employee
Former Employee

If you have multiple dates in your data model, you will have misunderstandings whatever solution you choose. You're damned if you do, and you're damned if you don't. So, I am fine with whatever solution you choose.

My point is only that I think it is easier to explain if you you have the explicit dates as well as the canonical date. Then the user will eventually learn. But if you don't have the explicit dates, the user will never understand what goes on behind the curtain.

Further, with your method, the user can never make a selection like "ordered in July, but shipped in September". It is just not possible.

631 Views
Anonymous
Not applicable

Thanks Henric. I have been following your blogs for most of my development activity and your suggestions have always guided me correctly. I have tried to use both the approaches i.e. canonical as well as generation of reference dates. The only issue with this is that it takes long time to execute the scripts.. I did post my initial query in the forum but didnt receive any response on it.

0 Likes
631 Views
cbushey1
Creator III
Creator III

hic‌,

I wanted to reach out to you since you seem to be the expert on Date table (among many other things). I have a scenario that I have been struggling with and was hoping for your opinion on where to go. I am leaning towards this Canonical approach to having one master calendar but wanted thought you could shed some oversight on my data model and the best approach.

I have two calendars in my data model. One is the master calendar which is created at the sql level and brought in as a table and the other is an Account Summary Calendar generated using Qlik code from a field in the Account Summary table. Account summary contains 1 row for each accountID and month. It holds summary level data on that accounts revenue.

My master calendar is based off the events table (eventDate) and is much more granular. The event table sort of acts as the date bridge you have outlined as it has all the dates throughout the rest of the data model in event date with an event name that corresponds to that event.

We want the user to select year(s) or month(s) from the master calendar and return information that is housed at the account summary level. Right now this will not work. Here is what our current data model looks like (part of it at least).

If you could let me know where you think I should go next to get at having one filter cascade down both events and Account summary based on that year or month I would appreciate it.

datamodel_community.png

0 Likes
627 Views
hic
Former Employee
Former Employee

This is tricky...

First of all, you could claim that the summary data is redundant - it is probably just a summary of what you already have in other tables. And as such it doesn't really belong in the same app... But I realise that you (or the users) want it there for e.g. comparison purposes, so may perhaps not be an option to remove it.

So, I would then probably regard it as a "second fact table" with different granularity than the Activity table, which I guess is the "real" fact table. The solution would be to concatenate the two fact tables (AccountSummary and Activity) and use the same keys (when appropriate). See Fact Table with Mixed Granularity and Generic keys. In a way, you can claim that making a monthly account summary is an activity.


One problem is however that you don't have a date in your activities table. One solution could be to join the Activities table with the Events table before you concatenate the AccountSummary onto it. Another solution could be to add extra records to the Events table - basically an ActivityID that would link all dates to the relevant records in the AccountSummary part of the fact table.


Another problem is that there are keys in the Activities table that don't exist in the AccountSummary table. You would need Generic keys to solve this.

Bottom line: Changing the data model to solve this could work, but it would need a lot of work and involve many challenges.

So, perhaps a completely different approach is better: You could leave the data model as it is, and display measures in charts using if()-conditions. For example, say that you have a chart with month as dimension, and you want to display both Count(CaseID) - which is linked to the event date - and Sum(ActualRevenue) - which is linked to the SummaryMonth.

Then you could probably use SummaryMonth as dimension and

Count( If(SummaryMonth=Date_Month, CaseID ) )

Sum(ActualRevenue)

as measures. But verify that the numbers are right. Using if() this way sometimes leads to record duplication and incorrect numbers.


Good Luck


HIC

0 Likes
627 Views
hic
Former Employee
Former Employee

I just realized...

There could exist a completely different solution: If you create a composite key of Account and YearMonth, in your Activity table, then you could use this to link your AccountSummary to this instead of to the Accounts table. 

I assume that an Activity always belongs to a specific month. If not, you can probably do the same thing to the Events table instead.

HIC

0 Likes
627 Views
cbushey1
Creator III
Creator III

Thank you for the quick and thorough response hic‌.

I think changing the data model at this juncture is going to be a tough sell since we just added this summary table into the existing structure. The summary information wasn't already being captured elsewhere in the data model and that is why we needed to add it.

I like your idea of creating a composite key of AccountID and YearMonth as this will make each row have a unique key that can then be joined to either Activities or Events.

If I understood correctly, I would make this key in my Account Summary table using the fields (accountId and SummaryMonth) and in either Events or Activities table I would make this key using AccountId and Date_YM (from the Master calendar). Thus giving me the same values to link on.

0 Likes
627 Views