Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Transpose

Hi All

I have large piece of data in the format (see below). I am looking to transpose in the format (see below)

01/04/201301/05/201301/06/2013
CodeNameNumDenomNumDenomNumDenom
NYNew York203306403
LONLondon214317414
BIRBirmingham225328425
CARCardiff236339436
MANManchester247345447

After Transpose:(to look like)

CodeNameMonthNum Denom
NYNew York01/04/2013203
NYNew York01/05/2013306
NYNew York01/06/2013403
LONLondon01/04/2013214
LONLondon01/05/2013317
LONLondon01/06/2013414
BIRBirmingham01/04/2013225
BIRBirmingham01/05/2013328
BIRBirmingham01/06/2013425
CARCardiff01/04/2013236
CARCardiff01/05/2013339
CARCardiff01/06/2013436
MANManchester01/04/2013247
MANManchester01/05/2013345
MANManchester01/06/2013447
1 Solution

Accepted Solutions
rbecher
MVP
MVP

Hi Koushik,

QlikView script is very powerful. You can solve this with some "hand-coding":

// Replace manually if new file has different amount of columns:

FirstRecord:

First 1 LOAD A,

     B,

     C,

     D,

     E,

     F,

     G,

     H

FROM

D:\Transpose.xlsx

(ooxml, no labels, table is Tabelle1);

// Read all Dates from Header line:

FOR i = 1 to NoOfFields('FirstRecord')

     Let vField = FieldName($(i), 'FirstRecord');

     Dates:

     Load Date#(Date) as DateValue Where Len(Trim(Date))>0;

     Load Text(Peek('$(vField)', -1, 'FirstRecord')) as Date Autogenerate 1;

NEXT

Drop Table FirstRecord;

// Loop over Dates and read additional columns:

FOR i = 0 to NoOfRows('Dates') -1

     Let vDate = Text(Peek('DateValue', $(i), 'Dates'));

     Set vFieldNum = '';

     Set vFierldDenom = '';

     IF $(i) > 0 THEN

         Let vFieldNum = 'Num' & i;

         Let vFierldDenom = 'Denom' & i;

     ELSE

         Set vFieldNum = 'Num';

         Set vFierldDenom = 'Denom';

     END IF

     Result:

     LOAD Code,

          Name,

          Date#('$(vDate)') as Month,

          $(vFieldNum) as Num,

          $(vFierldDenom) as Denom

     FROM

     D:\Transpose.xlsx

     (ooxml, embedded labels, header is 1 lines, table is Tabelle1);

NEXT

Drop Table Dates;

In my example I assumed you have an Excel file. But this could be adapted to CSV or other formats.

- Ralf

Astrato.io Head of R&D

View solution in original post

13 Replies
Anonymous
Not applicable
Author

Koushik

What does your source data table /model look like ?

Your 'before' does not look like a QlikView table.  Is it a spreadsheet or something ?

Best Regards,     Bill

rbecher
MVP
MVP

Hi Koushik,

QlikView script is very powerful. You can solve this with some "hand-coding":

// Replace manually if new file has different amount of columns:

FirstRecord:

First 1 LOAD A,

     B,

     C,

     D,

     E,

     F,

     G,

     H

FROM

D:\Transpose.xlsx

(ooxml, no labels, table is Tabelle1);

// Read all Dates from Header line:

FOR i = 1 to NoOfFields('FirstRecord')

     Let vField = FieldName($(i), 'FirstRecord');

     Dates:

     Load Date#(Date) as DateValue Where Len(Trim(Date))>0;

     Load Text(Peek('$(vField)', -1, 'FirstRecord')) as Date Autogenerate 1;

NEXT

Drop Table FirstRecord;

// Loop over Dates and read additional columns:

FOR i = 0 to NoOfRows('Dates') -1

     Let vDate = Text(Peek('DateValue', $(i), 'Dates'));

     Set vFieldNum = '';

     Set vFierldDenom = '';

     IF $(i) > 0 THEN

         Let vFieldNum = 'Num' & i;

         Let vFierldDenom = 'Denom' & i;

     ELSE

         Set vFieldNum = 'Num';

         Set vFierldDenom = 'Denom';

     END IF

     Result:

     LOAD Code,

          Name,

          Date#('$(vDate)') as Month,

          $(vFieldNum) as Num,

          $(vFierldDenom) as Denom

     FROM

     D:\Transpose.xlsx

     (ooxml, embedded labels, header is 1 lines, table is Tabelle1);

NEXT

Drop Table Dates;

In my example I assumed you have an Excel file. But this could be adapted to CSV or other formats.

- Ralf

Astrato.io Head of R&D
Not applicable
Author

Hi Bill

The 'Before' table is a spreadsheet.

Not applicable
Author

Hi Ralf

The script is unable to read 'Num' and 'Denom'. I get an error

Field not found - <Num>

Result: 

     LOAD Code,  

          Name,  

          Date#('2J') as Month, 

          Num as Num,  

          Denom as Denom 

FROM

(ooxml, embedded labels, table is Sheet1)

rbecher
MVP
MVP

The format of your Excel file must be different then. You have to adapt it, or upload it that I can have a look.

Astrato.io Head of R&D
Not applicable
Author

Thanks Ralf. It worked.

Not applicable
Author

Hi Ralf

The above code works if I have one excel workbook.

I have multiple workbooks with multiple sheets. How can I loop them? I could get it to work by ODBC the excel workbook but this only works if the workbook has same number of sheets in each workbook. 

Not applicable
Author

Hi

you can use

FOR EACH vSheet IN  SheetName_1, SheetName_2...SheetName_n

here is the code given by Ralph

just modify

D:\Transpose.xlsx       (ooxml, embedded labels, header is 1 lines, table is Tabelle1); 

by

D:\Transpose.xlsx       (ooxml, embedded labels, header is 1 lines, table is $(vSheet)); 

NEXT

best regards

Chris

Not applicable
Author

And for multiples xls files

SET vFile =  Transpose*.xlsx ;

LOAD

...

FROM  D:\$(vFile) (ooxml, embedded labels, header is 1 lines, table is $(vSheet));