Most hierarchies are dimensional hierarchies. This implies several things: First, you have a many-to-one relationship between the levels: a day belongs to one (and only one) month, a month to one (and only one) quarter, etc. Strictly speaking, it is not a hierarchy unless this condition is fulfilled.
Secondly, the hierarchy contains no measures. Instead, numbers are stored in a transactional table that is linked to the dimensional hierarchy.
But there is another hierarchy-like structure, the Bill of Materials, the “BoM”. This is a list of items, assemblies and sub-assemblies representing the design of a product or device. Many products are planned and documented with BoMs.
A multi-level BoM depicts parent-child relationships and shows the hierarchical structure of the assemblies and their related subcomponents. A multi-level BoM is essentially a nested list whose items are listed to illustrate multiple assemblies within a product.
But a BoM is very different from a dimensional hierarchy. It does not have to have a strict many-to-one relationship between the levels. For instance, a specific bearing type can be used in several places. For the BoM, this means that the bearing can have several parents in the hierarchy.
Further, each line in the BoM has numbers in it, typically Quantity and Cost. These are measures that should be summed. In a sense, a BoM is more similar to a transaction table than a dimensional table.
A BoM can easily be loaded and analyzed with Qlik Sense or QlikView, but there are some challenges: First, the list often lacks a parent reference. Instead, the parent-child relationship is implied by the order of the rows and the Level field, and visualized by indentations.
Secondly, aggregating the measures is not straightforward. When summing the costs, the multiplicities of all the nodes above it must be taken into account. In the example in the above table, the wheel assembly uses 2 bearings, and the trailer uses 4 wheel assemblies. Then the trailer obviously needs 8 bearings. In other words: The row for the wheel assembly – and all rows belonging to it – must be looped 4 times when summing the cost.
Luckily, both these challenges can be handled in the Qlik script. One possible solution is the following:
The reference to the parent is created in two steps: First a Path is built using the Level and the Path of the above row. Having the path, it is straightforward to extract the parent id using Subfield().
Further, each row is loaded several times using a while loop. Hence, row 16 (the ball bearings) is loaded twice since its Quantity is 2. But it should be loaded 8 times since the Quantity of its parent (row 6, Wheel assembly) is 4. This multiplication is achieved using the Hierarchy prefix.
Finally, the above multiplication algorithm only works for integer quantities. For this reason the bottom Load splits the Quantity into two fields: a field Units that is used in the While loop, and an Amount that is used in the aggregation:
Sum( Amount * UnitCost )
However, this means that nodes that have non-integer quantities cannot have any children. If they do, the above algorithm cannot be used, and the cost roll-up must be made a different way. Luckily, this is rarely – or never – the case in real life.
The script I'm using looks very similar to yours only I keep the original materials table intact for the joining throughout the loop.
Also in the loop I add a temporary field in the join that I can count. That way I can keep track of how many joins actually occurred during last loop and keep on looping until there were no more joins.
(I do have a maximum also just in case there is some circular reference)
My original table does not contain any level field, I only have a parent-child relation table with quantities and a separate materials table.
I got the following error after manually building example. Below error image are the is the copy of the Excel data and the script I used. Can you take a peek and advise?
Thank you for the explanation above. It was really useful. However I have a requirement which is somewhat different. I don't have to show any measures, instead I have to display all the data in a tree-like structure.
I have to create a dashboard for BOM(Bills of Materials). Let me tell you what BOM is all about. There are some Materials which is having respective Components under it. These Components are further divided into Sub-Components which is under the same field i.e. Material. There is a flag known as Assembly Indicator which signifies if any Components is broken down into Sub-Components or not. The values of this flag are either 'X' or 'Null wherein 'X' tells us that the Components has child components(sub-components).
So my requirement is to publish these lines one after the other. Please see the example below:
Material
Component
Assembly Indicator
Mat1
Comp1
X
Mat1
Comp2
Mat1
Comp3
X
Mat2
Comp56
Mat3
Comp87
Mat7
Comp6
Comp1
SubComp1
X
Comp1
SubComp2
SubComp1
SubComp76
Comp3
SubComp33
So it should look like below table:
Since Material 'Mat1' has Component 'Comp1' and has an Assembly Indicator flag as 'X' so it's component line should be published/displayed soon after this Material 'Mat1' details and so on. It should display all the sub components detail unless it finds a Null flag.
Material
Component
Assembly Indicator
Mat1
Comp1
X
Mat1
Comp2
Mat1
Comp3
X
Comp1
SubComp1
X
Comp3
SubComp33
I hope I was able to explain. Eagerly awaiting your reply.
I tried your both BOM scripts. Somehow the results are different or so I think. I have chosen only labor Item for this mail as a sample. I have difficulties to understand the results. Somehow I think I understand that Total Quantity is ok ( I calculated it in excel) in the below table but how upper table equals the below one?
Thank you for this blog, its very helpful in understanding the concept and the script. However, when I tried using this same data, I am not able to get the correct output for the unit cost (rolled up sum for Trailer).
Also, in your script above, the Hierarchy Prefix shows 'Description', which is not in available in the source data. I am assuming that field is 'Item'.
Could you please provide a solution to show the roll-up cost for Trailer, Chassis and Wheel Assembly