Sometimes when you load data into QlikView you find that a field is sparsely populated, i.e. it has discrete enumerable values where some values are missing.
It could be like in the table to the right where you have three dates, each with some kind of conversion rate. The table only contains the dates where the conversion rate changed, not the dates between the changes.
However, the user will want to ask the question: “What was the status on this specific day?” In other words, the user wants to be able to click on a reference date to see the number that is associated with this date – but the date might not exist in the source data.
In such a situation, you need to generate the missing dates between the changes as individual records and use the “Rate” value from the previous date.
There are several ways to do this in QlikView, and all of them involve some script programming, using temporary tables. One algorithm is
Load the source table containing the rates (below called “Rates”).
Find largest and smallest date in the “Rates” table.
Generate all dates between the largest and smallest dates (below called “Dates”)..
Join the “Dates” table (outer join) onto the “Rates” table.
Sort the resulting table according to date.
Propagate the value of “Rate” downwards to all records that have NULL in the “Rate” field, using the Peek() function.
Visually, the join and peek steps of the algorithm look like this:
In the QlikView script, the algorithm would look like the following:
TempTable_Rates: Load Date, Rate From Rates ;
MinMaxDate: LoadMin(Date) asMinDate, Max(Date) as MaxDate resident TempTable_Rates; Let vMinDate = Peek('MinDate',-1,'MinMaxDate') - 1; Let vMaxDate = Peek('MaxDate',-1,'MinMaxDate') ;
Join (TempTable_Rates)
Load Date(recno()+$(vMinDate)) as Date AutogeneratevMaxDate - vMinDate;
Rates:
NoConcatenate Load Date, If( IsNull( Rate ), Peek( Rate ), Rate ) as Rate Resident TempTable_Rates Order By Date ; /* so that above values can be propagated downwards */
Drop Table MinMaxDate, TempTable_Rates;
Problem solved!
This method can be adapted for most situations when you need to insert additional records in existing data: Warehouse balances, Exchange rates, etc.
I couldn't get it to work for our situation. I think that is because the same problem occurs for a lot of situations in a single table. Let me explain: in my situation it's about an insurance company that wishes to keep track of reserve status totals AND reserve per customer file. My table looks something like the one I put below. Now suppose the question would be: what is the reserve for all filenrs at a given reference date?
Let's say the reference date would be feb 4th, 2014... Of course we don't have three files, but 100,000s..
crdate= creation date of that record (follows a payment to customer automatically);
linenr= nr of the created line;
rnk= ranking of the reserve status. All rankings are re-numbered each time a new line is added, so rnk = 1 represents the most current reserve status.
reserve= the mount of reserve for that particular customer file.
Though your solution did indeed work, it lead to a ridicously huge table. After a lot of thought and a "little help from my friend" (read: a lot of help) I decided to go for his solution, in which the original table stayed at exactly the same size.
It all boils down to adding a new field to the record (end_date), which indicates the "period of useability" of that record.
Step 1: read all records from the table into a temporary table.
Step 2: Make a new table, containing all fields and values from the temporary table with addition of a new field end_date. Sort this table by filenr and crdate, descending. Now fill the value of end_date by using an if-clause. IF the filenr of the previous record = the filenr of the current record then the end_date equals today (one time only), and the end_date of the previous record equals crdate of the current record minus 1.
Doing this, you create a period of useability for all records.
Step 3: drop the original, temporary table.
Totaling the reserve is just a sum of all records that meet the condition crdate<= reference date<= end_date.
The script looks like this (I don't know how to paste scripts like you do):
Reserveringen_Temp: LOAD filenr, crdate, reserve FROM (ooxml, embedded labels, table is Sheet1);
Reserveringen: LOAD *, [crdate] AS Startdatum, Date(if(filenr=previous(filenr), previous(crdate)-1, Date(Floor(Today())))) AS Einddatum Resident Reserveringen_Temp ORDER BY filenr, crdate DESC ;
// Opschonen tijdelijke tabel
Drop Table Reserveringen_Temp;
MasterCalendar: LOAD Datum, Year(Datum) AS Jaar, Month(Datum) AS Maand ; LOAD Date(EersteDatum + IterNo()) AS Datum, LaatsteDatum WHILE EersteDatum + IterNo() <= LaatsteDatum ; LOAD Min(crdate)-1 AS EersteDatum, Floor(Today()) AS LaatsteDatum RESIDENT Reserveringen;
I have Unit , Date, Accumulated mileage value for a vehicle , the vehicle may not have continues dates, however I need all the dates and its accumulated values if vehicle has the value in the source data we need to pick that or else need to pick from its previous date value, each vehicle has its own minimum dates. I have totally 50,000 vehicles the dates are started from 2005. how can I apply the above method to my issue could you please help me on this.
I have created a cumulative expression for our stock quantity (per item) with the rollingtodate example from the post mentioned above (my period is YYYYMM) but now I have to calculate the stock's value....
I have created a query that gives me the price per item per period (YYYMM) but how do I merge these prices with the quantity and how do I populate the prices per item per period (because a price has no end date)?
I had a small wrinkle on this that might be applicable to others: I need to report on the full range of dates for a dozen different organizations. So there is an additional column, ORGID in my table. I added a For... Each statement around the Load statement that generates the dates. It draws its values from a pre-loaded table that contains ORGID. I also store the list of ORGID + dates in a new table so that I can reuse it many times in subsequent Load statements. I drop it at the end.
FOR Each a in FieldValueList('ORGID')
DateRange:
LOAD '$(a)' as ORGID, Date(recno()+$(vMinDate)) as TheDate Autogenerate vMaxDate - vMinDate;
I have a slightly different requirement.I have been able to populate Ticket numbers with '0' for the missing months. But this field needs to link with the Application filter box. i.e.i need the application to be mentioned for blank ticket number for the generated missing dates. I have 10 such applications and these 10 application names should be repeated in separate rows with '0' Ticket number for the same date.