Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

Parent and child calculation

i have a parent and child table, i wanted to calculate the values with respect to their root parents like a family tree

 

ParentChildValue
Site1Site210
Site2Site320
Site3Site415

 

The output should be,

 

ParentChildValueOutput
Site1Site21010 
Site2Site32030
Site3Site41545

 

Thanks in advance

 

3 Replies

  • AilleryO's avatar
    AilleryO
    Memorable Member

    Hi,

    Not 100% sure to understand your needs, but I suppose it's more complicated than your example.

    If you need to take into account both levels Parent and Child, you should use 3 variables in your calculation, one for Parent, one for Child and one for Index (could be a date if you have one ?).

    And basically you do your calculation, using filters like :

    Parent=VARParent && Child=VARChild && IndexValue<=VARCurrentIndex

    Hope it's clear enough, otheriwse do not hesitate to come back.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    You can create a measure as below to get it, please find the details in the attachment.

    Output = 
    VAR _parent =
        SELECTEDVALUE ( 'Table'[Parent] )
    VAR _child =
        SELECTEDVALUE ( 'Table'[Child] )
    VAR _tab =
        CALCULATETABLE (
            VALUES ( 'Table'[Child] ),
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Child] = _parent ||'Table'[Parent]=_parent)
        )
    VAR _tab2 =
        CALCULATETABLE (
            VALUES ( 'Table'[Parent] ),
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Child] = _parent )
        )
    RETURN
        CALCULATE (
            SUM ( 'Table'[Value] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
               'Table'[Child]
                    IN _tab || 
                        'Table'[Child] IN _tab2
            )
        )

    Best Regards

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous , this works fine. Thanks..

      what if i have duplicate child it gives wrong output. (on site6)

       

      Parent Child Value Output

      Site1Site21010
      Site2Site32030
      Site3Site41545
      Site4Site55095
      Site6Site51060

       

      or if i make it reverse , so the child will always be unique.. it give a wrong output now.. 

       

      Parent Child Value Column

      Site2Site11045
      Site3Site22085
      Site4Site31565
      Site5Site45060
      Site5Site61060

       

       

      the output should be as below.. when i make a unique child 

      ParentChildValueOutputParent in sequence that was added base on child name
      Site2Site11010+20+15+50+15=110Site1,Site2,Site3,Site4,Site5
      Site3Site22020+15+50+15=100Site2,Site3,Site4,Site5
      Site4Site31515+50+15=80Site3,Site4,Site5
      Site5Site45050+15=65Site4,Site5
      Site5Site61010+15=25Site6,Site5
       Site51515Site5

       

       

      Thanks again.