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.
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.
In real life, it is often useful to nest an interpretation function inside a formatting function:
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.
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.
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.
Thx, a very nice explanation Though I got the transformation part. What I think I dont fully understand is what happens after that. From the output stage, right side and further on. If I do not specify anything, which one does it take. In the table viewer, when I hover the field, why does it not tell me both tags, text and num? So I mean it seems like there is a "standard" that it use, while the other is in a "background layer"?
Also if it was truly dual I should have been able to do Date(Date#()) in one function, just specifying both formats?
First of all, it is a truly dual value. You can nest Date#() inside Date(). The only reason why the nesting is not implemented as single one function is that the two functions use different format codes, and it is clearer what you do if you use two functions. Input is perhaps YYYYMMDD and output should perhaps be M/D/YY.
Which value QlikView uses? That depends on where you use the dual field.
The text is always used for display.
The number (if it exists) can be used for sorting.
String functions and operators, e.g. Left(<dual_value>) use the textual part
Numeric functions and operators, e.g. Mod(<dual_value>, n) use the numeric part.
Can you help me understand what is happening in this code?
Trim(Date(Timestamp_Field)) as Timestamp_Removed
Either in the load script or in the UI, this removes the timestamp portion from the underlying serial number. I am trying to understand how Trim(), a text function, alters the numeric portion of a field in this way.
I found this in some existing code and after explaining that Trim() only removes leading/trailing spaces, found that it in fact does the same thing as Floor() in this context. (I created a sample .qvw but can't attach it to this reply.)
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?
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'.
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.
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:
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.