Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
venkatesh7
Partner - Contributor II
Partner - Contributor II

Convert Rows into Columns with 2 fields in the table

Hello Folks,

Have a sample data like below and want to convert into a table

Sample Data:

Load * inline [
Header, Data
ID, 201
Timestamp, 2023-02-08T06:35:12.500Z
Country, US
Sales, 150000
ID, 201
Timestamp, 2023-02-08T07:26:29.441Z
Country, US
Sales, 200000
ID, 202
Timestamp, 2023-02-09T10:36:19.241Z
Country, US
Sales, 180000
];

Expected Output:

id country timestamp sales
201 US 2023-02-08T06:35:12.500Z 150000
201 US 2023-02-08T07:26:29.441Z 200000
202 US 2023-02-09T10:36:19.241Z 180000

 

Please help me out to achieve output like above

Thanks in Advance😊

 

Labels (2)
2 Solutions

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You will need to assign a common identifier to the rows you want to group. I wasn't clear whether you meant for your sample ID values to be unique.  If they were unique, you could propagate the ID to each row using Peek(). In the example below I'm generating a new grouping field "Sequence" which will work if ID is unique or not. 

RawData:
LOAD *,
  alt(Peek('Sequence'), 0)  + (Header = 'ID') as Sequence
;
Load * inline [

Header, Data
ID, 201
Timestamp, 2023-02-08T06:35:12.500Z
Country, US
Sales, 150000
ID, 201
Timestamp, 2023-02-08T07:26:29.441Z
Country, US
Sales, 200000
ID, 202
Timestamp, 2023-02-09T10:36:19.241Z
Country, US
Sales, 180000
];

Props:
Generic Load Sequence, Header, Data
Resident RawData
;

//*** Optional - consolidate tables
Final:
Load Distinct Sequence Resident RawData;
Drop Table RawData;

For i = NoOfTables()-1 to 0 step -1
 
Let vTable = TableName($(i));
 
if '$(vTable)' like 'Props.*' THEN
 
Join (Final) Load * Resident $(vTable);
 
Drop Table $(vTable);
 
ENDIF
Next i 

-Rob
http://www.easyqlik.com
http://masterssummit.com
http://qlikviewcookbook.com

View solution in original post

vinieme12
Champion III
Champion III

Here is an alternative script

 

Temp:
Load *,if(Header='ID',Data,Peek('RecKey')) as RecKey
, alt(Peek('Sequence'),0) + (Header = 'ID') as Sequence

inline [
Header, Data
ID, 201
Timestamp, 2023-02-08T06:35:12.500Z
Country, US
Sales, 150000
ID, 201
Timestamp, 2023-02-08T07:26:29.441Z
Country, US
Sales, 200000
ID, 202
Timestamp, 2023-02-09T10:36:19.241Z
Country, US
Sales, 180000
];

Set vFieldList = '';
for each Col_name in FieldValueList('Header')
let vFieldList = ',TextBetween(Rows,''$(Col_name)@'',''@'') as [$(Col_name)] ' & '$(vFieldList)' ;
next Col_name ;

let vFieldList = Mid('$(vFieldList)',2,len('$(vFieldList)'));

Main:
Load
$(vFieldList)
;
Load Sequence ,Concat(Header&'@'&Data&'@',',') as Rows
Resident Temp
Group by Sequence;

Drop table Temp;

 

vinieme12_0-1676363252525.png

 

 

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.

View solution in original post

3 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You will need to assign a common identifier to the rows you want to group. I wasn't clear whether you meant for your sample ID values to be unique.  If they were unique, you could propagate the ID to each row using Peek(). In the example below I'm generating a new grouping field "Sequence" which will work if ID is unique or not. 

RawData:
LOAD *,
  alt(Peek('Sequence'), 0)  + (Header = 'ID') as Sequence
;
Load * inline [

Header, Data
ID, 201
Timestamp, 2023-02-08T06:35:12.500Z
Country, US
Sales, 150000
ID, 201
Timestamp, 2023-02-08T07:26:29.441Z
Country, US
Sales, 200000
ID, 202
Timestamp, 2023-02-09T10:36:19.241Z
Country, US
Sales, 180000
];

Props:
Generic Load Sequence, Header, Data
Resident RawData
;

//*** Optional - consolidate tables
Final:
Load Distinct Sequence Resident RawData;
Drop Table RawData;

For i = NoOfTables()-1 to 0 step -1
 
Let vTable = TableName($(i));
 
if '$(vTable)' like 'Props.*' THEN
 
Join (Final) Load * Resident $(vTable);
 
Drop Table $(vTable);
 
ENDIF
Next i 

-Rob
http://www.easyqlik.com
http://masterssummit.com
http://qlikviewcookbook.com

vinieme12
Champion III
Champion III

Here is an alternative script

 

Temp:
Load *,if(Header='ID',Data,Peek('RecKey')) as RecKey
, alt(Peek('Sequence'),0) + (Header = 'ID') as Sequence

inline [
Header, Data
ID, 201
Timestamp, 2023-02-08T06:35:12.500Z
Country, US
Sales, 150000
ID, 201
Timestamp, 2023-02-08T07:26:29.441Z
Country, US
Sales, 200000
ID, 202
Timestamp, 2023-02-09T10:36:19.241Z
Country, US
Sales, 180000
];

Set vFieldList = '';
for each Col_name in FieldValueList('Header')
let vFieldList = ',TextBetween(Rows,''$(Col_name)@'',''@'') as [$(Col_name)] ' & '$(vFieldList)' ;
next Col_name ;

let vFieldList = Mid('$(vFieldList)',2,len('$(vFieldList)'));

Main:
Load
$(vFieldList)
;
Load Sequence ,Concat(Header&'@'&Data&'@',',') as Rows
Resident Temp
Group by Sequence;

Drop table Temp;

 

vinieme12_0-1676363252525.png

 

 

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
venkatesh7
Partner - Contributor II
Partner - Contributor II
Author

Thank you so much @rwunderlich and @vinieme12 both the solutions worked liked a charm!