Forum Discussion

DSenkovic's avatar
DSenkovic
New Member
5 years ago
Solved

How to make hierarchical path use visual filters

Hi everyone,   I have a table which contains some hierarchical data with id and parentid fields among others fields. I can create appropriate structure with PATH (and other path related DAX functi...
  • OwenAuger's avatar
    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:

    1. Nodes table, with columns Node, Parent and Path (created using PATHITEM)
    2. NodeValue table containing Node and Value columns.
    3. 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