简体   繁体   English

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

[英]Power Query Excel show values as percentage of column

Normally, PivotTables are used to present data in a certain order.通常,数据透视表用于按特定顺序显示数据。 For this specific "issue" I need to present numbers and a target.对于这个特定的“问题”,我需要提供数字和目标。 This conflicts in Pivots as the percentage of column takes into account the target as well.这与 Pivots 冲突,因为列的百分比也考虑了目标。

No worries, with PowerPivot it should be possible to address this issue.不用担心,使用 PowerPivot 应该可以解决这个问题。 So far I have been able to create a table with the following layout:到目前为止,我已经能够创建一个具有以下布局的表格:

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

Where 1, 2 and 3 are the first three days of a month (for sake of simplicity rest is left out).其中 1、2 和 3 是一个月的前三天(为简单起见,省略了其余部分)。

I can get totals of the columns as follows:我可以得到列的总数如下:

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

Furthermore I am able to divide each value of a column by a number:此外,我可以将列的每个值除以一个数字:

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

Now I would like to replace 42 with the totals as calculated previously for the columns columns.现在我想用之前为列计算的总数替换 42。 Note that "{"1","2","3"}" will be calculated automatically in another step.请注意,"{"1","2","3"}" 将在另一个步骤中自动计算。

Can someone elaborate how to achieve this?有人可以详细说明如何实现这一目标吗? Expected result:预期结果:

         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

I couldn't come up with a way to simply replace 42 in your code, but since I saw this was matrix math, I based this below solution to your problem on Gil Raviv's blog on matrix multiplication . 我想不出一种简单的方法来替换您的代码中的42,但是由于我看到这是矩阵数学,因此我在Gil Raviv的有关矩阵乘法的博客上以下面的解决方案为基础。

I started with this as Table1: 我从表1开始:

在此处输入图片说明

Then, to get to this... 然后,要达到这个目的...

在此处输入图片说明

...I used Table1 as the source for a new query, and this code: ...我将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"

You'll notice I kinda "jumped around" a bit, using previous Applied Steps (which are basically query table states) as "tables" in the code...like in these Applied Steps: 您会发现我有点“跳来跳去”,使用以前的应用步骤(基本上是查询表状态)作为代码中的“表” ...就像这些应用步骤中一样:

  • Index MatrixB ( IndexedMatrixB = Table.AddIndexColumn(MatrixB, "Index", 1, 1), where MatrixB is a previous Applied Step); 索引MatrixB( IndexedMatrixB = Table.AddIndexColumn(MatrixB, "Index", 1, 1),其中MatrixB是先前的应用步骤); and

  • Merged Queries ( #"Merged Queries" = Table.NestedJoin(RenamedColumnsMatrixA,{"Column"},RenamedColumnsMatrixB,"Column"},"RenamedColumnsMatrixB",JoinKind.LeftOuter), where RenamedColumnsMatrixA and RenamedColumnsMatrixB are previous Applied Steps). 合并查询( #"Merged Queries" = Table.NestedJoin(RenamedColumnsMatrixA,{"Column"},RenamedColumnsMatrixB,"Column"},"RenamedColumnsMatrixB",JoinKind.LeftOuter),其中RenamedColumnsMatrixARenamedColumnsMatrixB是先前应用的步骤)。

@Marc Pince; @马克·皮斯; thanks for your suggestions. 感谢您的建议。 After some struggeling I was capable to cook the following: 经过一番挣扎之后,我可以做以下事情:

First, I swapped the total sum calculation line as follows: 首先,我交换总和计算行如下:

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

Where daysOfMonth are the days of that actual month, obtained elsewhere. 其中daysOfMonth是该实际月份的天,在其他地方获得。

Next I calculate the percentage as follows: 接下来,我计算百分比如下:

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

Here I use the fact that the index of the sum is based on the day of month minus 1 (starting from zero) providing solid results. 在这里,我使用了一个事实,即总和的索引基于月份的天减去1(从零开始),从而提供可靠的结果。

Thanks for all inputs. 感谢您的所有投入。

In this case, you could access the columns name using the construction: Table.Column(TableName, ColumnName).在这种情况下,您可以使用以下结构访问列名称:Table.Column(TableName, ColumnName)。

In your example it would be: Table.Column(prevStep, _ ), where " _ " access each column.在您的示例中,它将是: Table.Column(prevStep, _ ),其中“ _”访问每一列。

So, your code should be this:所以,你的代码应该是这样的:

= 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 / Table.Column(prevStep, _), type number}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM