A QlikView feature that is poorly known and brilliant in its simplicity is the Preceding Load.
If you don’t know what it is, then I strongly suggest that you read this blog post and find out. Because it will help you in your QlikView scripting.
So what is it?
It is a way for you to define successive transformations and filters so that you can load a table in one pass but still have several transformation steps. Basically it is a Load statement that loads from the Load/SELECT statement below.
Example: you have a database where your dates are stored as strings and you want to use the QlikView date functions to interpret the strings. But the QlikView date functions are not available in the SELECT statement. The solution is to put a Load statement in front of the SELECT statement: (Note the absence of “From” or “Resident”.)
Load Date#(OrderDate,’YYYYMMDD’) as OrderDate; SQL SELECT OrderDate FROM … ;
What happens then is that the SELECT statement is evaluated first, and the result is piped into the Load statement that does the date interpretation. The fact that the SELECT statement is evaluated before the Load, is at first glance confusing, but it is not so strange. If you read a Preceding Load as
Load From ( Select From ( DB_TABLE ) )
then it becomes clearer. Compare it with nested functions: How would you evaluate “Round( Exp( x ) )”. You would of course evaluate the Exp() function first and then the Round() function. That is, you evaluate it from right to left.
The reason is that the Exp() function is closest to the source data, and therefore should be evaluated first. It’s the same with the Preceding Load: The SELECT is closest to the source data and should therefore be evaluated first. In both cases, you can look at it as a transformation that has an input and an output and to do it correctly, you need to start with the part of the transformation closest to the input.
Any number of Loads can be “nested” this way. QlikView will start from the bottom and pipe record by record to the closest preceding Load, then to the next, etc. And it is almost alwaysfaster than running a second pass through the same table.
With preceding Load, you don’t need to have the same calculation in several places. For instance, instead of writing
where the same calculation is made for both Age and ReferenceDate, I would in real life define my ReferenceDate only once and then use it in the Age function in a Preceding Load:
Maybe I missed this in the string but how does one aggregate/summarize/group by in a preceding load? I understand the transformation of the data in the preceding load but can't seem to summarize after the transformation... See script below. Working from the bottom up the first preceding load where I perform my transformations works but when I add the second preceding load to summarize it fails and says Error: Invalid expression.
You have to add a "group by" clause to the preceeding load statement where you are using aggregate functions. Be sure to include all the column expressions that are not aggrgates in your group by list.
So that part of the preceding load is similar to SQL. Could I accomplish the transformation and grouping by in 1 preceding load? Is there a benefit to keeping it separate?
We have observed that preceding loads do tend to extend the load time as they behave like separate load statements running in sequence so clearly there is an argument for combining them into one. I find that if I have a series of calculated fields like what you have in your sample it is usually easier to follow if I do all the transformation operations and then the aggregation step.
The long and short of it is based on personal preference and performance, if thing are not performing well then you may need to try a different approach.
It is similar to any other load-statement in the script, only there is no source (FROM or RESIDENT) . You can use WHERE and GROUP BY, probably also ORDER BY.
A problem with preceding loads is they are not as easy to read. Have also noticed at times that preceding load seems to take more time to execute then if using a temporary table and doing a RESIDENT load from that.
I would not attempt doing the transformation and grouping in the same LOAD statement. Also I would separate loading from database and transformation of data. Ever heard of ETL? Your code you can't just run the transformation and grouping, you always have to fetch the data from database also. Separating those steps you can do the transformation over again without having to fetch new data from the database.
More than once I see a preceding load which simply regurgitates the field names from the select. Typically its the result of using the wizard. The only reason I use preceding is to gain access to functionality only available in Qlik (e.g. previous). It's double-work when you have to change the select and remember to also change the preceding load. It would seem to faster and easier to rename the fields in the sql rather then in the preceding load. Is there a best method?
I usually do the same as you - I only use preceding load when needed, e.g. to make successive transformation or to use Qlik specific functionality. Renaming fields in the SELECT statement usually works excellently, so I do that quite often.
However, there are some odd ODBC drivers that are case-insensitive, and this causes problems. For example
SELECT FIELD_A as NewFieldName FROM ...
will then create a field called NEWFIELDNAME - in upper case. This is extremely annoying, and the work-around is of course to rename the fields in a Preceding Load.
But if you then write
Load FIELD_A as NewFieldName ;
SELECT * FROM ...
you will get an inefficient SELECT statement: The star could mean hundreds of fields.
This means that you HAVE TO list all fields in both the Load and the SELECT, to get an efficient load sequence: