2015-04-29 60 views
0

我在PowerQuery以下數據:如何在PowerQuery中創建自定義索引列?

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

我想補充一點,計數父母的一個元素數量的索引列:

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

的最終目標是支點在此基礎上這樣新的列:

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

回答

1

這是我用來產生問題的索引列的查詢:

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" 

一旦這樣做是我創建另一個查詢持有合併後的結果:

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" 

這裏有三個關鍵步驟:

  1. 使用Table.Group得到每個孩子元素的父母數量。
  2. 使用List.Numbers可獲取每個父/子關係的索引值。
  3. 使用Table.AddIndexColumn添加索引列以用作Table.Join調用中的鍵如果您不這樣做,那麼您將在合併中獲得重複的數據。
0
  1. 2列(Parents,0創建Excel表)
  2. 使用此表中電源查詢
  3. 插入功能Combiner.CombineTextByDelimiter(";")(見行3)
  4. 組由Child,並使用上述功能(見第4行)
  5. 分割結果(第5行)

代碼:

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" 

個問候 R.

+0

「插入函數Combiner.CombineTextByDelimiter(」;「)'」是什麼意思?我不能使用'CombineTextByDelimiter'和'Group By'。 此外,這將工作爲任意數量的父母?這就是我所擁有的 - 我在示例中使用了最多三個,但是我的數據更多。 – Vinay

相關問題