1

I have the following data in PowerQuery:

| ParentX | A |
| ParentY | A |
| ParentZ | A |
| ParentY | B |
| ParentZ | B |
| ParentX | C |
| ParentY | C |
| ParentZ | C |

I want to add an index column that counts the number of parents for an element:

| ParentX | A | 3 |
| ParentY | A | 2 |
| ParentZ | A | 1 |
| ParentY | B | 2 |
| ParentZ | B | 1 |
| ParentX | C | 3 |
| ParentY | C | 2 |
| ParentZ | C | 1 |

The end goal is to pivot based on this new column like this:

| Object | Root    | Parent 2 | Parent 3 |
| A      | ParentZ | ParentY  | ParentX  |
| B      | ParentZ | ParentY  |          |
| C      | ParentZ | ParentY  | ParentX  |

2 Answers 2

3

Here's the query I used to generate the index column in the question:

let
    // This has the original parent/child column
    Source = #"Parent Child Query",

    // Count the number of parents per child
    #"Grouped Rows" = Table.Group(Source, {"Attribute:id"}, {{"Count", each Table.RowCount(_), type number}}),

    // Add a new column of lists with the indexes per child
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "ParentIndex", each List.Numbers([Count], [Count], -1)),

    // Expand the lists in the previous step
    #"Expand ParentIndex" = Table.ExpandListColumn(#"Added Custom", "ParentIndex"),

    // Create the column name columns (Parent.1, Parent.2, etc)
    #"Added Custom1" = Table.AddColumn(#"Expand ParentIndex", "ParentColumn", each "Parent."&Text.From([ParentIndex])),

    // Adds an index column that you use when merging with the original table
    #"Added Index" = Table.AddIndexColumn(#"Added Custom1", "Index", 0, 1)
in
    #"Added Index"

Once this was done I created another query to hold the merged result:

let
    // This is the original parent/child column
    Source = #"Parent Child Query",

    // Add an index column that matches the index column in the previous query
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),

    // Merge the two queries based on the index columns
    Merge = Table.NestedJoin(#"Added Index",{"Index"},#"Epic Parent Indices",{"Index"},"NewColumn"),

    // Expand the new column
    #"Expand NewColumn" = Table.ExpandTableColumn(Merge, "NewColumn", {"ParentColumn"}, {"ParentColumn"}),

    // Remove the index column
    #"Removed Columns" = Table.RemoveColumns(#"Expand NewColumn",{"Index"}),

    // Sort the data by attribute and then by Parent column so the columns will be in the right order
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Attribute:id", Order.Descending}, {"ParentColumn", Order.Ascending}}),

    // Pivot!
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[ParentColumn]), "ParentColumn","Parent:id")
in
    #"Pivoted Column"

There were three key steps here:

  1. Use Table.Group to get the number of parents per child element.
  2. Use List.Numbers to get index values for each parent/child relationship.
  3. Use Table.AddIndexColumn to add index columns to be used as the key in the call to Table.Join If you don't do this then you'll get duplicate data in the merge.
Sign up to request clarification or add additional context in comments.

Comments

0
  1. Create an Excel Table with 2 Columns (Parents, Child)
  2. Use this Table in Power Query
  3. Insert function Combiner.CombineTextByDelimiter(";") (See Line3)
  4. Group by Child and use function above (See Line 4)
  5. Split result (Line 5)

The code:

let
    Quelle    = Excel.CurrentWorkbook(){[Name="Tabelle2"]}[Content],
    fcombine  = Combiner.CombineTextByDelimiter(";"), 
    #"Group1" = Table.Group(Quelle, {"Child"}, {{"Parents", each fcombine([Parent]), type text}}),
    #"Split1" = Table.SplitColumn(#"Group1", "Parents", Splitter.SplitTextByDelimiter(";"),{"Parents.1", "Parents.2", "Parents.3"}),
    #"Result" = Table.TransformColumnTypes(#"Split1", {{"Parents.1", type text}, {"Parents.2", type text}, {"Parents.3", type text}})
in
    #"Result"

Greetings R.

1 Comment

What do you mean by "insert function Combiner.CombineTextByDelimiter(";")"? I can't use CombineTextByDelimiter with Group By. Also, will this work for an arbitrary number of parents? that's what I have -- I use a max of three in the example but my data has more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.