如何检查相应的父子列以标记为x?

huangapple go评论82阅读模式
英文:

How to check corresponding parent and child columns to be marked with x?

问题

我有一个数据表格源,其中有各种列,可以是x或null。根据一个层次表,我想检查源是否填写正确,如果不正确,可能列出数据不正确的子父路径。

要有效的规则/检查是:如果子级为x,则所有父级必须为x,如果父级至少有一个x,则至少一个子级必须为x。

我希望有人能够理解我想要实现的内容。

以下是示例代码:

  1. source = Table.FromRecords({
  2. [Name="Jason", A="x", B="x", C="x", D="x", E="x", F="x", G=null, H="x", I=null, J=null, K=null, L="x", M=null],
  3. [Name="Joe", A="x", B=null, C="x", D=null, E=null, F=null, G="x", H="x", I=null, J=null, K=null, L=null, M="x"],
  4. [Name="Eddie", A="x", B=null, C="x", D=null, E=null, F="x", G=null, H="x", I=null, J="x", K=null, L=null, M=null],
  5. [Name="Phil", A=null, B=null, C=null, D="x", E=null, F=null, G=null, H=null, I=null, J=null, K=null, L="x", M=null],
  6. [Name="Thomas", A="x", B=null, C=null, D=null, E=null, F="x", G="x", H="x", I=null, J=null, K=null, L=null, M=null],
  7. [Name="David", A="x", B=null, C=null, D=null, E="x", F="x", G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  8. [Name="Matthew", A=null, B="x", C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K="x", L=null, M=null]
  9. }),
  10. hierarchy = Table.FromRecords({
  11. [Column1 = "A", Hierarchy = {null}],
  12. [Column1 = "B", Hierarchy = {"A"}],
  13. [Column1 = "C", Hierarchy = {"A"}],
  14. [Column1 = "D", Hierarchy = {"A", "C"}],
  15. [Column1 = "E", Hierarchy = {"A", "C"}],
  16. [Column1 = "F", Hierarchy = {"A"}],
  17. [Column1 = "G", Hierarchy = {"A", "F"}],
  18. [Column1 = "H", Hierarchy = {"A", "F"}],
  19. [Column1 = "I", Hierarchy = {null}],
  20. [Column1 = "J", Hierarchy = {"I"}],
  21. [Column1 = "K", Hierarchy = {"I"}],
  22. [Column1 = "L", Hierarchy = {"I", "K"}],
  23. [Column1 = "M", Hierarchy = {"I", "K"}]
  24. }),

这里是一些手动比较的屏幕截图:链接

英文:

I've got a data table source, with various columns that could be either x or null. Depending on a hierarchy table, I would like to check the source whether it is filled out correctly, and if not maybe list the child to parent path where the data is incorrect.

The rule / check to be valid would be: if a child is x, all parents have to be x if a parent is x at least one child has to be x

I hope somebody is able to figure out, what I would like to achieve.

  1. source = Table.FromRecords({
  2. [Name="Jason", A="x", B="x", C="x", D="x", E="x", F="x", G=null, H="x", I=null, J=null, K=null, L="x", M=null],
  3. [Name="Joe", A="x", B=null, C="x", D=null, E=null, F=null, G="x", H="x", I=null, J=null, K=null, L=null, M="x"],
  4. [Name="Eddie", A="x", B=null, C="x", D=null, E=null, F="x", G=null, H="x", I=null, J="x", K=null, L=null, M=null],
  5. [Name="Phil", A=null, B=null, C=null, D="x", E=null, F=null, G=null, H=null, I=null, J=null, K=null, L="x", M=null],
  6. [Name="Thomas", A="x", B=null, C=null, D=null, E=null, F="x", G="x", H="x", I=null, J=null, K=null, L=null, M=null],
  7. [Name="David", A="x", B=null, C=null, D=null, E="x", F="x", G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  8. [Name="Matthew", A=null, B="x", C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K="x", L=null, M=null]
  9. }),
  10. hierarchy = Table.FromRecords({
  11. [Column1 = "A", Hierarchy = {null}],
  12. [Column1 = "B", Hierarchy = {"A"}],
  13. [Column1 = "C", Hierarchy = {"A"}],
  14. [Column1 = "D", Hierarchy = {"A", "C"}],
  15. [Column1 = "E", Hierarchy = {"A", "C"}],
  16. [Column1 = "F", Hierarchy = {"A"}],
  17. [Column1 = "G", Hierarchy = {"A", "F"}],
  18. [Column1 = "H", Hierarchy = {"A", "F"}],
  19. [Column1 = "I", Hierarchy = {null}],
  20. [Column1 = "J", Hierarchy = {"I"}],
  21. [Column1 = "K", Hierarchy = {"I"}],
  22. [Column1 = "L", Hierarchy = {"I", "K"}],
  23. [Column1 = "M", Hierarchy = {"I", "K"}]
  24. }),

Here is a screenshot of some manual comparison:

enter image description here

答案1

得分: 0

以下是翻译好的代码部分:

  1. let source = Table.FromRecords({
  2. [Name="Jason", A="x", B="x", C="x", D="x", E="x", F="x", G=null, H="x", I=null, J=null, K=null, L="x", M=null],
  3. [Name="Joe", A="x", B=null, C="x", D=null, E=null, F=null, G="x", H="x", I=null, J=null, K=null, L=null, M="x"],
  4. [Name="Eddie", A="x", B=null, C="x", D=null, E=null, F="x", G=null, H="x", I=null, J="x", K=null, L=null, M=null],
  5. [Name="Phil", A=null, B=null, C=null, D="x", E=null, F=null, G=null, H=null, I="x", J=null, K="x", L="x", M=null],
  6. [Name="Thomas", A="x", B=null, C=null, D=null, E=null, F="x", G="x", H="x", I=null, J=null, K=null, L=null, M=null],
  7. [Name="David", A="x", B=null, C=null, D=null, E="x", F="x", G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  8. [Name="Matthew", A=null, B="x", C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K="x", L=null, M=null]
  9. }),
  10. hierarchy = Table.FromRecords({
  11. [Column1 = "A", Hierarchy = {null}],
  12. [Column1 = "B", Hierarchy = {"A"}],
  13. [Column1 = "C", Hierarchy = {"A"}],
  14. [Column1 = "D", Hierarchy = {"A", "C"}],
  15. [Column1 = "E", Hierarchy = {"A", "C"}],
  16. [Column1 = "F", Hierarchy = {"A"}],
  17. [Column1 = "G", Hierarchy = {"A", "F"}],
  18. [Column1 = "H", Hierarchy = {"A", "F"}],
  19. [Column1 = "I", Hierarchy = {null}],
  20. [Column1 = "J", Hierarchy = {"I"}],
  21. [Column1 = "K", Hierarchy = {"I"}],
  22. [Column1 = "L", Hierarchy = {"I", "K"}],
  23. [Column1 = "M", Hierarchy = {"I", "K"}]
  24. }),
  25. #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(source, {"Name"}, "Attribute","Value"),
  26. #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Name"}, {"data", each
  27. let a = Table.NestedJoin(_, {"Attribute"}, hierarchy, {"Column1"}, "hierarchy", JoinKind.LeftOuter),
  28. b = Table.ExpandTableColumn(a, "hierarchy", {"Hierarchy"}, {"David"}),
  29. c = Table.ExpandListColumn(b, "David"),
  30. d = Table.NestedJoin(c, {"David"}, c, {"Attribute"}, "Ron", JoinKind.LeftOuter),
  31. e = Table.ExpandTableColumn(d, "Ron", {"Attribute"}, {"Peter"}),
  32. f = Table.SelectRows(e, each ([David] <> null) and ([Peter] = null)),
  33. g= Table.Group(f, {"Name","Attribute"}, {"Concat", each Text.Combine([David],", "), type text}),
  34. h = Table.AddColumn(g, "Custom", each [Attribute] & "->" & [Concat], type text),
  35. i= try Table.Group( h, {"Name"}, {"Concat", each Text.Combine([Custom],"; "), type text}) {0}[Concat] otherwise null
  36. in i , type text }),
  37. #"Merged Queries" = Table.NestedJoin(source, {"Name"},#"Grouped Rows", {"Name"}, "table2", JoinKind.LeftOuter),
  38. #"Expanded table2" = Table.ExpandTableColumn(#"Merged Queries", "table2", {"data"}, {"Errors"})
  39. in #"Expanded table2"

如果只需要错误列表,可以删除最后两个步骤。

英文:

Well, that was fun for once, assuming this works

(Updated Phil code input to match image)

如何检查相应的父子列以标记为x?

  1. let source = Table.FromRecords({
  2. [Name=&quot;Jason&quot;, A=&quot;x&quot;, B=&quot;x&quot;, C=&quot;x&quot;, D=&quot;x&quot;, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=null, J=null, K=null, L=&quot;x&quot;, M=null],
  3. [Name=&quot;Joe&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=null, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=&quot;x&quot;],
  4. [Name=&quot;Eddie&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=null, J=&quot;x&quot;, K=null, L=null, M=null],
  5. [Name=&quot;Phil&quot;, A=null, B=null, C=null, D=&quot;x&quot;, E=null, F=null, G=null, H=null, I=&quot;x&quot;, J=null, K=&quot;x&quot;, L=&quot;x&quot;, M=null],
  6. [Name=&quot;Thomas&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=null, F=&quot;x&quot;, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=null],
  7. [Name=&quot;David&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  8. [Name=&quot;Matthew&quot;, A=null, B=&quot;x&quot;, C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K=&quot;x&quot;, L=null, M=null]
  9. }),
  10. hierarchy = Table.FromRecords({
  11. [Column1 = &quot;A&quot;, Hierarchy = {null}],
  12. [Column1 = &quot;B&quot;, Hierarchy = {&quot;A&quot;}],
  13. [Column1 = &quot;C&quot;, Hierarchy = {&quot;A&quot;}],
  14. [Column1 = &quot;D&quot;, Hierarchy = {&quot;A&quot;, &quot;C&quot;}],
  15. [Column1 = &quot;E&quot;, Hierarchy = {&quot;A&quot;, &quot;C&quot;}],
  16. [Column1 = &quot;F&quot;, Hierarchy = {&quot;A&quot;}],
  17. [Column1 = &quot;G&quot;, Hierarchy = {&quot;A&quot;, &quot;F&quot;}],
  18. [Column1 = &quot;H&quot;, Hierarchy = {&quot;A&quot;, &quot;F&quot;}],
  19. [Column1 = &quot;I&quot;, Hierarchy = {null}],
  20. [Column1 = &quot;J&quot;, Hierarchy = {&quot;I&quot;}],
  21. [Column1 = &quot;K&quot;, Hierarchy = {&quot;I&quot;}],
  22. [Column1 = &quot;L&quot;, Hierarchy = {&quot;I&quot;, &quot;K&quot;}],
  23. [Column1 = &quot;M&quot;, Hierarchy = {&quot;I&quot;, &quot;K&quot;}]
  24. }),
  25. #&quot;Unpivoted Other Columns&quot; = Table.UnpivotOtherColumns(source, {&quot;Name&quot;}, &quot;Attribute&quot;,&quot;Value&quot;),
  26. #&quot;Grouped Rows&quot; = Table.Group(#&quot;Unpivoted Other Columns&quot;, {&quot;Name&quot;}, {{&quot;data&quot;, each
  27. let a = Table.NestedJoin(_, {&quot;Attribute&quot;}, hierarchy, {&quot;Column1&quot;}, &quot;hierarchy&quot;, JoinKind.LeftOuter),
  28. b = Table.ExpandTableColumn(a,&quot;hierarchy&quot;, {&quot;Hierarchy&quot;}, {&quot;David&quot;}),
  29. c = Table.ExpandListColumn(b, &quot;David&quot;),
  30. d = Table.NestedJoin(c, {&quot;David&quot;}, c, {&quot;Attribute&quot;}, &quot;Ron&quot;, JoinKind.LeftOuter),
  31. e = Table.ExpandTableColumn(d, &quot;Ron&quot;, {&quot;Attribute&quot;}, {&quot;Peter&quot;}),
  32. f = Table.SelectRows(e, each ([David] &lt;&gt; null) and ([Peter] = null)),
  33. g= Table.Group(f, {&quot;Name&quot;,&quot;Attribute&quot;}, {{&quot;Concat&quot;, each Text.Combine([David],&quot;,&quot;), type text}}),
  34. h = Table.AddColumn(g, &quot;Custom&quot;, each [Attribute]&amp;&quot;-&gt;&quot;&amp;[Concat],type text),
  35. i= try Table.Group( h, {&quot;Name&quot;}, {{&quot;Concat&quot;, each Text.Combine([Custom],&quot;;&quot;), type text}}) {0}[Concat] otherwise null
  36. in i , type text }}),
  37. #&quot;Merged Queries&quot; = Table.NestedJoin(source, {&quot;Name&quot;},#&quot;Grouped Rows&quot;, {&quot;Name&quot;}, &quot;table2&quot;, JoinKind.LeftOuter),
  38. #&quot;Expanded table2&quot; = Table.ExpandTableColumn(#&quot;Merged Queries&quot;, &quot;table2&quot;, {&quot;data&quot;}, {&quot;Errors&quot;})
  39. in #&quot;Expanded table2&quot;

