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

How to convert rows to columns

Hi QV users,

i need to convert rows to columns.

RAW DATA

KEY_FIELD1KEY_FIELD2CODE_1QTY_1CODE_2QTY_2CODE_3QTY_3CODE_4QTY_4
12345678018952001      
1234567802897001992001992001992001

 

OUTPUT

KEY_FIELD1CODEQTYPROG
1234567889520011
123456788970012
123456789920013
123456789920014
123456789920015

 

So, i would like to see a single row for each codes (only the codes that are not null) and every row must have a new field called PROG (with a numerical sequences).

It's important the order of single rows. First i want the codes with the value "01" in key_fields2 and after the codes with the value "02" in key_fields2.

I've read many posts with possibile solutions (e.g. crosstable, rowno(), autonumber, etc.) but i'm a newbie so i don't know what is the right function.

Attached is a sample data file.

Please, someone can bring me in the right direction?

Thanks,

Mattia

Labels (1)
2 Solutions

Accepted Solutions
hic
Former Employee
Former Employee

CorrectTable:
Load
   RowNo() as Prog,
   *
   Where Len(Trim(Code))>0;
Load
   KEY_FIELD1 as Key_Field1,
   KEY_FIELD2 as Key_Field2,
   Pick(IterNo(),CODE_1,CODE_2,CODE_3,CODE_4,CODE_5,CODE_6,CODE_7,CODE_8) as Code,
   Pick(IterNo(), QTY_1, QTY_2, QTY_3, QTY_4, QTY_5, QTY_6, QTY_7, QTY_8) as Qty
Resident OriginalTable
   While IterNo()<=8
   Order By KEY_FIELD1, KEY_FIELD2
;

 

/HIC

View solution in original post

hic
Former Employee
Former Employee

CorrectTable:
Load
If(Key_Field1=Peek(Key_Field1),Peek(Prog)+1,1) as Prog,
// RowNo() as Prog,
*
Where Len(Trim(Code))>0;
Load
KEY_FIELD1 as Key_Field1,
KEY_FIELD2 as Key_Field2,
Pick(IterNo(),CODE_1,CODE_2,CODE_3,CODE_4,CODE_5,CODE_6,CODE_7,CODE_8) as Code,
Pick(IterNo(), QTY_1, QTY_2, QTY_3, QTY_4, QTY_5, QTY_6, QTY_7, QTY_8) as Qty
Resident OriginalTable
While IterNo()<=8
Order By KEY_FIELD1, KEY_FIELD2
;

View solution in original post

5 Replies
hic
Former Employee
Former Employee

CorrectTable:
Load
   RowNo() as Prog,
   *
   Where Len(Trim(Code))>0;
Load
   KEY_FIELD1 as Key_Field1,
   KEY_FIELD2 as Key_Field2,
   Pick(IterNo(),CODE_1,CODE_2,CODE_3,CODE_4,CODE_5,CODE_6,CODE_7,CODE_8) as Code,
   Pick(IterNo(), QTY_1, QTY_2, QTY_3, QTY_4, QTY_5, QTY_6, QTY_7, QTY_8) as Qty
Resident OriginalTable
   While IterNo()<=8
   Order By KEY_FIELD1, KEY_FIELD2
;

 

/HIC

Mattia
Creator II
Creator II
Author

Thank you Henric!!!

It works perfectly.

Mattia

Mattia
Creator II
Creator II
Author

Henric, last thing.

The values in the field "PROG" must be resetted for every key_field1.

e.g.

1) now

key field1,       key field2,      code,          prog

00000001,       01                      987               1

00000001,       02                      965               2

00000002,       01                      987               3

00000002,       02                      976               4

2) desired

key field1,       key field2,      code,          prog

00000001,       01                      987               1

00000001,       02                      965               2

00000002,       01                      987               1

00000002,       02                      976               2

How can i modify your expression?

 

Thanks,

Mattia

hic
Former Employee
Former Employee

CorrectTable:
Load
If(Key_Field1=Peek(Key_Field1),Peek(Prog)+1,1) as Prog,
// RowNo() as Prog,
*
Where Len(Trim(Code))>0;
Load
KEY_FIELD1 as Key_Field1,
KEY_FIELD2 as Key_Field2,
Pick(IterNo(),CODE_1,CODE_2,CODE_3,CODE_4,CODE_5,CODE_6,CODE_7,CODE_8) as Code,
Pick(IterNo(), QTY_1, QTY_2, QTY_3, QTY_4, QTY_5, QTY_6, QTY_7, QTY_8) as Qty
Resident OriginalTable
While IterNo()<=8
Order By KEY_FIELD1, KEY_FIELD2
;

Mattia
Creator II
Creator II
Author

Thank you so much!!!

Mattia