Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

Hierarchies are very common in all database and business intelligence solutions. Usually they are balanced and with a fix number of levels, and then they do not pose any problems. Just load the data, add a drill-down group, and you’re done.


Adjacent Notes source 2.png

But there is one type of hierarchy that is somewhat tricky to get right – an unbalanced, n-level hierarchy. Typical for this type of hierarchy is that the levels are not named, and you really don’t know on which level you need to search for a specific node.

 

Usually such a hierarchy is stored in an Adjacent Nodes table, i.e. a table that has one record per node and each node has a reference to its parent.

 

Such a table can be loaded into QlikView directly using the Hierarchy prefix. This prefix will transform the Adjacent Nodes table into an Expanded Nodes table that has additional fields that you can use in your app.

 

Data model single table - BP.png

 

With the fields in this table, you can easily create a pivot table and a tree-view list box. Below you can see some wine districts displayed in both these object types:

 

Tables - BP.png

 

One challenge with hierarchies is that you can refer to a node in two different ways: Either to the node including the entire sub-tree, or to the node only, excluding all sub-nodes. In the example with the wine districts, it would mean any wine from Bordeaux, and unspecified Bordeaux, respectively. In the pivot table above, the difference is obvious: Any wine from Bordeaux sums up to 150 units, and the unspecified Bordeaux sums up to 18 units.

 

A user usually wants to make selections referring to the entire sub-tree, but the above solution does not have any field for this. To create such a field, you need the second hierarchy-resolving prefix – the HierarchyBelongsTo.

 

This prefix will also transform the hierarchy table. The result will be a table containing one record per descendant-ancestor pair. In other words, the ancestor (tree ID) will link to all its descendants (node ID), and can thus be used to make selections of entire sub-trees. (The “TreeBridge” table in the picture below.)

 

But it doesn’t stop here… The above solution creates one field in which tree searches can be made, but in order to create a drill-down for trees, you need an additional table – an expanded nodes table for the trees. This can be created with a second Hierarchy statement, but now one that links to the tree ID instead of the node ID. (The “Trees” table in the picture below.)

 

Data model full - BP.png

 

The data model with the three hierarchy tables is the one I recommend: It generates all fields you need.

 

A more elaborate explanation with script examples can be found in the technical brief about Hierarchies.

 

HIC

 

Further reading related to this topic:

Authorization using a Hierarchy

Bill of Materials

41 Comments
Partner
Partner

Hi!

Unfortunately HIERARCHY Load is not able to handle BillOfMaterials(BOMs) correctly.

If for example the same screw is used multiple times in a single BOM, Hierarchy Load does not generate all nodes correctly.

I once wrote a blog post about this issue (here) - hope your German is still good enough

Any plans to change this in the future? Currently I solve this through a recursive SQL statement.

Thx,

Roland

7,601 Views
Not applicable

Great reading once again!

I know it is a blog - But coding examples would be nice included in blog. I know it is possible to get the solution. But examples written could very nice!

7,601 Views
Not applicable

follow his link

0 Likes
7,601 Views

Roland

You're right that the Hierarchy prefix cannot resolve a BOM correctly, but I think there is a good reason for this: A Bill Of Materials is really not a dimensional hierarchy. First of all, as you point out, a node can have several parents. Secondly, the same node can exist many times in the tree and have different quantities, depending on where in the tree it is found: The nodes are instantiated and each instance (e.g. the screw in the wheel) has a number.

Thirdly, the total quantity (of the dimension "screw") should be calculated - it is the sum of the product of all the quantities of the instances above.

All this makes the BOM a transactional table rather than a dimensional.

I would load the BOM using a For-Next loop and some advanced scripting that does the above calculation. Maybe this is material for a blog post...

HIC

7,601 Views

@Roland Vecera

You should be able to load a BOM using the following (The quantities in "QuantityPerPath"):

BOM:

Hierarchy (Part, Parent, PartName, ParentName, NodeQuantity, QuantityPath, '/', Depth)

LOAD Part,

  Part as PartName,

  Parent,

  Text(Quantity) as NodeQuantity

FROM BOM.xlsx (ooxml, embedded labels, table is Sheet1);

Sums:

Load Exp(Sum(lnQuantity)) as QuantityPerPath,

  QuantityPath

  Group By QuantityPath;

Load Log(Subfield(QuantityPath,'/')) as lnQuantity,

  QuantityPath

  Resident BOM ;

// HIC

0 Likes
7,601 Views
Specialist III
Specialist III

Thank you, Henric. As always, your posts and comments are what keeps me reading QlikCommunity.

0 Likes
7,601 Views