Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
AlonF
Contributor
Contributor

Pivoting data in data load editor

 Hey everyone,

I have a table with these fields:

Worker Name,

HourCost_2014,

    MonthCost_2014,

    MonthCost_2014,

    MonthCost_2015,

    HourCost_2016,

    MonthCost_2016,

    HourCost_2017,

    MonthCost_2017

This table contains the hourly and monthly cost of each worker, for every year.

I would like to turn it to be like this:

Worker name

Year

Hour_Cost

Month_Cost

can you give me any advise? i read a bit about cross table an i'm not sure how to use it properly for this one.

Thanks!

1 Reply
Nicole-Smith

Load script like this should do the trick: 

/* Cross table data */
Temp:
CrossTable(Field, Value, 1)
LOAD * 
INLINE [
	Worker Name, HourCost_2014, MonthCost_2014, HourCost_2015, MonthCost_2015, HourCost_2016, MonthCost_2016, HourCost_2017, MonthCost_2017
	Jane Doe, 100, 200, 300, 400, 500, 600, 700, 800
	John Doe, 800, 700, 600, 500, 400, 300, 200, 100
];

/* Format data */
Final:
LOAD [Worker Name],
	 SUBFIELD([Field], '_', 2) AS [Year],
	 [Value] AS [Hour_Cost]
RESIDENT Temp
WHERE [Field] LIKE 'HourCost*';

OUTER JOIN (Final)
LOAD [Worker Name],
	 SUBFIELD([Field], '_', 2) AS [Year],
	 [Value] AS [Month_Cost]
RESIDENT Temp
WHERE [Field] LIKE 'MonthCost*';

DROP TABLE Temp;

First we use CrossTable to get the data into two columns.  Then we have to split out the year and type from the original field names.  The above code will return a result that looks like this:

Worker Name Year Hour_Cost Month_Cost 
Jane Doe2014100200
Jane Doe2015300400
Jane Doe2016500600
Jane Doe2017700800
John Doe2014800700
John Doe2015600500
John Doe2016400300
John Doe2017200100