How do I create a custom index column in PowerQuery? -


i have following data in powerquery:

| parentx | | | parenty | | | parentz | | | parenty | b | | parentz | b | | parentx | c | | parenty | c | | parentz | c | 

i want add index column counts number of parents element:

| parentx | | 3 | | parenty | | 2 | | parentz | | 1 | | parenty | b | 2 | | parentz | b | 1 | | parentx | c | 3 | | parenty | c | 2 | | parentz | c | 1 | 

the end goal pivot based on new column this:

| object | root    | parent 2 | parent 3 | |      | parentz | parenty  | parentx  | | b      | parentz | parenty  |          | | c      | parentz | parenty  | parentx  | 

here's query used generate index column in question:

let     // has original parent/child column     source = #"parent child query",      // count number of parents per child     #"grouped rows" = table.group(source, {"attribute:id"}, {{"count", each table.rowcount(_), type number}}),      // add new column of lists indexes per child     #"added custom" = table.addcolumn(#"grouped rows", "parentindex", each list.numbers([count], [count], -1)),      // expand lists in previous step     #"expand parentindex" = table.expandlistcolumn(#"added custom", "parentindex"),      // create column name columns (parent.1, parent.2, etc)     #"added custom1" = table.addcolumn(#"expand parentindex", "parentcolumn", each "parent."&text.from([parentindex])),      // adds index column use when merging original table     #"added index" = table.addindexcolumn(#"added custom1", "index", 0, 1) in     #"added index" 

once done created query hold merged result:

let     // original parent/child column     source = #"parent child query",      // add index column matches index column in previous query     #"added index" = table.addindexcolumn(source, "index", 0, 1),      // merge 2 queries based on index columns     merge = table.nestedjoin(#"added index",{"index"},#"epic parent indices",{"index"},"newcolumn"),      // expand new column     #"expand newcolumn" = table.expandtablecolumn(merge, "newcolumn", {"parentcolumn"}, {"parentcolumn"}),      // remove index column     #"removed columns" = table.removecolumns(#"expand newcolumn",{"index"}),      // sort data attribute , parent column columns in 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 3 key steps here:

  1. use table.group number of parents per child element.
  2. use list.numbers index values each parent/child relationship.
  3. use table.addindexcolumn add index columns used key in call table.join if don't you'll duplicate data in merge.

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -