【问题标题】:Replicate Excel formula logic in power query在电源查询中复制 Excel 公式逻辑
【发布时间】:2020-07-29 05:43:47
【问题描述】:

如果这里不属于这里,请告诉我!或者如果需要更多信息

在此处提供示例数据文件的链接 - https://www.dropbox.com/s/mz3f24ciby7qthv/example%20PBI%20Community.xlsx?dl=0

上下文:第 18 到 25 行包含国家客户分组粒度的 FORECAST 数据...我们需要分配给更细粒度的 Label1 和 Label2 第 2 到 17 行包含 2020 年的数据,具有我们需要的粒度,因此我们使用 2020 年的数据为 2021 年的数据创建一个分配基础,这是通过 I 列和 J 列中的公式完成的

公式/逻辑解释:

  • 这是“数据库”格式。即使添加了新列,它也需要保持这种状态
  • FormulaPart1 是“基础图”数据的索引,基于 N 列中的索引键
  • FormulaPart2 是基于列 O 中的索引键的“基本图”数据的 SUMIF
  • FormulaPart3 是“待分配”数据的索引,基于 P 列中的索引键

希望这一切都清楚...我想将此逻辑移至 PQ 以提高效率和最小化错误 所以任何正确方向的指导都会非常有用:) 我使用的真实数据集要大得多,并且在 excel 表中包含所有这些公式和索引键列是有问题的 :)

感谢您的指导! :)

【问题讨论】:

    标签: excel excel-formula dax powerquery


    【解决方案1】:

    假设范围 A1:H25 被命名为 Table1,这似乎是你想要的

    //assumes range A1:H25 from sample data is loaded as named range Table1
    let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Label1", Int64.Type}, {"Label2", type text}, {"Type", type text}, {"Country", type text}, {"Customer", type text}, {"Grouping", type text}, {"Original Value", Int64.Type}}),
    
    // add index for re-sorting later
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    
    // pull Base Figure rows This starts us with Part 1 of formula
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "Base Figure")),
    
    // formula part 2
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Country", "Customer", "Grouping"}, {{"Original Value 2", each List.Sum([Original Value]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Filtered Rows",{"Country", "Customer", "Grouping"},#"Grouped Rows" ,{"Country", "Customer", "Grouping"},"Table2",JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Original Value 2"}, {"Original Value 2"}),
    
    // formula part 3
    #"Filtered Rows2" = Table.SelectRows(#"Added Index", each ([Type] = "To be Allocated")),
    #"Grouped Rows2" = Table.Group(#"Filtered Rows2", {"Country", "Customer", "Grouping"}, {{"Original Value", each List.Sum([Original Value]), type number}}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded Table2",{"Country", "Customer", "Grouping"},#"Grouped Rows2",{"Country", "Customer", "Grouping"},"Table2",JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table2", {"Original Value"}, {"Original Value 3"}),
    
    // Add math for new column based on Formula / Formula2 * Formula3
    #"Added Custom" = Table.AddColumn(#"Expanded Table1", "Custom", each [Original Value]/[Original Value 2]*[Original Value 3]),
    
    // Replace year with following year, replace Base Figure with Allocated Figure, re-sort on original sort
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom", #"Filtered Rows"{0}[Year], #"Filtered Rows2"{0}[Year],Replacer.ReplaceValue,{"Year"}),
    #"BlankOriginalValue" = Table.TransformColumns(#"Replaced Value",{{"Original Value", each null}}),
    #"Replaced Value1" = Table.ReplaceValue(#"BlankOriginalValue","Base Figure","Allocated Figure",Replacer.ReplaceText,{"Type"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replaced Value1",{"Original Value 2", "Original Value 3"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Index", Order.Ascending}}),
    
    //combine new Allocated Figure data with original range and remove extra columns
    #"Combine" = Table.Combine({#"Added Index" , #"Sorted Rows" }),
    #"Added Custom1" = Table.AddColumn(Combine, "Forumla", each if [Custom]=null then [Original Value] else [Custom]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Index"})
    
    in #"Removed Columns1"
    

    第二个版本应该适用于基准年之外的多个待分配年份。注释省略。解释见第一版

    let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Label1", Int64.Type}, {"Label2", type text}, {"Type", type text}, {"Country", type text}, {"Customer", type text}, {"Grouping", type text}, {"Original Value", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "Base Figure")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Country", "Customer", "Grouping"}, {{"Original Value 2", each List.Sum([Original Value]), type number}}),
    #"Filtered Rows2" = Table.SelectRows(#"Added Index", each ([Type] = "To be Allocated")),
    #"Merged Queries3" = Table.NestedJoin(#"Filtered Rows2",{"Country", "Customer", "Grouping"},#"Filtered Rows" ,{"Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
    #"Removed Columns1" = Table.RemoveColumns(#"Merged Queries3",{"Label1", "Label2"}),
    #"Expanded Table3" = Table.ExpandTableColumn(#"Removed Columns1", "Table3", {"Label1", "Label2", "Original Value"}, {"Label1", "Label2", "Formula1"}),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Table3" ,{"Country", "Customer", "Grouping"},#"Grouped Rows" ,{"Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table3", {"Original Value 2"}, {"Formula2"}),
    #"Grouped Rows1" = Table.Group(#"Filtered Rows2" , {"Year", "Country", "Customer", "Grouping"}, {{"Part3", each List.Sum([Original Value]), type number}}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded Table2",{"Year", "Country", "Customer", "Grouping"},#"Grouped Rows1",{"Year", "Country", "Customer", "Grouping"},"Table3",JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table3", {"Part3"}, {"Formula3"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table1", "New", each [Formula1]/[Formula2]*[Formula3]),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}, {"Label1", Order.Ascending}}),
    #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows","To be Allocated","Allocated Figure",Replacer.ReplaceText,{"Type"}),
    #"BlankOriginalValue" = Table.TransformColumns(#"Replaced Value",{{"Original Value", each null}}),
    #"Removed Columns" = Table.RemoveColumns(BlankOriginalValue,{"Index", "Formula2", "Formula3", "Formula1"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Year", "Label1", "Label2", "Type", "Country", "Customer", "Grouping", "Original Value", "New"}),
    #"Combine" = Table.Combine({#"Changed Type",#"Reordered Columns"  })
    in #"Combine"
    

    【讨论】:

    • 嘿! :) 所以这花了我很长时间来处理和理解。但它有效。谢谢你!有没有一种有效的方法来编辑它以包括 2022 年和 2023 年(仍然是 2020 年的基准年)而不编写相同 M 代码的 3 个版本?为简单起见,我假设 2022 年和 2023 年的“待分配”数字与 2021 年相同
    • 添加了适用于多个分配年的第二个版本。这超出了为 Bounty 发布的问题,请接受
    • 接受:)。现在通过第二个版本。如有任何问题会发表评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多