【问题标题】:Sum of filtered values in PowerBIPowerBI 中过滤值的总和
【发布时间】:2020-06-30 10:26:08
【问题描述】:

在 Power BI 中,我有一个数据集,其中包含带有 ID 和链接编号的列。

看起来像这样:

[ID_01] [Number_01]  [ID_02] [Number_02]  [ID_03]  [Number_03]
    A        12         A         03         C          45
    A        43         B         12         B          64
    B        02         B         78         C          19 t
    C        14         A         51         B          23

我需要的是,对于每一行,获取每个 ID 的总和,如下所示:

[ID_01] [Number_01]  [ID_02] [Number_02]  [ID_03]  [Number_03] [SUM_A] [SUM_B] [SUM_C]
    A        12         A         03         C          45       15      00      45
    A        43         B         12         B          64       43      76      00
    B        02         B         78         C          19       00      80      19
    C        14         A         51         B          23       51      23      14

您对我如何实现它有任何想法吗?

非常感谢!

【问题讨论】:

    标签: powerbi dax powerquery


    【解决方案1】:

    这在 PowerQuery / M 中似乎可以正常工作,并且对于任意数量的列都是动态的,而不仅仅是三组 ID/Number

    我确定某处有这样的两行版本,但它可能会让你开始

     let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
     // set up merge table that can be merged into index
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"_"}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Attribute.1]), "Attribute.1", "Value"),
    #"Grouped Rows" = Table.Group(#"Pivoted Column", {"Index", "ID"}, {{"Total", each List.Sum([Number]), type number}}),
    SecondTable= Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[ID]), "ID", "Total", List.Sum),
    // merge the two table
    #"Merged Queries" = Table.NestedJoin(#"Added Index",{"Index"},SecondTable,{"Index"},"Table2",JoinKind.LeftOuter),
    ColumnsToExpand = List.RemoveFirstN(List.Distinct(List.Combine(List.Transform(Table.Column(#"Merged Queries", "Table2"), each if _ is table then Table.ColumnNames(_) else {}))),1),
    #"Expanded Part2" = Table.ExpandTableColumn(#"Merged Queries", "Table2",ColumnsToExpand ,ColumnsToExpand ),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Part2",{"Index"})
    in #"Removed Columns"
    

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多