Forum Discussion
How to make hierarchical path use visual filters
- 5 years ago
Hi DSenkovic
Here's an initial idea - PBIX attached.
I'm working on the assumption that you have a data model with:
- Nodes table, with columns Node, Parent and Path (created using PATHITEM)
- NodeValue table containing Node and Value columns.
- Relationship between NodeValue[Node] and Nodes[Node]
Path = PATH ( Nodes[Node], Nodes[Parent] )Then you can create a measure like the one below (note that this can most likely be optimized).
Assumptions I made in writing the measure:
- You only want the measure evaluated for a single node at a time.
- The measure calculates the value sum for the topmost node in the path for the currently filtered node, within the overall selection of nodes (using ALLSELECTED).
Topmost node value = VAR CurrentNode = SELECTEDVALUE ( Nodes[Node] ) RETURN IF ( NOT ISBLANK ( CurrentNode ), -- Only return result if single node selected VAR CurrentNodePath = -- Get path of current node SELECTEDVALUE ( Nodes[Path] ) VAR CurrentNodePathLength = PATHLENGTH ( CurrentNodePath ) VAR CurrentNodePathTable = -- Table containing Node/Position pairs for CurrentNodePath SELECTCOLUMNS ( GENERATESERIES ( 1, CurrentNodePathLength ), -- Table of values {1,2,...,CurrentNodePathLength} "@Node", PATHITEM ( CurrentNodePath, [Value] ), "@Position", [Value] ) VAR VisibleNodesAllSelected = -- List of all nodes selected overall CALCULATETABLE ( VALUES ( Nodes[Node] ), ALLSELECTED () ) VAR CurrentNodePathTableFiltered = -- Filter path to just those nodes selected overall FILTER ( CurrentNodePathTable, [@Node] IN VisibleNodesAllSelected ) VAR TopNode = -- Get the top Node (i.e. with minimum position in the path) -- and treat as Nodes[Node] value TREATAS ( SELECTCOLUMNS ( TOPN ( 1, CurrentNodePathTableFiltered, [@Position], ASC ), "@TopNode", [@Node] ), Nodes[Node] ) RETURN CALCULATE ( [Value Sum], TopNode, ALL ( Nodes ) ) )For example, if you select N1, N11, N12, N2, N14, then:
- N1, N11 and N12 have Topmost node value = 7
- N2 and N14 have Topmost node value = 8
Hopefully this is of some use and can be adapted to your model.
Regards,
Owen
OwenAuger Thank you for your help so far. Just a quick question.
If I had a new (child) table to the Node table - let's call it a Leaf table. It has foreign key to node_id of course among other fields. Any Node can have any number of Leaves.
If I needed to display the Topmost node measure value in the visual that displays the Leaves data of selected Nodes, I suppose that the measure would need to be changed in a way that SELECTEDVALUE is no longer the way to get the current node for the Leaf. I should somehow use the Node from the filter context of the Leaf.
Is that correct way of thinking? Thank you.
You're welcome! 🙂
I just managed to get back to looking at this.
The exact way of handling this depends on your data model setup.
I have attached an updated PBIX using option 1 below, with a NodeChild table added.
- You could set up the model with the NodeChild table having a bidirectional relationship with the Node table. This would mean the filter on a Child would propogate to the corresponding node.
There would just be a small tweak required to the measure near the end, changing ALL ( Nodes ) to ALLCROSSFILTERED ( Nodes ). This is because we need to remove all filters affecting the Nodes table, including those on the many-side of a relationship with Nodes.
I have used this method in the attached PBIX. - If you don't have a bidirectional relationship between Nodes and NodeChild, then you need to use DAX to filter Nodes based on NodeChild. There are different ways to do this, but one is to wrap the SELECTEDVALUE calls in CALCULATE with NodeChild as a filter argument, so you end up with code like below:
VAR CurrentNode =
CALCULATE (
SELECTEDVALUE ( Nodes[Node] ),
NodeChild
)
VAR CurrentNodePath =
CALCULATE (
SELECTEDVALUE ( Nodes[Path] ),
NodeChild
)
Hope this helps!
Regards,
Owen
- DSenkovic5 years agoNew Member
Thank you!