求助:Power Query递归函数无法获取多层子节点问题排查
问题:Power Query递归函数无法处理多子节点的层级遍历
我编写了一个名为flecheD的Power Query函数,用于遍历Parent和Child两列,获取主父节点的所有子节点(输出至Children列):
(ColChild,ColParent,ParentActuel,source)=> let mylist=Table.Column(Table.SelectRows(source,each Record.Field(_,ColParent)=ParentActuel),ColChild), resultat=Text.Combine(mylist) in Text.Trim( if resultat ="" then "" else @ resultat &"|" & @ flecheD(ColChild,ColParent,resultat,source),"|")
但这个函数存在问题:当父节点有多个子节点时,resultat变量会变成多字符/多个子节点的组合,导致函数无法继续查找后续的子子节点。
为解决该问题,我尝试用List.Generate()创建了一个名为SubChildren的自定义函数:
(children as text, ColChild,ColParent,source)=> let i = 1, length = Text.Length(children), subchildren = List.Generate( ()=>@flecheD(ColChild,ColParent,Text.At(children,i-1),source), i<=length, i+1 ) in Text.Combine(subchildren)
并将其与初始函数结合:
(ColChild,ColParent,ParentActuel,source)=> let mylist=Table.Column(Table.SelectRows(source,each Record.Field(_,ColParent)=ParentActuel),ColChild), resultat=Text.Combine(mylist) in Text.Trim( if resultat ="" then "" else if Text.Length(resultat) = 1 then @ resultat &"|" & @ flecheD(ColChild,ColParent,resultat,source) else @resultat &"|"& SubChildren(resultat,ColChild,ColParent,source),"|")
理论上这个组合应该能获取每个子节点的子子节点,但实际无法正常运行,恳请提供帮助。
内容的提问来源于stack exchange,提问作者Steph
相关产品推荐
相关产品推荐

