Power Query Excel将值显示为列的百分比

时间:2017-11-01 13:29:58

标签: excel percentage powerquery subtotal

通常,数据透视表用于按特定顺序显示数据。对于这个具体的问题"我需要提供数字和目标。 这在Pivots中存在冲突,因为列的百分比也考虑了目标。

不用担心,使用PowerPivot应该可以解决这个问题。到目前为止,我已经能够创建一个具有以下布局的表:

         1     2    3
cat A    5    10    7
cat B    10    8    9
cat C    0     2    1

其中1,2和3是一个月的前三天(为简单起见,剩下的就是遗漏)。

我可以按如下方式获得列的总数:

= Table.FromRows({List.Transform({"1","2","3"}, each List.Sum(Table.Column(prevStep, _)))}, {"1","2","3"})

此外,我能够将一个列的每个值除以一个数字:

= Table.TransformColumns(prevStep, List.Transform({"1","2","3"}, each {_, (this) =>  this / 42, type number}))

现在我想用之前为列列计算的总数替换42。请注意" {" 1"," 2"," 3"}"将在另一步骤中自动计算。

有人可以详细说明如何实现这一目标吗?预期结果:

         1     2    3
cat A    0.33  0.5    0.41
cat B    0.67  0.4    0.53
cat C    0     0.1    0.06

2 个答案:

答案 0 :(得分:0)

我无法想出一种简单地在代码中替换42的方法,但是因为我看到这是矩阵数学,所以我将此解决方案基于Gil Raviv's blog on matrix multiplication上的问题。

我从表1开始:

enter image description here

然后,为了达到这个目标......

enter image description here

...我使用Table1作为新查询的源代码,此代码:

let
    Source = Table1,
    #"Removed Columns" = Table.RemoveColumns(Source,{""}),
    MatrixA = Table.TransformColumnTypes(#"Removed Columns",{{"1", type number}, {"2", type number}, {"3", type number}}),
    Summed = Table.FromRows({List.Transform({"1","2","3"}, each List.Sum(Table.Column(MatrixA, _)))}, {"1","2","3"}),
    MatrixB = Table.TransformColumnTypes(Summed,{{"1", type number}, {"2", type number}, {"3", type number}}),
    IndexedMatrixA = Table.AddIndexColumn(MatrixA, "Index", 1, 1),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(IndexedMatrixA, {"Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", Int64.Type}}),
    RenamedColumnsMatrixA = Table.RenameColumns(#"Changed Type",{{"Index", "Row"}, {"Attribute", "Column"}}),
    IndexedMatrixB = Table.AddIndexColumn(MatrixB, "Index", 1, 1),
    #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(IndexedMatrixB, {"Index"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns1",{{"Attribute", Int64.Type}}),
    RenamedColumnsMatrixB = Table.RenameColumns(#"Changed Type1",{{"Index", "Row"}, {"Attribute", "Column"}}),
    #"Merged Queries" = Table.NestedJoin(RenamedColumnsMatrixA,{"Column"},RenamedColumnsMatrixB,{"Column"},"RenamedColumnsMatrixB",JoinKind.LeftOuter),
    #"Expanded RenamedColumnsMatrixB" = Table.ExpandTableColumn(#"Merged Queries", "RenamedColumnsMatrixB", {"Row", "Column", "Value"}, {"B.Row", "B.Column", "B.Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded RenamedColumnsMatrixB", "AB", each [Value]/[B.Value]),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Row", "B.Column"}, {{"AB", each List.Sum([AB]), type number}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Grouped Rows", {{"B.Column", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Grouped Rows", {{"B.Column", type text}}, "en-US")[B.Column]), "B.Column", "AB"),
    #"Added Index" = Table.AddIndexColumn(#"Pivoted Column", "Index", 1, 1),
    IndexedSource = Table.AddIndexColumn(Source, "Index", 1, 1),
    #"Merged Queries1" = Table.NestedJoin(#"Added Index",{"Index"},IndexedSource,{"Index"},"IndexedSource",JoinKind.LeftOuter),
    #"Expanded IndexedSource" = Table.ExpandTableColumn(#"Merged Queries1", "IndexedSource", {""}, {"Column1"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded IndexedSource",{"Row", "Index"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Column1", "1", "2", "3"})
in
    #"Reordered Columns"

你会注意到我有点跳过"使用先前的应用步骤(基本上是查询表状态)作为"表"在代码中......就像在这些应用步骤中一样:

  • 索引MatrixB(IndexedMatrixB = Table.AddIndexColumn(MatrixB, "Index", 1, 1),其中MatrixB是之前的应用步骤);和

  • 合并查询(#"Merged Queries" = Table.NestedJoin(RenamedColumnsMatrixA,{"Column"},RenamedColumnsMatrixB,"Column"},"RenamedColumnsMatrixB",JoinKind.LeftOuter),其中RenamedColumnsMatrixARenamedColumnsMatrixB以前是应用步骤。

答案 1 :(得分:0)

@Marc Pince;谢谢你的建议。经过一番努力,我有能力做下面的事情:

首先,我按如下方式交换总和计算行:

totMinDay =  Table.ToRows    (Table.FromRows({List.Transform(daysOfMonth , each List.Sum(Table.Column(prevStep, _)))}, daysOfMonth )){0},

其中daysOfMonth是实际月份的天数,在其他地方获得。

接下来,我按如下方式计算百分比:

perc= Table.TransformColumns(prevStep, List.Transform(daysOfMonth , each {_, (this) =>  Number.Round(this/ ( totMinDay {Number.FromText( _ ) - 1} ), 3), type number})),

这里我使用的事实是总和的索引是基于月中的日期减去1(从零开始),提供可靠的结果。

感谢所有投入。