<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Trouble with recursive logic in building a hierarchy qvd in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516968#M105852</link>
    <description>&lt;P&gt;there is already an out-of-the-box function named Hierarchy that solved your requirement:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Hierarchy&lt;/P&gt;&lt;P&gt;The&amp;nbsp;hierarchy&amp;nbsp;prefix is used to transform a parent-child hierarchy table to a table that is useful in a&amp;nbsp;Qlik Sense&amp;nbsp;data model. It can be put in front of a&amp;nbsp;LOAD&amp;nbsp;or a&amp;nbsp;SELECT&amp;nbsp;statement and will use the result of the loading statement as input for a table transformation.&lt;/P&gt;&lt;P&gt;The prefix creates an expanded nodes table, which normally has the same number of records as the input table, but in addition each level in the hierarchy is stored in a separate field. The path field can be used in a tree structure.&lt;/P&gt;&lt;P&gt;Syntax:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hierarchy (NodeID, ParentID, NodeName, [ParentName, [PathSource, [PathName, [PathDelimiter, Depth]]]])(loadstatement | selectstatement)&amp;nbsp;&lt;/P&gt;&lt;P&gt;the first 3 parameters are obligatory, the remaining are optional:&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hierarchy(NodeID, ParentID, NodeName, ParentName, NodeName, PathName, '\', Depth) LOAD * inline [&lt;/P&gt;&lt;P&gt;NodeID, ParentID, NodeName&lt;/P&gt;&lt;P&gt;1, 4, London&lt;/P&gt;&lt;P&gt;2, 3, Munich&lt;/P&gt;&lt;P&gt;3, 5, Germany&lt;/P&gt;&lt;P&gt;4, 5, UK&lt;/P&gt;&lt;P&gt;5, , Europe&lt;/P&gt;&lt;P&gt;];&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ali_hijazi_0-1746616968359.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/180287iAB5A013C512275A3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ali_hijazi_0-1746616968359.png" alt="ali_hijazi_0-1746616968359.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 07 May 2025 11:23:54 GMT</pubDate>
    <dc:creator>ali_hijazi</dc:creator>
    <dc:date>2025-05-07T11:23:54Z</dc:date>
    <item>
      <title>Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516891#M105844</link>
      <description>&lt;P&gt;I am having a bit of a circular logic issue in Qlik Sense Data Load Script that I can't seem to answer fully. I need to build a hierarchy of 'levels' based solely on ID, First_Name, Last_Name, Supervisor_ID, and Supervisor_Name. Where there is a singular top level person that represents 'level 4' and each person who reports to them is a 'level 5' and each person who reports to a 'level 5' is a 'level 6' and so on down to level 7. Where if a person is missing a given bottom tier they are that tier for themselves. I keep going in circles of either getting everyone the same 'level 4' and 'level 5' but blanks in everything else or only their own levels with no other level populated. Please help if you can, Example data and script below. TIA.&lt;/P&gt;&lt;P&gt;sample data:&lt;BR /&gt;ID,FIRST_NAME,LAST_NAME,SUPERVISOR_ID,SUPERVISOR_NAME&lt;BR /&gt;100,Jill,Hill,1234567890,Rachel Pail&lt;BR /&gt;210,Don,Mountain,100,Jill Hill&lt;BR /&gt;1234567890,Rachel,Pail,1234567890,Rachel Pail&lt;BR /&gt;310,Jennifer,Story,210,Don Mountain&lt;BR /&gt;410,Jackson,Hilltop,310,Jennifer Story&lt;BR /&gt;500,Al,Mudd,310,Jennifer Story&lt;/P&gt;&lt;P&gt;desired outcome:&lt;BR /&gt;ID,FIRST_NAME,LAST_NAME,SUPERVISOR_ID,SUPERVISOR_NAME,LEVEL,HC_LEVEL_4,HC_LEVEL_5,HC_LEVEL_6,HC_LEVEL_7&lt;BR /&gt;1234567890,Rachel,Pail,1234567890,Rachel Pail,4,Rachel Pail,,,&lt;BR /&gt;100,Jill,Hill,1234567890,Rachel Pail,5,Rachel Pail,Jill Hill,,&lt;BR /&gt;210,Don,Mountain,100,Jill Hill,6,Rachel Pail,Jill Hill,Don Mountain,&lt;BR /&gt;310,Jennifer,Story,210,Don Mountain,7,Rachel Pail,Jill Hill,Don Mountain,Jennifer Story&lt;BR /&gt;410,Jackson,Hilltop,310,Jennifer Story,7,Rachel Pail,Jill Hill,Don Mountain,Jackson Hilltop&lt;BR /&gt;500,Al,Mudd,310,Jennifer Story,7,Rachel Pail,Jill Hill,Don Mountain,Al Mudd&lt;/P&gt;&lt;P&gt;actual outcome:&lt;BR /&gt;final_id,final_first_name,final_last_name,final_supervisor_id,final_supervisor_name,final_level,final_hc_level_4,final_hc_level_5,final_hc_level_6,final_hc_level_7&lt;BR /&gt;100,Jill,Hill,1234567890,Rachel Pail,4,Jill Hill,-,-,-&lt;BR /&gt;100,Jill,Hill,1234567890,Rachel Pail,5,Rachel Pail,Jill Hill,-,-&lt;BR /&gt;210,Don,Mountain,100,Jill Hill,6,Rachel Pail,-,Don Mountain,-&lt;BR /&gt;310,Jennifer,Story,210,Don Mountain,7,Rachel Pail,-,-,Jennifer Story&lt;BR /&gt;410,Jackson,Hilltop,310,Jennifer Story,8,Rachel Pail,-,-,-&lt;BR /&gt;1234567890,Rachel,Pail,1234567890,Rachel Pail,4,Rachel Pail,-,-,-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;data load script wip:
// Step 1: Load the source QVD data
Headcount_Table:
LOAD
    id AS headcount_id,
    FIRST_NAME AS headcount_first_name,
    LAST_NAME AS headcount_last_name,
    SUPERVISOR_id AS headcount_supervisor_id,
    SUPERVISOR_NAME AS headcount_supervisor_name
INLINE [
    id, FIRST_NAME, LAST_NAME, SUPERVISOR_id, SUPERVISOR_NAME
    100, Jill, Hill, 1234567890, Rachel Pail
    210, Don, Mountain, 100, Jill Hill
    1234567890, Rachel, Pail, 1234567890, Rachel Pail
    310, Jennifer, Story, 210, Don Mountain
    410, Jackson, Hilltop, 310, Jennifer Story
    500, Al, Mudd, 310, Jennifer Story
];

// Step 2: Assign the top-level supervisor (Level 4)
Hierarchy_Assigned:
LOAD
    headcount_id AS hierarchy_id,
    headcount_first_name AS hierarchy_first_name,
    headcount_last_name AS hierarchy_last_name,
    headcount_supervisor_id AS hierarchy_supervisor_id,
    headcount_supervisor_name AS hierarchy_supervisor_name,
    4 AS hierarchy_level,
    headcount_first_name &amp;amp; ' ' &amp;amp; headcount_last_name AS hierarchy_hc_level_4,
    NULL() AS hierarchy_hc_level_5,
    NULL() AS hierarchy_hc_level_6,
    NULL() AS hierarchy_hc_level_7
RESIDENT Headcount_Table
WHERE headcount_supervisor_id = '1234567890';

// Store the Level 4 table into a QVD for interim processing
STORE Hierarchy_Assigned INTO [$(vMainPath)QVD_files/Hierarchy_Assigned.qvd] (qvd);

// Step 3: Iteratively assign levels and populate hierarchy fields
LET vRowsAdded = 1; // Initialize a variable to track if rows are added

DO WHILE vRowsAdded &amp;gt; 0

    // Load the next level of employees
    Next_Level:
    LOAD
        headcount_id AS hierarchy_id,
        headcount_first_name AS hierarchy_first_name,
        headcount_last_name AS hierarchy_last_name,
        headcount_supervisor_id AS hierarchy_supervisor_id,
        headcount_supervisor_name AS hierarchy_supervisor_name,
        Peek('hierarchy_level', -1, 'Hierarchy_Assigned') + 1 AS hierarchy_level,
        Peek('hierarchy_hc_level_4', -1, 'Hierarchy_Assigned') AS hierarchy_hc_level_4,
        IF(Peek('hierarchy_level', -1, 'Hierarchy_Assigned') = 4, headcount_first_name &amp;amp; ' ' &amp;amp; headcount_last_name) AS hierarchy_hc_level_5,
        IF(Peek('hierarchy_level', -1, 'Hierarchy_Assigned') = 5, headcount_first_name &amp;amp; ' ' &amp;amp; headcount_last_name) AS hierarchy_hc_level_6,
        IF(Peek('hierarchy_level', -1, 'Hierarchy_Assigned') = 6, headcount_first_name &amp;amp; ' ' &amp;amp; headcount_last_name) AS hierarchy_hc_level_7
    RESIDENT Headcount_Table
    WHERE headcount_supervisor_id = Peek('hierarchy_id', -1, 'Hierarchy_Assigned');

    // Check if the Next_Level table has rows
    LET vRowsAdded = NoOfRows('Next_Level');

    // If rows were added, store and concatenate them
    IF vRowsAdded &amp;gt; 0 THEN
        // Store the next level into a QVD for interim processing
        STORE Next_Level INTO [$(vMainPath)QVD_files/Next_Level.qvd] (qvd);

        // Concatenate the new level into the Hierarchy_Assigned table
        CONCATENATE (Hierarchy_Assigned)
        LOAD
            *
        RESIDENT Next_Level;

        // Drop the Next_Level table to free memory
        DROP TABLE Next_Level;
    END IF

LOOP

// Step 4: Create the final table with hierarchical levels and management structure
Final_Table:
LOAD
    hierarchy_id AS final_id,
    hierarchy_first_name AS final_first_name,
    hierarchy_last_name AS final_last_name,
    hierarchy_supervisor_id AS final_supervisor_id,
    hierarchy_supervisor_name AS final_supervisor_name,
    hierarchy_level AS final_level,
    hierarchy_hc_level_4 AS final_hc_level_4,
    hierarchy_hc_level_5 AS final_hc_level_5,
    hierarchy_hc_level_6 AS final_hc_level_6,
    hierarchy_hc_level_7 AS final_hc_level_7
RESIDENT Hierarchy_Assigned;

// Store the final table into a QVD
//STORE Final_Table INTO [$(vMainPath)QVD_files/Final_Table.qvd] (qvd);

// Drop intermediate tables
DROP TABLE Headcount_Table;
DROP TABLE Hierarchy_Assigned;&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 06 May 2025 21:53:58 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516891#M105844</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-05-06T21:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516931#M105846</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Did you tried to use the Hierarchy&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Headcount_Table:&lt;BR /&gt;LOAD&lt;BR /&gt;id AS headcount_id,&lt;BR /&gt;FIRST_NAME AS headcount_first_name,&lt;BR /&gt;LAST_NAME AS headcount_last_name,&lt;BR /&gt;FIRST_NAME &amp;amp; ' ' &amp;amp; LAST_NAME AS headcount_full_name,&lt;BR /&gt;SUPERVISOR_id AS headcount_supervisor_id,&lt;BR /&gt;SUPERVISOR_NAME AS headcount_supervisor_name&lt;BR /&gt;INLINE [&lt;BR /&gt;id , FIRST_NAME , LAST_NAME , SUPERVISOR_id , SUPERVISOR_NAME&lt;BR /&gt;100 , Jill , Hill , 1234567890 , Rachel Pail&lt;BR /&gt;210 , Don , Mountain , 100 , Jill Hill&lt;BR /&gt;1234567890 , Rachel , Pail , 1234567890 , Rachel Pail&lt;BR /&gt;310 , Jennifer , Story , 210 , Don Mountain&lt;BR /&gt;410 , Jackson , Hilltop , 310 , Jennifer Story&lt;BR /&gt;500 , Al , Mudd , 310 , Jennifer Story&lt;BR /&gt;];&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;T_Hierarchy:&lt;/FONT&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;Hierarchy(headcount_id,headcount_supervisor_id, headcount_full_name)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Load &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;*&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Resident Headcount_Table;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Drop Table Headcount_Table;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;You will get this (in yellow, new columns added by the prefix Hierarchy before the load) :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SRA_0-1746606405057.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/180275i791D65F5F9BA1137/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SRA_0-1746606405057.png" alt="SRA_0-1746606405057.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;More info about Hierarchies :&amp;nbsp;&lt;A href="https://community.qlik.com/t5/Member-Articles/Hierarchies/ta-p/1487801" target="_blank"&gt;https://community.qlik.com/t5/Member-Articles/Hierarchies/ta-p/1487801&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;SRA&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2025 08:28:16 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516931#M105846</guid>
      <dc:creator>SRA</dc:creator>
      <dc:date>2025-05-07T08:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516968#M105852</link>
      <description>&lt;P&gt;there is already an out-of-the-box function named Hierarchy that solved your requirement:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Hierarchy&lt;/P&gt;&lt;P&gt;The&amp;nbsp;hierarchy&amp;nbsp;prefix is used to transform a parent-child hierarchy table to a table that is useful in a&amp;nbsp;Qlik Sense&amp;nbsp;data model. It can be put in front of a&amp;nbsp;LOAD&amp;nbsp;or a&amp;nbsp;SELECT&amp;nbsp;statement and will use the result of the loading statement as input for a table transformation.&lt;/P&gt;&lt;P&gt;The prefix creates an expanded nodes table, which normally has the same number of records as the input table, but in addition each level in the hierarchy is stored in a separate field. The path field can be used in a tree structure.&lt;/P&gt;&lt;P&gt;Syntax:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hierarchy (NodeID, ParentID, NodeName, [ParentName, [PathSource, [PathName, [PathDelimiter, Depth]]]])(loadstatement | selectstatement)&amp;nbsp;&lt;/P&gt;&lt;P&gt;the first 3 parameters are obligatory, the remaining are optional:&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hierarchy(NodeID, ParentID, NodeName, ParentName, NodeName, PathName, '\', Depth) LOAD * inline [&lt;/P&gt;&lt;P&gt;NodeID, ParentID, NodeName&lt;/P&gt;&lt;P&gt;1, 4, London&lt;/P&gt;&lt;P&gt;2, 3, Munich&lt;/P&gt;&lt;P&gt;3, 5, Germany&lt;/P&gt;&lt;P&gt;4, 5, UK&lt;/P&gt;&lt;P&gt;5, , Europe&lt;/P&gt;&lt;P&gt;];&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ali_hijazi_0-1746616968359.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/180287iAB5A013C512275A3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ali_hijazi_0-1746616968359.png" alt="ali_hijazi_0-1746616968359.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2025 11:23:54 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516968#M105852</guid>
      <dc:creator>ali_hijazi</dc:creator>
      <dc:date>2025-05-07T11:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516985#M105857</link>
      <description>&lt;P&gt;See more on&lt;BR /&gt;&lt;A href="https://community.qlik.com/t5/Design/Unbalanced-n-level-hierarchies/ba-p/1474325" target="_blank"&gt;https://community.qlik.com/t5/Design/Unbalanced-n-level-hierarchies/ba-p/1474325&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://community.qlik.com/t5/Member-Articles/Hierarchies/ta-p/1487801" target="_blank"&gt;https://community.qlik.com/t5/Member-Articles/Hierarchies/ta-p/1487801&lt;/A&gt;&lt;/P&gt;&lt;P&gt;HIC&lt;/P&gt;&lt;P&gt;PS You will also find all this in&amp;nbsp;&lt;A title="Qlik according to HIC" href="https://www.amazon.com/Qlik-according-HIC-Henric-Cronstr%C3%B6m/dp/9152799263/ref=sr_1_1" target="_blank" rel="noopener"&gt;Qlik according to HIC&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2025 12:35:44 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2516985#M105857</guid>
      <dc:creator>hic</dc:creator>
      <dc:date>2025-05-07T12:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2517003#M105859</link>
      <description>&lt;P&gt;Thank you sir, I've seen your avatar and work around, but didn't know you published a book.&amp;nbsp; I'll give the links a shot.&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2025 13:18:51 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2517003#M105859</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-05-07T13:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with recursive logic in building a hierarchy qvd</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2517025#M105866</link>
      <description>&lt;P&gt;TY, your code got me the closest to what I was needing; and no I hadn't heard of Hierarchy before, so I've been doing things the hard way for a while :)-.&lt;/P&gt;&lt;P&gt;Small sample of what I am working with now.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// 1) Raw data
Headcount_Raw:
LOAD
  ID           AS ID,
  FIRST_NAME   AS FIRST_NAME,
  LAST_NAME    AS LAST_NAME,
  SUPERVISOR_ID    AS SUPERVISOR_ID,
  FIRST_NAME &amp;amp; ' ' &amp;amp; LAST_NAME  AS LEVEL
INLINE [
  ID, FIRST_NAME, LAST_NAME, SUPERVISOR_ID
  100, Jill, Hill,              1234567890
  210, Don,  Mountain,          100
  1234567890, Rachel, Pail,     1234567890
  310, Jennifer, Story,         210
  410, Jackson, Hilltop,        310
  500, Al, Mudd,                 310
];

// 2) Build hierarchy — load *only* ID, SUPERVISOR_ID, FullName.
//    Qlik will auto-generate Path and Depth fields here.

HierarchyTable:
Hierarchy(ID,SUPERVISOR_ID,LEVEL,ParentName,LEVEL,PathName,'|',Depth)
LOAD *
RESIDENT Headcount_Raw;

DROP TABLE Headcount_Raw;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2025 15:49:04 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-recursive-logic-in-building-a-hierarchy-qvd/m-p/2517025#M105866</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-05-07T15:49:04Z</dc:date>
    </item>
  </channel>
</rss>

