Skip to main content
Jennell_McIntire
Employee
Employee

Have you every used the Autogenerate clause in your load statement?  The Autogenerate clause is used in the load statement to automatically generate data in Qlik Sense or QlikView.  In the load statement you may see Autogenerate replace the From or Resident clause.  The key difference with the Autogenerate clause noted in Qlik Sense Help is that:

 

“The field list must not contain expressions which require data from an external data source or a previously loaded table, unless you refer to a single field value in a previously loaded table with the Peek function.”

 

I have seen the Autogenerate clause used to create a table of random numbers or to create a table with a range of dates.  Let’s take a look at how we can use it in the script.  Let’s assume I need a table of random numbers in my data model.  I can create a file, such as an Excel file, that has all the random numbers I need and load that into Qlik Sense or I can save myself some time and let Qlik Sense create the table for me.  In the script below, I am loading a table with an ID and a random number between 0 and 1 using the Autogenerate clause.  The 100 in parentheses lets Qlik Sense know how many records I would like to generate.  Note that the parentheses after Autogenerate are optional.

Script1.png

The resulting table looks like this:

Table1.png

That was pretty simple.  Now let’s see how we can generate a table with a range of dates.  If we want a table with all the dates in 2016, we can use a script (as seen below) to generate each date for the year.  The Autogenerate clause creates one date every time it reads the script until it gets to the end of the year (12/31/2016 in this example).

Script2.png

To begin the script, the variables for the date range are set.  The variable vMinDate stores the first day in the year and vMaxDate stores the last day in the year.  In the Load script, the TempDate is created.  If the first row is being created, then the date should be the value of vMinDate (1/1/2016).  If it is not the first row then increment the date by one day using the RowNo() function.  The While clause will repeat the script until the last day of the year is reached.  The resulting table looks like the image below with all dates from 1/1/2016 to 12/31/2016:

Table2.png

The Autogenerate clause in the load script provides an easy way to generate data in your Qlik Sense or QlikView data model.  You can control how many records you would like to add and, as done in the date example, you can repeat the script based on some condition.  So the next time you need to generate records that are not using an external data source or previously loaded table, try out the Autogenerate clause in your script.

 

Thanks,

Jennell

29 Comments
rarpecalibrate
Contributor III
Contributor III

Hi @wade12 ,

Try num#().

Ryan

0 Likes
8,507 Views
Martijn1
Contributor
Contributor

Thanks Ryan, this works!

0 Likes
8,500 Views
Martijn1
Contributor
Contributor

Ryan,

maybe you know what I do wrong, when I run Jennells script (of original post). At first I got the same error, base on the num#(). This I corrected and the script runs. I created the Temp table. But the table doesn't contain values. What do I do wrong?

Code script:


LET vMinDate = num#('1/1/2019');
LET vMaxDate = num#('12/31/2030');

Temp:

Load
if (RowNo ()=1, Date($(vMinDate), 'M/D/YYYY'), Date($(vMinDate) + RowNo(), 'M/D/YYYY')) as TempDate
AutoGenerate (1)
While $(vMinDate) + RowNo() < $(vMaxDate);

 

Picture Code script:

Picture script.PNG

Picture Temp table: Without results.

Table TempDate.PNG

0 Likes
8,497 Views
rarpecalibrate
Contributor III
Contributor III

Hi @Martijn1 ,

Try 

Temp: 

Load

Date($(vMinDate) + IterNo() - 1) as TempDate,

Autogenerate 1 While $(vMinDate) + IterNo() -1 <= $(vMaxDate);

Sorry writing this on my phone isnt easy... haha

0 Likes
8,480 Views
rarpecalibrate
Contributor III
Contributor III

Ps your date format should be MM/DD/YYYY

0 Likes
8,460 Views
Martijn1
Contributor
Contributor

No Worries,

I adjusted your code a bit, so it would run. Still I got the same result:

  • Table was created
  • Field was created
  • But Values weren't generated

Last code

Temp:
LOAD Date(MinDate+IterNo()-1) as TempDate
While MinDate+IterNo()-1<=MaxDate;
LOAD '1/1/2019' as MinDate,
'12/31/2019' as MaxDate
AutoGenerate 1;

PS: I also run the previous code with the new date format. But with the same results.

0 Likes
8,435 Views
rarpecalibrate
Contributor III
Contributor III

Hi @Martijn1 ,

Try maybe the following:

Date#()

I am still not sure why its not working. Use the debugger and trace - Use peek function to create the variable and then trace the variable. The load script will show you the value.

0 Likes
8,415 Views
Martijn1
Contributor
Contributor

Hi,

I have tried several options now:

  • use Date#
  • use Num#('1/1/2019') as MinDate
  • use Date#(….,'MM/DD/YYYY')

 

All with the same result: no outcome. 

Next step for me is to find out how the debugger and peek function works. This will take some time.

 

Nevertheless, thanks for trying to help me out!

 

KR 

Martijn

0 Likes
8,318 Views
rarpecalibrate
Contributor III
Contributor III

Hi,

Try the following to see what the first value that come from the table.

  • let test = peek('TempDate',0,'temp')
  • Trace first date value - $(test)

Also, after your temp table , create a MasterCalender where you can convert all the values to date, and drop the temp table. There are some good articles relating to MasterCalenders. 

Ryan Arpe

0 Likes
8,312 Views