Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

One Qlik function that occasionally causes confusion is the Date function. I have often seen errors caused by an incorrect usage of it, so today I will try to explain what the function does – and what it does not.

Interpretation vs Formatting

The first thing you should be aware of is that there are two different functions: Date#() and Date(). The first is an Interpretation function and the second is a Formatting function.

 

  • Interpretation functions use the textual value of the input, and convert this to a number.
  • Formatting functions use the numeric value of the input, and convert this to a text.

 

In both cases, the output is a dual, i.e. it has both a textual value and a numeric value. The textual value is displayed, whereas the numeric value is used for all numerical calculations and sorting.

 

The table below shows how to use the interpretation function Date#(). Note that the format code must match the input parameter.

 

Interpretation.png

 

This is very different from the formatting function Date(). Next table shows how to use this function. Note that the format code matches the format of the output text.

 

Formatting1.png

 

In real life, it is often useful to nest an interpretation function inside a formatting function:

 

Nested.png

 

Formatting vs Rounding

The second thing you should be aware of is that the Date() function and other formatting functions never change the numeric value of the input value.

 

This means that you can format a timestamp as a date only, without the time information. This can sometimes be confusing since there is a “hidden” value. In the table below, you can see that the input value corresponds to 12:00 in the middle of the day, but the Date() function effectively hides this from the textual output - but it remains in the the numeric value.

 

Formatting2.png

 

So what should you do if you want to remove the time part of the field, and just keep the date part? Well, obviously you must use a function that changes the numeric value: You need a Rounding function, e.g. DayStart() or Floor().

 

In the table below, you can compare the output of the Date() function with a couple of different rounding and formatting options.

 

Rounding.png

 

Summary

The above discussion is not relevant to dates only. It is just as relevant for Years, Weeks, hours, seconds and any other time interval. Further, it is relevant to a number of other functions:

 

Interpretation functions: Date#(), TimeStamp#(), Time#(), Interval#(), etc.

Formatting functions: Date(), TimeStamp(), Time(), Interval(), etc.

Rounding functions: Round(), Floor(), Ceil(), DayStart(), WeekStart(), MonthStart(), etc.

 

Combine these functions sensibly, and you will be able to round or format any way you want.

 

HIC

 

Further reading related to this topic:

Get the Dates Right

Why don’t my dates work?

34 Comments
Creator II
Creator II

Thank you for this very clear Blog, hic. My problem with dates has been down to my difficulty understanding the QlikView help screens/User guide.  What does it take to get the "help screen" writers to consult with you before they put in their examples?

7,937 Views
Partner
Partner

Ah, I had it all but the automatic date interpretation of the resulting string. Thank you!

0 Likes
7,937 Views
Contributor
Contributor

Hello,

I would like to have in text object the date with format 16th of may 2015. How can i have the "th" after the day?

Kind Regards

Sébastien

0 Likes
7,937 Views
Creator II
Creator II

I don't think you can get this from a build in function in QV, so you might have to build your date as a string where you add the 'xx' to the date.

Just be aware that you'll have to check the date, because 'th' is not used for all date. It's e.g. the 1st. , 2nd, 3rd, 4th., ...21st....31st., so it's not all 'th'.

Regards

Steen

0 Likes
7,937 Views
Creator II
Creator II

update: See below

@sebbrunie1

My understanding is the same as sspe_dgs.com. - you will have to create a text (string) value.  If it helps, this is the rather cumbersome expression I use in my script.

First, the map...

ordMap:

Mapping LOAD * Inline [

From,To
1, 1st
2, 2nd
3, 3rd
]
;

and then the expression...

if(len(day(DateRecorded))=1,ApplyMap('ordMap',day(DateRecorded),right(day(DateRecorded),1)&'th'),left(DateRecorded,1)&ApplyMap('ordMap',right(day(DateRecorded),1),right(day(DateRecorded),1)&'th')) as OrdinalDay

Update 16-05-2016:

I look at this today and wonder how long have I been doing this so badly wrong! As mentioned by sspe_dgs.com‌ (Steen Schlüter Persson) below, it just a simple applymap for the exceptions:

ApplyMap('ordMap',day(DateRecorded),day(DateRecorded)&'th') as OrdinalDay

...and, of course, the missing lines from the map itself...

21, 21st

22, 22nd

23, 23rd

31, 31st

0 Likes
7,937 Views
Creator II
Creator II

Along the same line as the above suggestion, you can also try this script -

set vDateStart = '01-04-2015';
set vDateEnd = '01-05-2015';
 
DateMap:
Mapping LOAD * Inline [
From,To
1, 1st
2, 2nd
3, 3rd
21, 21st
31, 31st
  ]
;
 
[Date]:
 
LOAD *, ApplyMap('DateMap',DAY(Temp_Date), DAY(Temp_Date)&'th') &' '& MonthName(Temp_Date)&' '& Year(Temp_Date) AS NewDate ;
LOAD
date( date#('$(vDateStart)','DD-MM-YYYY')-1 + recno() ,'DD-MM-YYYY') as Temp_Date
AUTOGENERATE (date#('$(vDateEnd)')-date#('$(vDateStart)'))+1
;

It's simply using a Map table to hold all the "exception" dates (the ones that shouldn't have "th" applied). If a match is found using the ApplyMap it will use the "To" value from the Map table - otherwise it will apply "th" as the default.

You can add this in your load script where you load your Date dimension so you have it for all dates.

/Steen

7,937 Views