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

Define hierarchy by Id

Hi to eveyone!

Please help!

I have a table:

ID                 

1

1.1

1.1.1

1.1.2

1.1.2.1

1.1.2.2

1.1.2.2.1

1.2

1.2.1

....

How can I define that "1" is a parent for "1.1", "1.1" is a parent for "1.1.1" and "1.1.2". There is 4 levels of hierarchy.

I tried to use:

if(len(ID)>=len(previous(ID)),alt(peek(Parent),previous(ID))) as Parent

but it works only partly, it compares only previous and current row. But it should take ID when it find the first short (in length) ID from the all previous rows.

Thanks!

1 Solution

Accepted Solutions
Not applicable
Author

I think I have found a solution:

left(ID,index(ID,'.',-1)-1) as ParentID

View solution in original post

6 Replies
Not applicable
Author

You can try with Hierarchy function in scripting

or use below expression:

Trim(Replace(ID,subfield(ID,'.',-1),'')) as ParentID

If you want put the null if there in no parent then modified the expression:

IF( ID = Trim(Replace(ID,subfield(ID,'.',-1),'')) , Null() , Trim(Replace(ID,subfield(ID,'.',-1),'')) ) AS ParentID

Not applicable
Author

Thank you very much.

But it also works partially.

For example:

"1.1.6.1" - "..6." - wrong

"1.1.6.2" - "1.1.6" - ok

"1.1.6.6" -  "1.1.." - wrong

"1.1.7.1" - "..7." - wrong

"1.1.7.7" - "1.1.." - wrong

So, as you see, in some cases it do not work. It is strange, because logically it should work correctly.

Not applicable
Author

I think I have found a solution:

left(ID,index(ID,'.',-1)-1) as ParentID

MarcoWedel

Hi,

to get different selection and analysis options using the different hierarchy levels of your ID field, you could try something like:

QlikCommunity_Thread_189323_Pic1.JPG

QlikCommunity_Thread_189323_Pic2.JPG

QlikCommunity_Thread_189323_Pic3.JPG

QlikCommunity_Thread_189323_Pic4.JPG

QlikCommunity_Thread_189323_Pic5.JPG

QlikCommunity_Thread_189323_Pic6.JPG

tabIDs:

LOAD Text(ID) as ID,

    Text(Left(ID,Index(ID&'.','.',IterNo())-1)) as HierarchyID,

    IterNo() as Level

While IterNo()<=SubStringCount(ID,'.')+1;

LOAD * Inline [

    ID

    1

    1.1

    1.1.1

    1.1.2

    1.1.2.1

    1.1.2.2

    1.1.2.2.1

    1.2

    1.2.1

];

tabLevels:

Generic LOAD

  ID,

  'Level'&Level&'ID',

  HierarchyID

Resident tabIDs;

tabParents:

Generic LOAD

  ID,

  'Parent'&(SubStringCount(ID,'.')+1-Level)&'ID',

  HierarchyID

Resident tabIDs

Where Level<=SubStringCount(ID,'.');

hope this helps

regards

Marco

Not applicable
Author

Thank you very much, Marco!

MarcoWedel

You're welcome

regards

Marco