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: 
Not applicable

transform data (maybe crosstable)

I have data in following format

Store#Year-MonthToysCleaningPharmacy
12014-144665343
12014-23436565676
12014-3352343878
12014-4534526565
12014-565654542343
12014-674329896
12014-7687343656
12014-887343454
12014-999813132423
12014-109893536564
12014-1134332345565
12014-12147
22014-1258
22014-2369
22014-34710
22014-45811
22014-56912
22014-671013
22014-781114
22014-891215
22014-9101316
22014-10111417
22014-11121518
22014-12131619

How can i transform it in below format

Store#Category2014-12014-22014-32014-42014-52014-62014-72014-82014-92014-102014-112014-12
1Toys443433556567687879989893431
1Cleaning66565652343345254542432343343131353323454
1Pharmacy343676878656534398966564543242365645657
2Toys2345678910111213
2Cleaning5678910111213141516
2Pharmacy8910111213141516171819
1 Solution

Accepted Solutions
Gysbert_Wassenaar

Personally, I'd stop after the Crossload and only show the data in the format you posted and not store it in that format. But that's up to you. You can use (parts of) this script:

Temp:

CrossTable(Category, Value,2)

LOAD * INLINE [

    Store#, Year-Month, Toys, Cleaning, Pharmacy

    1, 2014-1, 44, 665, 343

    1, 2014-2, 343, 6565, 676

    1, 2014-3, 35, 2343, 878

    1, 2014-4, 5, 3452, 6565

    1, 2014-5, 656, 54542, 343

    1, 2014-6, 7, 432, 9896

    1, 2014-7, 687, 343, 656

    1, 2014-8, 87, 343, 454

    1, 2014-9, 998, 131, 32423

    1, 2014-10, 989, 353, 6564

    1, 2014-11, 343, 32345, 565

    1, 2014-12, 1, 4, 7

    2, 2014-1, 2, 5, 8

    2, 2014-2, 3, 6, 9

    2, 2014-3, 4, 7, 10

    2, 2014-4, 5, 8, 11

    2, 2014-5, 6, 9, 12

    2, 2014-6, 7, 10, 13

    2, 2014-7, 8, 11, 14

    2, 2014-8, 9, 12, 15

    2, 2014-9, 10, 13, 16

    2, 2014-10, 11, 14, 17

    2, 2014-11, 12, 15, 18

    2, 2014-12, 13, 16, 19

];

Temp2:

Generic load Store#,Category, [Year-Month], Value Resident Temp;

drop Table Temp;

Result:

load 1 as Store#, 1 as Category

AutoGenerate 0;

for i = 0 to NoOfTables() -2

  let tn = TableName($(i));

  join(Result)

  load * Resident [$(tn)];

next

LET vTables = chr(34) & TableName(0) & chr(34);

FOR i = 1 to NoOfTables() - 2

    LET d = chr(34) & TableName(i) & chr(34);

    LET vTables = vTables & ', ' & d;

    TRACE $(vTables);

NEXT

DROP Tables $(vTables);


talk is cheap, supply exceeds demand

View solution in original post

8 Replies
Gysbert_Wassenaar

Personally, I'd stop after the Crossload and only show the data in the format you posted and not store it in that format. But that's up to you. You can use (parts of) this script:

Temp:

CrossTable(Category, Value,2)

LOAD * INLINE [

    Store#, Year-Month, Toys, Cleaning, Pharmacy

    1, 2014-1, 44, 665, 343

    1, 2014-2, 343, 6565, 676

    1, 2014-3, 35, 2343, 878

    1, 2014-4, 5, 3452, 6565

    1, 2014-5, 656, 54542, 343

    1, 2014-6, 7, 432, 9896

    1, 2014-7, 687, 343, 656

    1, 2014-8, 87, 343, 454

    1, 2014-9, 998, 131, 32423

    1, 2014-10, 989, 353, 6564

    1, 2014-11, 343, 32345, 565

    1, 2014-12, 1, 4, 7

    2, 2014-1, 2, 5, 8

    2, 2014-2, 3, 6, 9

    2, 2014-3, 4, 7, 10

    2, 2014-4, 5, 8, 11

    2, 2014-5, 6, 9, 12

    2, 2014-6, 7, 10, 13

    2, 2014-7, 8, 11, 14

    2, 2014-8, 9, 12, 15

    2, 2014-9, 10, 13, 16

    2, 2014-10, 11, 14, 17

    2, 2014-11, 12, 15, 18

    2, 2014-12, 13, 16, 19

];

Temp2:

Generic load Store#,Category, [Year-Month], Value Resident Temp;

drop Table Temp;

Result:

load 1 as Store#, 1 as Category

AutoGenerate 0;

for i = 0 to NoOfTables() -2

  let tn = TableName($(i));

  join(Result)

  load * Resident [$(tn)];

next

LET vTables = chr(34) & TableName(0) & chr(34);

FOR i = 1 to NoOfTables() - 2

    LET d = chr(34) & TableName(i) & chr(34);

    LET vTables = vTables & ', ' & d;

    TRACE $(vTables);

NEXT

DROP Tables $(vTables);


talk is cheap, supply exceeds demand
Not applicable
Author

anyone with clue

Nicole-Smith

userid128223
Creator
Creator

How Nicole. Can you give example based on my question.

Not applicable
Author

Gysbert_Wassenaar

Yes, but for some reason I cannot fathom my clue is is stuck in the moderation queue. You'll have to wait for a moderator to approve my post.


talk is cheap, supply exceeds demand
felipe_dutra
Partner - Creator
Partner - Creator

See the attachment:

Nicole-Smith

Load script (it will handle any number of categories and year-months):

Test:

CrossTable(Category,Value,2)

LOAD *;

LOAD * INLINE [

    Store#, Year-Month, Toys, Cleaning, Pharmacy

    1, 2014-1, 44, 665, 343

    1, 2014-2, 343, 6565, 676

    1, 2014-3, 35, 2343, 878

    1, 2014-4, 5, 3452, 6565

    1, 2014-5, 656, 54542, 343

    1, 2014-6, 7, 432, 9896

    1, 2014-7, 687, 343, 656

    1, 2014-8, 87, 343, 454

    1, 2014-9, 998, 131, 32423

    1, 2014-10, 989, 353, 6564

    1, 2014-11, 343, 32345, 565

    1, 2014-12, 1, 4, 7

    2, 2014-1, 2, 5, 8

    2, 2014-2, 3, 6, 9

    2, 2014-3, 4, 7, 10

    2, 2014-4, 5, 8, 11

    2, 2014-5, 6, 9, 12

    2, 2014-6, 7, 10, 13

    2, 2014-7, 8, 11, 14

    2, 2014-8, 9, 12, 15

    2, 2014-9, 10, 13, 16

    2, 2014-10, 11, 14, 17

    2, 2014-11, 12, 15, 18

    2, 2014-12, 13, 16, 19

];

CategoryList:

LOAD DISTINCT Category as CategoryList

RESIDENT Test

ORDER BY Category;

LET vLoop = FieldValueCount('Category');

FOR c=1 TO vLoop

    LET vCategory = PEEK('CategoryList',$(c)-1,'CategoryList');

    LET vCategoryTable = '[' & PEEK('CategoryList',$(c)-1,'CategoryList') & ']';

   

    $(vCategoryTable):

    Generic

    LOAD Store#, [Year-Month], Value

    Resident Test

    WHERE Category = '$(vCategory)'; 

    YearMonthList:

    LOAD DISTINCT [Year-Month] as YearMonthList

    RESIDENT Test

    ORDER BY [Year-Month] DESC;

    LET vLoop2 = FieldValueCount('YearMonthList')-1;

    LET vTable = '[$(vCategory).' & PEEK('YearMonthList',0,'YearMonthList') & ']';

    LET vYearMonth = '[' & PEEK('YearMonthList',0,'YearMonthList') & ']';

    RENAME TABLE $(vTable) TO $(vCategoryTable);

    Outer Join ($(vCategoryTable))

    LOAD *, '$(vCategory)' as Category

    RESIDENT $(vCategoryTable);

    FOR i=1 TO vLoop2

        LET vTable = '[$(vCategory).' & PEEK('YearMonthList',$(i),'YearMonthList') & ']';

               

        OUTER JOIN ($(vTable))

        LOAD *,'$(vCategory)' as Category

        RESIDENT $(vTable);

   

        OUTER JOIN ($(vCategoryTable))

        LOAD *

        RESIDENT $(vTable);

   

        DROP TABLE $(vTable);   

    NEXT i

   

    IF $(c)=1 THEN

        RENAME Table $(vCategoryTable) TO Final;

    ELSE

        CONCATENATE (Final)

        LOAD *

        RESIDENT $(vCategoryTable);

        DROP TABLE $(vCategoryTable);

    ENDIF

NEXT c

DROP TABLE CategoryList;

DROP TABLE YearMonthList;

DROP TABLE Test;

I've also attached a working file.