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

How to build Product Hierarchy based on Product ID

Hi Everyone,

I want to build product hierarchy structure based on Product ID. I have Product ID from table1. I want to produce table2.

Product ID
A.A.A.A.A
B.B.B.B.B
A.B.C
B.A
C.B.F.G
A.A.B.C
Product IDLevel1Level2Level3Level4Level5
A.A.A.A.AAA.AA.A.AA.A.A.AA.A.A.A.A
B.B.B.B.BBB.BB.B.BB.B.B.BB.B.B.B.B
A.B.CAA.BA.B.C
B.ABB.A
C.B.F.GCC.BC.B.FC.B.F.G
A.A.B.CAA.AA.A.BA.A.B.C

Thank you very much for help!

17 Replies
Not applicable
Author

Clever,

When I test your script, the program cannot get level 5 value. Very interesting.

Best regards.

Clever_Anjos
Employee
Employee

I´m supposing that level 5 when product has 5 codes should be null,

Easy to fix, need it?

Not applicable
Author

Yes, please. I need max level value for each row.

Clever_Anjos
Employee
Employee

Kindly test this version and check if fits your needs:

t:

LOAD

  [Product ID],

  SubStringCount([Product ID],'.')+1 as HowManyLevels

FROM

[https://community.qlik.com/thread/218664]

(html, codepage is 1252, embedded labels, table is @2);

let max=0;

for i = 0 to NoOfRows('t') -1

  let h = Peek('HowManyLevels',i,'t');

  let max = RangeMax(h,max);

  let p = Peek('Product ID',i,'t');

  temp:load null() as n AutoGenerate 0;

  for j = 1 to $(h)

  Concatenate(temp)

  LOAD

  '$(p)' as [Product ID],

  'Level$(j)' as [Level],

  if(

  len(left('$(p)',Index('$(p)','.',$(j))-1)) >0,

  left('$(p)',Index('$(p)','.',$(j))-1),

  '$(p)'

  ) as Value

  AutoGenerate 1;

  next

next;

drop Field n;

drop Table t;

Final:

Generic LOAD

[Product ID],

Level,

Value

Resident temp;

drop Table temp;

LET vLoop = NoOfTables() - 1;

//Rename first table name

RENAME Table Final.Level1 to Final;

FOR i = 1 to vLoop

LET vCurrentTable = 'Final.Level'& ($(i) + 1);

Outer Join(Final)

LOAD  *

Resident $(vCurrentTable);

DROP Table $(vCurrentTable); 

NEXT

Not applicable
Author

Thank you very much. I like you use SubStringCount function to find the level information. Very very smart solution.

Appreciate your help.

Best regards.

Not applicable
Author

Hello Clever and settu_periasamy,

Your script is working for small data set. I have 50 million row need to load and it fail and lock down the whole system. Do you have any idea to make this script work for large data set?


Thank you very much!

Clever_Anjos
Employee
Employee

Process small chunks of data each time

Not applicable
Author

Thank you - Clever!

I run each level at a time and not use the for loop. The script looks working.

Thank you very much - Clever!