Skip to main content
hic
Former Employee
Former Employee

There are a number of prefixes in QlikView, that help you load and transform data. One of them is the Generic prefix.

Whenever you have a generic database, the Generic prefix can be used to transform the data and create the desired fields. A generic database is basically a table where the second last column is an arbitrary attribute and the very last is the value of the attribute. In the input table below you have a three-column generic database.

 

Generic transformation4.png

 

But if you want to analyze this data, it is much easier to have all attributes in separate fields so that you can make the appropriate selections. It is not very practical to have one single field for all attribute values, since you may want to make selections using different attributes at the same time.

 

Enter the Generic prefix.

 

It converts the data to a structure where each attribute is placed in a field of its own. Another way to express it is to say that it takes field values and converts these to field names. If you compare it to the Crosstable prefix, you will find that they in principle are each other’s inverses.

 

The syntax is

 

   Generic Load Key, Attribute, Value From … ;

 

There are however a couple of things worth noting:

  • Usually the input data has three columns: one qualifier field (Key in the above example), an Attribute and a Value. But you may also have several qualifying fields. If you have four or more columns, all columns except the two last will be treated as qualifying fields.
  • The Generic prefix will create several tables; one table per attribute. This is normally not a problem. Rather, it is an advantage: It is the least memory-consuming way to store data if you have many attributes.

 

If you have more than one key, this means that you will get a composite key – a synthetic key – in the data model:

 

Multiple keys3.png

 

Although it looks ugly, this synthetic key is completely harmless. But it may still be a good idea to replace it with a manually created concatenated key:

 

   Autonumber(Key1 & '|' & Key2 & '|' & Key3) as Key,

 

Finally, I have seen many examples on QlikCommunity where a For-Next loop is used to join together all tables created by the Generic prefix, e.g.:

 

   Set vListOfTables = ;

   For vTableNo = 0 to NoOfTables()

      Let vTableName = TableName($(vTableNo)) ;

      If Subfield(vTableName,'.',1)='GenericLabel' Then

         Let vListOfTables = vListOfTables & If(Len(vListOfTables)>0,',') & Chr(39) & vTableName & Chr(39) ;

      End If

   Next vTableNo

 

   CombinedGenericTable:

   Load distinct Key From GenericDB;

 

   For each vTableName in $(vListOfTables)

      Left Join (CombinedGenericTable) Load * Resident [$(vTableName)];

      Drop Table [$(vTableName)];

   Next vTableName

 

The result is one big table that contains all attributes; a table that often is sparse (containing many NULL values) and much larger than the initial tables. And no performance has been gained… So I can only say:

 

You should not do this - unless you have a specific reason to.

 

The Generic prefix creates a set of tables that store the data in an optimal way. In most cases you should not change this. I realize, however, that there are cases where you want to transform the data further and need the data in one, unified table. Then the above scriptlet can be used.

 

HIC

60 Comments
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Dimitar,

We start by populating the final table with all Keys. So it appears to me that there would be no difference in this case between a LEFT or OUTER JOIN.

-Rob

26,159 Views
hic
Former Employee
Former Employee

I agree completely with Rob Wunderlich here. Further, I'd like to add that you should always start such a multiple join with a

   Load distinct Key From ....

to make sure that you have all keys from the very beginning. It makes the repeated Join faster and more stable.

HIC

0 Likes
26,159 Views
prodanov
Partner - Creator
Partner - Creator

Hi Rob,

   Yes, It is. I miss the part of script where you create a key. In this case LEFT JOIN work. In my scripts I use OUTER JOIN, and I don't create this tables. My script is:

// Join table after generic load

SUB JoinTable(wTblName,TblName)

  let i=0;

  let tblToDrop = null();

  for tbl=0 to NoOfTables()-1

  let tB = TableName($(tbl));

  if wildmatch('$(tB)','$(wTblName)*')>0 then

  let i=i+1;

  if i=1 then

  let renTable = '[$(tB)]' ;

  else

  let tblToDrop = tblToDrop & chr(39) & '[$(tB)]' & chr(39) & ',' ;

  end if

  end if

  NEXT tbl

  let tblToDrop = left(tblToDrop,len(tblToDrop)-1);

  RENAME Table $(renTable) to [$(TblName)];

  let strKey = Null() ;

  for i = 1 to NoOfFields('$(TblName)')-1

  let strKey = strKey & FieldName(i,'$(TblName)') & ',';

  next

  for each tbl in $(tblToDrop)

  let Rows = null();

  let fldDataValue = null();

  let tt = PurgeChar('$(tbl)','[]') ;

  // === Check is this field empty

  let Rows = NoOfRows('$(tt)') ;

  let fldData = TextBetween(TextBetween('$(tbl)','[',']'),'.','');

  wCnt:

  load NumericCount(Num#([$(fldData)],'0')) as tx resident $(tbl) group by 1;

  let fldDataTEXT = Peek('tx',0,'wCnt');

  drop table wCnt;

  let strLoad = strKey ;

  if '$(Rows)'='0' then

  drop table $(tbl);

  Else

  If '$(fldDataTEXT)'='0' then

  let strLoad = strLoad & 'text([' & fldData & ']) as [' & fldData & ']' ;

  Outer Join ([$(TblName)])

  load $(strLoad) Resident $(tbl);

  drop table $(tbl);

  Else

  if '$(Rows)'='$(fldDataTEXT)' then

  if WildMatch(lower(fldData),'*код*','*code*','*номер*','*склад*','*шофьор*','*saletype*','*група*')>0 then

  let strLoad = strLoad & 'text([' & fldData & ']) as [' & fldData & ']' ;

  ElseIf WildMatch(lower(fldData),'*дата*','*date*')>0 then

  let strLoad = strLoad & 'alt( date#([' & fldData & '],' & chr(39) & 'YYYY/MM/DD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM/DD/YYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM/DD/YY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'YYYY.MM.DD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM.DD.YYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM.DD.YY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'YYYYMMDD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MMDDYYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MMDDYY' & chr(39) & ')) as [' & fldData & ']';

  Else

  let strLoad = strLoad & 'num(num#([' & fldData & '],' & chr(39) & '0' & Chr(39) & ')) as [' & fldData & ']' ;

  End If

  else

  If WildMatch(lower(fldData),'*дата*','*date*')>0 then

  let strLoad = strLoad & 'alt( date#([' & fldData & '],' & chr(39) & 'YYYY/MM/DD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM/DD/YYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM/DD/YY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'YYYY.MM.DD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM.DD.YYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MM.DD.YY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'YYYYMMDD' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MMDDYYYY' & chr(39) & '),date#([' & fldData & '],' & chr(39) & 'MMDDYY' & chr(39) & ')) as [' & fldData & ']';

  Else

  let strLoad = strLoad & 'text([' & fldData & ']) as [' & fldData & ']' ;

  End If

  End if

  Outer Join ([$(TblName)])

  load $(strLoad) Resident $(tbl);

  drop table $(tbl);

  End If

  end if

  next tbl

