Reputation: 153
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.
No worries, with PowerPivot it should be possible to address this issue. 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).
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. Note that "{"1","2","3"}" will be calculated automatically in another step.
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
Upvotes: 1
Views: 2347
Reputation: 1
In this case, you could access the columns name using the construction: Table.Column(TableName, ColumnName).
In your example it would be: Table.Column(prevStep, _ ), where " _ " access each column.
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}))
Upvotes: 0
Reputation: 153
@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.
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.
Thanks for all inputs.
Upvotes: 0
Reputation: 5202
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.
I started with this as Table1:
Then, to get to this...
...I used Table1 as the source for a new query, and this code:
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); and
Merged Queries (#"Merged Queries" = Table.NestedJoin(RenamedColumnsMatrixA,{"Column"},RenamedColumnsMatrixB,"Column"},"RenamedColumnsMatrixB",JoinKind.LeftOuter),
where RenamedColumnsMatrixA
and RenamedColumnsMatrixB
are previous Applied Steps).
Upvotes: 0