you could delete last two steps if you just want the error list

如何检查相应的父子列以标记为x?

答案2

得分: 0

我目前正在努力修复/实现提供的代码中的以下情景:

same hierarchy, but data sample with parent without childs

层次结构与上述帖子中的初始层次结构相同。
数据源已更改为:

  1. let
  2. source = Table.FromRecords({
  3. [Name=&quot;Jason&quot;, A=&quot;x&quot;, B=&quot;x&quot;, C=&quot;x&quot;, D=&quot;x&quot;, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=&quot;>x&lt;", I=&quot;x&quot;, J=null, K=null, L=null, M=null],
  4. [Name=&quot;Joe&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=null, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=&quot;x&quot;],
  5. [Name=&quot;Eddie&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=&quot;x&quot;, G=null, H=&quot;>x&lt;", I=null, J=&quot;x&quot;, K=null, L=null, M=null],
  6. [Name=&quot;Phil&quot;, A=null, B=null, C=null, D=&quot;x&quot;, E=null, F=null, G=null, H=null, I=&quot;x&quot;, J=null, K=&quot;x&quot;, L=&quot;x&quot;, M=null],
  7. [Name=&quot;Thomas&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=null, F=&quot;x&quot;, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=null],
  8. [Name=&quot;David&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  9. [Name=&quot;Matthew&quot;, A=null, B=&quot;x&quot;, C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K=&quot;x&quot;, L=null, M=null]
  10. })
  11. in
  12. source

预期的结果应该是对于以下情况也有一个错误文本:

  • 父节点标记为 x,
  • 根据层次结构,它有子节点,
  • 但没有任何子节点标记为 x。
    --> 至少预计其中一个子节点也应该标记为 x。

我的第一个想法是在#“Grouped Rows”=中添加附加步骤,但我预计我可能会引入不必要的步骤,因此可能效率不高。

f2 = Table.SelectRows(e, each ([David] = null) and ([Peter] = null)),

英文:

I am struggling currently to fix / implement the following scenario in the provided code:

same hierarchy, but data sample with parent without childs

hierarchy is the initial one from posts above.
data source is changed to:

  1. let
  2. source = Table.FromRecords({
  3. [Name=&quot;Jason&quot;, A=&quot;x&quot;, B=&quot;x&quot;, C=&quot;x&quot;, D=&quot;x&quot;, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=&quot;x&quot;, J=null, K=null, L=null, M=null],
  4. [Name=&quot;Joe&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=null, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=&quot;x&quot;],
  5. [Name=&quot;Eddie&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=null, J=&quot;x&quot;, K=null, L=null, M=null],
  6. [Name=&quot;Phil&quot;, A=null, B=null, C=null, D=&quot;x&quot;, E=null, F=null, G=null, H=null, I=&quot;x&quot;, J=null, K=&quot;x&quot;, L=&quot;x&quot;, M=null],
  7. [Name=&quot;Thomas&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=null, F=&quot;x&quot;, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=null],
  8. [Name=&quot;David&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  9. [Name=&quot;Matthew&quot;, A=null, B=&quot;x&quot;, C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K=&quot;x&quot;, L=null, M=null]
  10. })
  11. in
  12. source

The expected result would be to have also an error text for those case where

  • a parent is marked with x,
  • according to hierarchy it has childs,
  • but none of the childs are marked with x.
    --> at least one of the childs is expected to be marked as well.

My first idea was to add additional steps in the #"Grouped Rows" = but I expect somehow that I am gonne introduce to much steps which might not be needed and thus might be inefficient.

  1. f2 = Table.SelectRows(e, each ([David] = null) and ([Peter] = null)),

答案3

得分: 0

我已经能够使用示例数据和分层表修复了关于父级和子级方向的错误处理。

再次感谢 @horseyride 提供了关于父级方向的解决方案,我能够重用该结构来支持我的新方法。

以下是示例查询的代码部分:

  1. // data_stackoverflow
  2. let
  3. source = Table.FromRecords({
  4. [Name="Jason", A="x", B="x", C="x", D="x", E="x", F="x", G=null, H="x", I="x", J=null, K=null, L=null, M=null],
  5. [Name="Joe", A="x", B=null, C="x", D=null, E=null, F=null, G="x", H="x", I=null, J=null, K=null, L=null, M="x"],
  6. [Name="Eddie", A="x", B=null, C="x", D=null, E=null, F="x", G=null, H="x", I=null, J="x", K=null, L=null, M=null],
  7. [Name="Phil", A=null, B=null, C=null, D="x", E=null, F=null, G=null, H=null, I="x", J=null, K="x", L="x", M=null],
  8. [Name="Thomas", A="x", B=null, C=null, D=null, E=null, F="x", G="x", H="x", I=null, J=null, K=null, L=null, M=null],
  9. [Name="David", A="x", B=null, C=null, D=null, E="x", F="x", G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  10. [Name="Matthew", A=null, B="x", C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K="x", L=null, M=null]
  11. })
  12. in
  13. source
  14. // listHierarchyLevels_stackoverflow
  15. let
  16. source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8jFUitUBUkYQylgpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [col = _t]),
  17. col = source[col]
  18. in
  19. col
  20. // data_hierarchy_stackoverflow
  21. let
  22. source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc67CcAwDATQXVR7ifj/nUBoA+Mu+ycIBa5IIe54cCBmusjpnXvvL8QxeXOPGAwDYgSMKgkkqWSTjMMCWFQqSFVpb2s/73XzjjgMB+IEnCoLZJHIAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [header = _t, L1 = _t, L2 = _t, L3 = _t])
  23. in
  24. source
  25. // GetHierarchy_stackoverflow
  26. let
  27. source = data_hierarchy_stackoverflow,
  28. HierarchyLevels = listHierarchyLevels_stackoverflow,
  29. FILTER_HierarchyOnly = Table.SelectRows(source, each Record.Field(_, HierarchyLevels{0}) <> null and Record.Field(_, HierarchyLevels{0}) <> ""),
  30. AddCol_Parents = Table.AddColumn(FILTER_HierarchyOnly, "Parents", each List.RemoveItems(List.RemoveNulls(Record.ToList(Record.SelectFields(_, HierarchyLevels))), {_[header]})),
  31. AddCol_LevelNo = Table.AddColumn(AddCol_Parents, "LevelNo", each List.Count(_[Parents])+1),
  32. AddCol_Level = Table.AddColumn(AddCol_LevelNo, "Level", each HierarchyLevels{_[LevelNo]-1}),
  33. AddCol_Childs = Table.AddColumn(AddCol_Level, "Childs", (r1) =>
  34. Table.SelectRows(AddCol_Level, (r2) => (Record.Field(r2, r1[Level]) = r1[header]) and (r2[LevelNo] = r1[LevelNo]+1))[header])
  35. in
  36. AddCol_Childs
  37. // output_stackoverflow
  38. let
  39. source = data_stackoverflow,
  40. Unpivot = Table.UnpivotOtherColumns(source, {"Name"}, "Attribute","Value"),
  41. GroupedRows = Table.Group(Unpivot, {"Name"}, {{"data", each
  42. let source = _,
  43. JOIN_hierarchy = Table.NestedJoin(source, {"Attribute"}, GetHierarchy_stackoverflow, {"header"}, "hierarchy", JoinKind.LeftOuter),
  44. Expand_hierarchy = let cols = Table.ColumnNames(GetHierarchy_stackoverflow) in Table.ExpandTableColumn(JOIN_hierarchy, "hierarchy", cols, cols),
  45. AddCol_ErrorsParents = Table.AddColumn(Expand_hierarchy, "ErrorsParents", each let misList = List.RemoveItems(_[Parents], Expand_hierarchy[Attribute]) in if List.Count(misList) > 0 then [Attribute] & "->" & Text.Combine(misList,",") else null),
  46. AddCol_ErrorsChilds = Table.AddColumn(AddCol_ErrorsParents, "ErrorsChilds", each if List.Count(_[Childs]) = 0 or List.ContainsAny(AddCol_ErrorsParents[Attribute], _[Childs]) then null else [Attribute] & "->(" & Text.Combine(_[Childs],"|") & ")"),
  47. AddCol_Error = Table.AddColumn(AddCol_ErrorsChilds, "Errors", each let err = Text.Combine({[ErrorsParents], [ErrorsChilds]}, ",") in if err = "" then null else err),
  48. Group_ConcatErrors_ByName = Table.Group(AddCol_Error, {"Name"}, {"Concat", each Text.Combine([Errors],";"), type text}),
  49. drilldown = Group_ConcatErrors_ByName{0}[Concat]
  50. in
  51. drilldown, type text }}),
  52. MergedQueries = Table.NestedJoin(source, {"Name"}, GroupedRows, {"Name"}, "table2", JoinKind.LeftOuter),
  53. ExpandedTable = Table.ExpandTableColumn(MergedQueries, "table2", {"data"}, {"Errors"})
  54. in
  55. ExpandedTable

希望这对你有所帮助。如果有任何其他问题,请随时提出。

英文:

I was able to fix the error handling in the direction of parents as well as childs using sample data and an hierarchy table.

Thanks again @horseyride for your solution in the direction of parents, I was able to reuse the structure for my new approach.

following the sample queries:

  1. // data_stackoverflow
  2. let
  3. source = Table.FromRecords({
  4. [Name=&quot;Jason&quot;, A=&quot;x&quot;, B=&quot;x&quot;, C=&quot;x&quot;, D=&quot;x&quot;, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=&quot;x&quot;, J=null, K=null, L=null, M=null],
  5. [Name=&quot;Joe&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=null, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=&quot;x&quot;],
  6. [Name=&quot;Eddie&quot;, A=&quot;x&quot;, B=null, C=&quot;x&quot;, D=null, E=null, F=&quot;x&quot;, G=null, H=&quot;x&quot;, I=null, J=&quot;x&quot;, K=null, L=null, M=null],
  7. [Name=&quot;Phil&quot;, A=null, B=null, C=null, D=&quot;x&quot;, E=null, F=null, G=null, H=null, I=&quot;x&quot;, J=null, K=&quot;x&quot;, L=&quot;x&quot;, M=null],
  8. [Name=&quot;Thomas&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=null, F=&quot;x&quot;, G=&quot;x&quot;, H=&quot;x&quot;, I=null, J=null, K=null, L=null, M=null],
  9. [Name=&quot;David&quot;, A=&quot;x&quot;, B=null, C=null, D=null, E=&quot;x&quot;, F=&quot;x&quot;, G=null, H=null, I=null, J=null, K=null, L=null, M=null],
  10. [Name=&quot;Matthew&quot;, A=null, B=&quot;x&quot;, C=null, D=null, E=null, F=null, G=null, H=null, I=null, J=null, K=&quot;x&quot;, L=null, M=null]
  11. })
  12. in
  13. source
  14. // listHierarchyLevels_stackoverflow
  15. let
  16. source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(&quot;i45W8jFUitUBUkYQylgpNhYA&quot;, BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [col = _t]),
  17. col = source[col]
  18. in
  19. col
  20. // data_hierarchy_stackoverflow
  21. let
  22. source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(&quot;bc67CcAwDATQXVR7ifj/nUBoA+Mu+ycIBa5IIe54cCBmusjpnXvvL8QxeXOPGAwDYgSMKgkkqWSTjMMCWFQqSFVpb2s/73XzjjgMB+IEnCoLZJHIAw==&quot;, BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [header = _t, L1 = _t, L2 = _t, L3 = _t])
  23. in
  24. source
  25. // GetHierarchy_stackoverflow
  26. let
  27. source = data_hierarchy_stackoverflow,
  28. HierarchyLevels = listHierarchyLevels_stackoverflow,
  29. FILTER_HierarchyOnly = Table.SelectRows(source, each Record.Field(_, HierarchyLevels{0}) &lt;&gt; null and Record.Field(_, HierarchyLevels{0}) &lt;&gt; &quot;&quot;),
  30. AddCol_Parents = Table.AddColumn(FILTER_HierarchyOnly, &quot;Parents&quot;, each List.RemoveItems(List.RemoveNulls(Record.ToList(Record.SelectFields(_, HierarchyLevels))), {_[header]})),
  31. AddCol_LevelNo = Table.AddColumn(AddCol_Parents, &quot;LevelNo&quot;, each List.Count(_[Parents])+1),
  32. AddCol_Level = Table.AddColumn(AddCol_LevelNo, &quot;Level&quot;, each HierarchyLevels{_[LevelNo]-1}),
  33. AddCol_Childs = Table.AddColumn(AddCol_Level, &quot;Childs&quot;, (r1) =&gt;
  34. Table.SelectRows(AddCol_Level, (r2) =&gt; (Record.Field(r2, r1[Level]) = r1[header]) and (r2[LevelNo] = r1[LevelNo]+1))[header])
  35. in
  36. AddCol_Childs
  37. // output_stackoverflow
  38. let
  39. source = data_stackoverflow,
  40. Unpivot = Table.UnpivotOtherColumns(source, {&quot;Name&quot;}, &quot;Attribute&quot;,&quot;Value&quot;),
  41. GroupedRows = Table.Group(Unpivot, {&quot;Name&quot;}, {{&quot;data&quot;, each
  42. let source = _,
  43. JOIN_hierarchy = Table.NestedJoin(source, {&quot;Attribute&quot;}, GetHierarchy_stackoverflow, {&quot;header&quot;}, &quot;hierarchy&quot;, JoinKind.LeftOuter),
  44. Expand_hierarchy = let cols = Table.ColumnNames(GetHierarchy_stackoverflow) in Table.ExpandTableColumn(JOIN_hierarchy, &quot;hierarchy&quot;, cols, cols),
  45. AddCol_ErrorsParents = Table.AddColumn(Expand_hierarchy, &quot;ErrorsParents&quot;, each let misList = List.RemoveItems(_[Parents], Expand_hierarchy[Attribute]) in if List.Count(misList) &gt; 0 then [Attribute]&amp;&quot;-&gt;&quot;&amp;Text.Combine(misList,&quot;,&quot;) else null),
  46. AddCol_ErrorsChilds = Table.AddColumn(AddCol_ErrorsParents, &quot;ErrorsChilds&quot;, each if List.Count(_[Childs]) = 0 or List.ContainsAny(AddCol_ErrorsParents[Attribute], _[Childs]) then null else [Attribute]&amp;&quot;-&gt;(&quot;&amp;Text.Combine(_[Childs],&quot;|&quot;)&amp;&quot;)&quot;),
  47. AddCol_Error = Table.AddColumn(AddCol_ErrorsChilds, &quot;Errors&quot;, each let err = Text.Combine({[ErrorsParents], [ErrorsChilds]}, &quot;,&quot;) in if err = &quot;&quot; then null else err),
  48. Group_ConcatErrors_ByName = Table.Group(AddCol_Error, {&quot;Name&quot;}, {&quot;Concat&quot;, each Text.Combine([Errors],&quot;;&quot;), type text}),
  49. drilldown = Group_ConcatErrors_ByName{0}[Concat]
  50. in
  51. drilldown, type text }}),
  52. MergedQueries = Table.NestedJoin(source, {&quot;Name&quot;}, GroupedRows, {&quot;Name&quot;}, &quot;table2&quot;, JoinKind.LeftOuter),
  53. ExpandedTable = Table.ExpandTableColumn(MergedQueries, &quot;table2&quot;, {&quot;data&quot;}, {&quot;Errors&quot;})
  54. in
  55. ExpandedTable

答案4

得分: 0

以下是您要翻译的代码部分:

  1. Okay I tried to optimized a little further for readability but mainly having both directions checked
  2. - parents --&gt; all parents must be enabled
  3. - child --&gt; at least one child has to be enabled
  4. I tried to find out how to reduce the access to the source file which is either a 1000x15 table in external Excel or even on the same file as tab. Preview are working nice and smooth as long as I keep out of business allErrors = Text.Combine(onlyErrors[Errors], &quot;;&quot;).
  5. main code - check query
  1. // Check
  2. let
  3. source = cache,
  4. hierarchy = Table.Buffer(GetHierarchy),
  5. hierarchyHeader = hierarchy[header],
  6. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  7. ...
  1. full code
  1. // cache
  2. let
  3. Source = Excel.CurrentWorkbook(){[Name=&quot;tabCheck_preparation&quot;]}[Content]
  4. in
  5. Source
  6. // Check
  7. let
  8. source = cache,
  9. hierarchy = Table.Buffer(GetHierarchy),
  10. hierarchyHeader = hierarchy[header],
  11. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  12. ...
  1. // tabHeader
  2. let
  3. Quelle = Excel.CurrentWorkbook(){[Name=&quot;tabHeader&quot;]}[Content],
  4. FILTER_relevantOnly = Table.SelectRows(Quelle, each ([&#220;bernahme] = &quot;ja&quot;)),
  5. Replace_NullBy_Kopfzeile = Table.ReplaceValue(FILTER_relevantOnly,null, each [Kopfzeile], Replacer.ReplaceValue, {&quot;Kopfzeile_Renamed&quot;})
  6. in
  7. Replace_NullBy_Kopfzeile
  1. // GetHierarchy
  2. let
  3. Quelle = tabHeader,
  4. HierarchyLevels = listHierarchyLevels,
  5. FILTER_HierarchyLevels = Table.SelectRows(Quelle, each List.Contains(HierarchyLevels, [hierarchy])),
  6. KEEP_HierarchyColumns = Table.SelectColumns(FILTER_HierarchyLevels, List.Combine({{&quot;Kopfzeile_Renamed&quot;}, HierarchyLevels})),
  7. RenameCol_header = Table.RenameColumns(KEEP_HierarchyColumns,{{&quot;Kopfzeile_Renamed&quot;, &quot;header&quot;}}),
  8. Replace_EmptyBy_Null = Table.ReplaceValue(RenameCol_header,&quot;&quot;,null,Replacer.ReplaceValue, listHierarchyLevels),
  9. ...
  1. // listHierarchyLevels
  2. let
  3. Quelle = Excel.CurrentWorkbook(){[Name=&quot;tabHierarchyLevels&quot;]}[Content],
  4. FILTER_ja = Table.SelectRows(Quelle, each ([validate] = &quot;ja&quot;)),
  5. listHierarchies = FILTER_ja[listHierarchies]
  6. in
  7. listHierarchies

希望这有所帮助。

英文:

Okay I tried to optimized a little further for readability but mainly having both directions checked

  • parents --> all parents must be enabled
  • child --> at least one child has to be enabled

I tried to find out how to reduce the access to the source file which is either a 1000x15 table in external Excel or even on the same file as tab.
Preview are working nice and smooth as long as I keep out of business
allErrors = Text.Combine(onlyErrors[Errors], &quot;;&quot;).

main code - check query

  1. // Check
  2. let
  3. source = cache,
  4. hierarchy = Table.Buffer(GetHierarchy),
  5. hierarchyHeader = hierarchy[header],
  6. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  7. /*
  8. source = cache,
  9. hierarchy = GetHierarchy,
  10. hierarchyHeader = hierarchy[header],
  11. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  12. */
  13. AddCol_Error = Table.AddColumn(source, &quot;Errors&quot;, each bomFunction(_, hierarchy, hierarchyCols)),
  14. bomFunction = (bomRow, bHierarchy, bHierarchyCols) =&gt;
  15. let
  16. // for each row in table (BOM #)
  17. xTable = Table.SelectRows(Record.ToTable(bomRow), each [Value] = &quot;x&quot;),
  18. xTableKey = Table.AddKey(xTable, {&quot;Name&quot;}, true),
  19. JOIN_hierarchy = Table.NestedJoin(xTable, {&quot;Name&quot;}, bHierarchy, &quot;header&quot;, &quot;hierarchy&quot;, JoinKind.LeftOuter),
  20. Expand_hierarchy = Table.ExpandTableColumn(JOIN_hierarchy, &quot;hierarchy&quot;, bHierarchyCols, bHierarchyCols),
  21. xList = List.Buffer(Expand_hierarchy[Name]),
  22. AddCol_Errors = Table.AddColumn(Expand_hierarchy, &quot;Errors&quot;, each xFunction(_, xList)),
  23. onlyErrors = Table.SelectRows(AddCol_Errors, each [Errors] &lt;&gt; null and [Errors] &lt;&gt; &quot;&quot;),
  24. //Group_ConcatErrors_ByName = Table.Group(AddCol_Error, {colIndex}, {&quot;Concat&quot;, each Text.Combine([Errors],&quot;;&quot;), type text}),
  25. allErrors = Text.Combine(onlyErrors[Errors], &quot;;&quot;)
  26. in
  27. allErrors,
  28. xFunction = (x, xList) =&gt;
  29. let
  30. // for each x
  31. // check for parents
  32. misList = List.RemoveItems(x[Parents], xList),
  33. errorsParents = if List.Count(misList) = 0 then null
  34. else x[Name] &amp; &quot;-&gt;&quot; &amp; Text.Combine(misList,&quot;,&quot;),
  35. // check for childs
  36. errorsChilds = if List.Count(x[Childs]) = 0 or List.ContainsAny(xList, x[Childs]) then null
  37. else x[Name] &amp; &quot;-&gt;(&quot; &amp; Text.Combine(x[Childs],&quot;|&quot;) &amp; &quot;)&quot;,
  38. err = Text.Combine({errorsParents, errorsChilds})
  39. in
  40. err,
  41. result = AddCol_Error,
  42. /*ReorderColumns = Table.ReorderColumns(ExpandedTable,
  43. List.Combine({
  44. {&quot;Errors&quot;},
  45. List.RemoveItems(Table.ColumnNames(ExpandedTable), {&quot;Errors&quot;})}))*/
  46. ReorderColumns = Table.ReorderColumns(result, List.Combine({ {&quot;Errors&quot;, &quot;#&quot;}, hierarchyHeader}))
  47. in
  48. ReorderColumns

full code

  1. // cache
  2. let
  3. Source = Excel.CurrentWorkbook(){[Name=&quot;tabCheck_preparation&quot;]}[Content]
  4. in
  5. Source
  6. // Check
  7. let
  8. source = cache,
  9. hierarchy = Table.Buffer(GetHierarchy),
  10. hierarchyHeader = hierarchy[header],
  11. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  12. /*
  13. source = cache,
  14. hierarchy = GetHierarchy,
  15. hierarchyHeader = hierarchy[header],
  16. hierarchyCols = List.Buffer(Table.ColumnNames(hierarchy)),
  17. */
  18. AddCol_Error = Table.AddColumn(source, &quot;Errors&quot;, each bomFunction(_, hierarchy, hierarchyCols)),
  19. bomFunction = (bomRow, bHierarchy, bHierarchyCols) =&gt;
  20. let
  21. // for each row in table (BOM #)
  22. xTable = Table.SelectRows(Record.ToTable(bomRow), each [Value] = &quot;x&quot;),
  23. xTableKey = Table.AddKey(xTable, {&quot;Name&quot;}, true),
  24. JOIN_hierarchy = Table.NestedJoin(xTable, {&quot;Name&quot;}, bHierarchy, &quot;header&quot;, &quot;hierarchy&quot;, JoinKind.LeftOuter),
  25. Expand_hierarchy = Table.ExpandTableColumn(JOIN_hierarchy, &quot;hierarchy&quot;, bHierarchyCols, bHierarchyCols),
  26. xList = List.Buffer(Expand_hierarchy[Name]),
  27. AddCol_Errors = Table.AddColumn(Expand_hierarchy, &quot;Errors&quot;, each xFunction(_, xList)),
  28. onlyErrors = Table.SelectRows(AddCol_Errors, each [Errors] &lt;&gt; null and [Errors] &lt;&gt; &quot;&quot;),
  29. //Group_ConcatErrors_ByName = Table.Group(AddCol_Error, {colIndex}, {&quot;Concat&quot;, each Text.Combine([Errors],&quot;;&quot;), type text}),
  30. allErrors = Text.Combine(onlyErrors[Errors], &quot;;&quot;)
  31. in
  32. allErrors,
  33. xFunction = (x, xList) =&gt;
  34. let
  35. // for each x
  36. // check for parents
  37. misList = List.RemoveItems(x[Parents], xList),
  38. errorsParents = if List.Count(misList) = 0 then null
  39. else x[Name] &amp; &quot;-&gt;&quot; &amp; Text.Combine(misList,&quot;,&quot;),
  40. // check for childs
  41. errorsChilds = if List.Count(x[Childs]) = 0 or List.ContainsAny(xList, x[Childs]) then null
  42. else x[Name] &amp; &quot;-&gt;(&quot; &amp; Text.Combine(x[Childs],&quot;|&quot;) &amp; &quot;)&quot;,
  43. err = Text.Combine({errorsParents, errorsChilds})
  44. in
  45. err,
  46. result = AddCol_Error,
  47. /*ReorderColumns = Table.ReorderColumns(ExpandedTable,
  48. List.Combine({
  49. {&quot;Errors&quot;},
  50. List.RemoveItems(Table.ColumnNames(ExpandedTable), {&quot;Errors&quot;})}))*/
  51. ReorderColumns = Table.ReorderColumns(result, List.Combine({ {&quot;Errors&quot;, &quot;#&quot;}, hierarchyHeader}))
  52. in
  53. ReorderColumns
  54. // tabHeader
  55. let
  56. Quelle = Excel.CurrentWorkbook(){[Name=&quot;tabHeader&quot;]}[Content],
  57. FILTER_relevantOnly = Table.SelectRows(Quelle, each ([&#220;bernahme] = &quot;ja&quot;)),
  58. Replace_NullBy_Kopfzeile = Table.ReplaceValue(FILTER_relevantOnly,null, each [Kopfzeile], Replacer.ReplaceValue, {&quot;Kopfzeile_Renamed&quot;})
  59. in
  60. Replace_NullBy_Kopfzeile
  61. // GetHierarchy
  62. let
  63. Quelle = tabHeader,
  64. HierarchyLevels = listHierarchyLevels,
  65. FILTER_HierarchyLevels = Table.SelectRows(Quelle, each List.Contains(HierarchyLevels, [hierarchy])),
  66. KEEP_HierarchyColumns = Table.SelectColumns(FILTER_HierarchyLevels, List.Combine({{&quot;Kopfzeile_Renamed&quot;}, HierarchyLevels})),
  67. RenameCol_header = Table.RenameColumns(KEEP_HierarchyColumns,{{&quot;Kopfzeile_Renamed&quot;, &quot;header&quot;}}),
  68. Replace_EmptyBy_Null = Table.ReplaceValue(RenameCol_header,&quot;&quot;,null,Replacer.ReplaceValue, listHierarchyLevels),
  69. AddCol_Parents = Table.AddColumn(Replace_EmptyBy_Null, &quot;Parents&quot;, each List.RemoveItems(List.RemoveNulls(Record.ToList(Record.SelectFields(_, HierarchyLevels))), {_[header]})),
  70. AddCol_LevelNo = Table.AddColumn(AddCol_Parents, &quot;LevelNo&quot;, each List.Count(_[Parents])+1),
  71. AddCol_Level = Table.AddColumn(AddCol_LevelNo, &quot;Level&quot;, each HierarchyLevels{_[LevelNo]-1}),
  72. #&quot;Removed Errors&quot; = Table.RemoveRowsWithErrors(AddCol_Level),
  73. FILTER_maxLevel = Table.SelectRows(#&quot;Removed Errors&quot;, let maxLevel = List.Count(HierarchyLevels) in each ([LevelNo] &lt;= maxLevel)),
  74. AddCol_Childs = Table.AddColumn(FILTER_maxLevel, &quot;Childs&quot;, (r1) =&gt;
  75. Table.SelectRows(FILTER_maxLevel, (r2) =&gt; (Record.Field(r2, r1[Level]) = r1[header]) and (r2[LevelNo] = r1[LevelNo]+1))[header]),
  76. RemovedCols_Hierarchy = Table.RemoveColumns(AddCol_Childs, HierarchyLevels)
  77. in
  78. RemovedCols_Hierarchy
  79. // listHierarchyLevels
  80. let
  81. Quelle = Excel.CurrentWorkbook(){[Name=&quot;tabHierarchyLevels&quot;]}[Content],
  82. FILTER_ja = Table.SelectRows(Quelle, each ([validate] = &quot;ja&quot;)),
  83. listHierarchies = FILTER_ja[listHierarchies]
  84. in
  85. listHierarchies

huangapple
  • 本文由 发表于 2023年3月4日 08:48:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定