END sub

In this script I also check the data type in fields and make the format.

0 Likes
26,026 Views
Anonymous
Not applicable

"If you compare it to the Crosstable prefix, you will find that they in principle are each other’s inverses."

That sentence alone was the key for me in finally understanding what a generic load is. Thanks!

0 Likes
26,026 Views
Not applicable

"Although it looks ugly, this synthetic key is completely harmless."

Thx thats nice to know, but how do I do this conclusion, when I dont have you around :-)?

Some more general background knowledge would be nice. I am still a bid confused with Qlik, the general databases rules does not always make sense in Qlik. Sometimes it does, sometimes it does not. Sometimes I could just put it all in one big pot, and other times it is crucial to do at least 3rd normalization etc., split it up in a billion tables.

I kind of miss "specfic Qlik - general database theory" if that makes sense. Is there anything like that?

26,026 Views
hic
Former Employee
Former Employee

I think it is fairly straightforward: Synthetic keys are not dangerous and should not be avoided, unless you have several of them and they "interfere" with each other, e.g. if one $Syn key is based on another. In such a case you should re-think your data model.

But if you have one single synthetic key that is "symmetric"; that uses the same constituent fields in all places where it is used, and you feel that the data model is OK, the synthetic key is most likely harmless.

But - you must of course always ask yourself whether it makes sense to use several keys between two tables; whether the data model is OK. Often one of the keys is redundant, and should therefore not be used.

HIC

26,026 Views
Not applicable

Henric Cronström

Hello Henric, thank yo for sharing such useful topic. I work with a similar table used in your script, however I have two fields only

@1= ProjectAttributes

@2= ProjectValue

project-info.png


My script is:

Projects:

GENERIC LOAD

Autonumber(@1) as %Projects_Key,

@1   as ProjectAttribute,

@2   as ProjectValue

FROM project-info.xlsx;

Here si the data model

generic-prefix-map.png

I would like to consolidate the tables into one using a For-Next loop becasue I have lots of Project Attributes.

I have no GENERICDB table where Key field is stored. I create my own Key (see %Projects_Key), so that your script below does not work for me.

CombinedGenericTable:

Load distinct Key From GenericDB;

Could you please advise on how I can consolidate the tables?

Thank yo for yrou support and please ask if the abpve does not make sense

Regards, Andrea

0 Likes
26,026 Views
hic
Former Employee
Former Employee

You can use the for-next loop I have in the blog post. The only changes you need to make are

  • What I call 'GenericLabel', you call 'Projects'. (the prefix in the table names)
  • The Load statement that loads from 'GenericDB' should be changed to a load statement that loads from project-info.xlsx.

But what I don't understand is why you use autonumber to create the keys. That will result in that the same attribute (e.g. Project Name) all have the same Key value. It seems to me as if you should have an ID for the project as Key...

HIC

0 Likes
26,026 Views
Not applicable

Hic, I will try your options.

The project ID is in the excel files: cell B2. See image above.

I do not have a column Key (like your example have). hence why I was creating my own Key (%Projects_Key) using column A, the projects attributes.

What are your thoughts?Thank yof oryour support.

Regards, Andrea

0 Likes
25,775 Views
hic
Former Employee
Former Employee

If the data is like in the picture and the same structure is repeated for each project - i.e. the first line of a project section is a line where 'Project Reference' is found in the first column - then you can create your project ID the following way:

tmpTable:

Load

   If(@1='Project Reference',@2,Peek(ProjectID)) as ProjectID,

   @1 as ProjectAttribute,

   @2 as ProjectValue

From ...

Then, in a second step, you can do the Generic Load:

Projects:

Generic Load ProjectID, ProjectAttribute, ProjectValue Resident tmpTable;

Drop Table tmpTable;

HIC

0 Likes
25,775 Views