Forum Discussion

hk2018086's avatar
hk2018086
Icon for Helper I rankHelper I
7 years ago
Solved

Find the Parent

 

Hi 

I need help to find the parent of each part as per the illustration below. I am trying to do this in Powerquery and an Index column can be added if required.

 

INPUT

 

LevelPart
1A
2B
3C
4D
2E
3F
3D

 

 

Desired output

 

LevelPartParent
1Anull
2BA
3CB
4DC
2EA
3FE
3DE
  • hk2018086

     

    Here is a Power Query Solution

    Please see attached file as well

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjICspzALGMgyxnMMgGyXOCyrnBZNzgLKBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Level = _t, Part = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Level", Int64.Type}, {"Part", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Parent Level", each [Level]+1),
        #"Merged Queries" = Table.NestedJoin(#"Added Custom",{"Level"},#"Added Custom",{"Parent Level"},"Added Custom",JoinKind.LeftOuter),
        #"Added Custom1" = Table.AddColumn(#"Merged Queries", "Custom", each let myindex = [Index] in
    Table.Max(Table.SelectRows([Added Custom],each [Index] < myindex),"Index")),
        #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom1", "Custom", {"Part"}, {"Custom.Part"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Index", "Parent Level", "Added Custom"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.Part", "Parent"}})
    in
        #"Renamed Columns"

11 Replies

  • Hey,

     

    this could be done, but for now an essental piece of information is missing in your sample data - a row index, that reflects the order of the rows in your sample data.

     

    Without this row index, it will not be possible, due to the ambiguous answers to the question "who is the parent of the current chils with the level id 3"

     

    With this row index, this parent can be determined by also looking for the parent with max rowindex.

     

    Please chcek if this will be possibel, and update your sample data accordingly, than we can provide you with the appropriate DAX statement.

     

    Regards,

    Tom

     

     

     

     

    • hk2018086's avatar
      hk2018086
      Icon for Helper I rankHelper I
      Hi Tom
      Adding an index is no issue. Please assume an index column.
    • hk2018086's avatar
      hk2018086
      Icon for Helper I rankHelper I

      that's because there is a level 2 that comes first when going from down to up.

      • Zubair_Muhammad's avatar
        Zubair_Muhammad
        Icon for Community Champion rankCommunity Champion

        hk2018086

         

        With help of supporting index column

         

        Column =
        MINX (
            TOPN (
                1,
                FILTER (
                    Table1,
                    [Level]
                        = EARLIER ( [Level] ) - 1
                        && [Index] < EARLIER ( [Index] )
                ),
                [Index], DESC
            ),
            [Part]
        )
        

  • Hey,

    I added an index column to the table using PowerQuery.

    Using this DAX statement to create a  CALCULATED column

    Parent = 
    var parentlevel = 'Table1'[Level] - 1
    var currentIndex = 'Table1'[Index]
    var indexofParent =
    CALCULATE(
    MAX('Table1'[Index])
    ,FILTER(ALL('Table1')
    ,'Table1'[Level] = parentlevel && 'Table1'[Index] < currentIndex
    )
    )
    return
    CALCULATE(
    FIRSTNONBLANK('Table1'[Part],0)
    ,FILTER(ALL('Table1')
    ,'Table1'[Level] = parentlevel && 'Table1'[Index] = indexofParent
    )
    )

    creates this output:

     

    I guess this is what you are looking for.

     

    Regards,
    Tom

    • hk2018086's avatar
      hk2018086
      Icon for Helper I rankHelper I

      Is there a way this can be done in powerquery?

    • TomMartens's avatar
      TomMartens
      Icon for Super User rankSuper User

      Ah, just read that you want a Power Query solution.

      Will provide this tomorrow.

       

      Regards,

      Tom

      • Zubair_Muhammad's avatar
        Zubair_Muhammad
        Icon for Community Champion rankCommunity Champion

        hk2018086

         

        Here is a Power Query Solution

        Please see attached file as well

         

         

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjICspzALGMgyxnMMgGyXOCyrnBZNzgLKBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Level = _t, Part = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Level", Int64.Type}, {"Part", type text}}),
            #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Parent Level", each [Level]+1),
            #"Merged Queries" = Table.NestedJoin(#"Added Custom",{"Level"},#"Added Custom",{"Parent Level"},"Added Custom",JoinKind.LeftOuter),
            #"Added Custom1" = Table.AddColumn(#"Merged Queries", "Custom", each let myindex = [Index] in
        Table.Max(Table.SelectRows([Added Custom],each [Index] < myindex),"Index")),
            #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom1", "Custom", {"Part"}, {"Custom.Part"}),
            #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Index", "Parent Level", "Added Custom"}),
            #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.Part", "Parent"}})
        in
            #"Renamed Columns"