我有一个问题需要在电源查询中解决。
我的表包含列名称,ID和父ID
Name ID Parent
A 1
B 2 1
C 3 1
D 4 2
E 5 2
我需要转换这个表,以便为每个行(和原始)获得一行。在这个例子中,这将产生11行。
Name ID
A 1
A 2
A 3
A 4
A 5
B 2
B 4
B 5
C 3
etc.
我知道这是通过某种加入完成的,但感觉就像一个循环,我不知道如何在Power Query中做循环。
答案 0 :(得分:0)
友好的reddit用户为此创建了解决方案。
(ID as list, CurrentItem as number, FullTable as table) as list =>
let
CurrentID = ID{CurrentItem},
ParentRows = Table.SelectRows(FullTable, each [Parent] = CurrentID),
AddNodes = if Table.RowCount(ParentRows) > 0 then List.Distinct(ID & ParentRows[ID]) else ID,
FinalResult = if (List.Count(ID) = List.Count(AddNodes)) and CurrentItem = List.Count(ID) - 1 then ID else BuildNodes(AddNodes, CurrentItem + 1, FullTable)
in
FinalResult
答案 1 :(得分:0)
试试这个,不一定是对的,请检查一下。其他人有没有更好的解决方案?
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Top = Table.SelectRows(Source, each ([Parent] = null)),
Path = Table.AddColumn(Top, "Path", each #table({"Name","ID"},{{[Name],[ID]}})),
Loop = Table.Combine(List.Generate(()=>
Path,
each not Table.IsEmpty(_),
each [
a = Table.NestedJoin(Source,{"Parent"},_,{"ID"},"join",JoinKind.Inner),
b = Table.ExpandTableColumn(a, "join", {"Path"}, {"Pre_Path"}),
c = Table.AddColumn(b,"Path", each [Pre_Path]&Table.Group([Pre_Path],"Name",{"ID",(x)=>[ID]})&#table({"Name","ID"},{{[Name],[ID]}}))
][c]
)),
Result = Table.Distinct(Table.Combine(Loop[Path])),
Sort = Table.Buffer(Table.Sort(Result,{{"Name",0}, {"ID",0}}))
in
Sort