Wednesday, November 26, 2014

Creating Flags in the Script


Creating flags in the script is something that experienced QlikView developers will do almost
without thinking. If they need to improve the performance of an expression, or just make an
expression easier to write, they will probably create a flag field in the script. By moving linear
calculations to the script and creating numeric fields, it means that the frontend performance
will be vastly improved.
Often, these will be created by simple If statements, but they can also be more complex.
In this recipe, we will look at creating a flag to help us calculate the end of period positions.
Getting ready
Load the following script:
Load
Date(TempDate) As Date,
WeekDay(TempDate) as WeekDay,
Year(TempDate) As Year,
Month(TempDate) As Month,
Date(MonthStart(TempDate), 'YYYY-MM') As YearMonth,
WeekYear(TempDate) & '-'
& Num(Week(TempDate), '00') As YearWeek,
Floor(5000 * Rand()) As StockVolume;
Load
RecNo()-1+Floor(MakeDate(2012)) As TempDate
AutoGenerate(730);

How to do it...
Follow these steps to create flags in the script:
1. Edit the QlikView script by adding the highlighted code between the YearWeek
and StockVolume lines:
...
WeekYear(TempDate) & '-'
& Num(Week(TempDate), '00') As YearWeek,
if(TempDate = Floor(MonthEnd(TempDate)), 1, 0)
As End_of_Month_Flag,
if(WeekDay(TempDate) = 5, 1, 0)
As End_of_Week_Flag,
Floor(5000 * Rand()) As StockVolume;
...
2. Reload the script.
3. Add a line chart with YearMonth as the dimension. Add the following expression:
Sum({<End_of_Month_Flag={1}>} StockVolume)
4. Add another line chart with YearWeek as the dimension. Add the following expression:
Sum({<End_of_Week_Flag={1}>} StockVolume)
Script

Note that your charts will look different as the values are random.
5. Add a straight table with Date and Weekday as the dimensions. Add the same
two expressions as the preceding ones to this chart:
Note that only Saturdays or month end dates have entries.
How it works...
The flags are really easy to calculate by using QlikView's built-in date functions. Once they are
created in the script, it is a really simple matter to use them in a set. Creating the flags in the
script makes this calculation really quick.

The WeekDay function returns a number between 0 and 6, where 0 is Monday. That is why we
look for 5; which means Saturday.

No comments:

Post a Comment