Forum Discussion
Calculate % diff in a matrix
Hi Everyone,
Need help with some table (matrix visual)
now I have this visual:
| P1 | P2 | P3 | P4 | |
| Value | 2 | 1 | 6 | 3 |
I'm trying to add a row so the visual should look like this:
| P1 | P2 | P3 | P4 | |
| Value | 2 | 1 | 6 | 3 |
| Change in % | -50% |
|
For example P1 : (1-2)/2= -50%
the Data is taken from this one, while on visual I'm doing SUM on each Period and there is a slicer by Group and Name
| Group | Name | P1 | P2 | P3 | P4 |
| a | abc | 1 | 0.5 | ||
| a | fd | 1 | 0.5 |
Thanks a lot!
Anonymous , In power bi; as of, we do not have the row or column operations. So you have to create a diff .
Create a separate period table or Year period table
Have a column like this in that Period table
Period Rank = RANKX(all('Period'),'Period'[year period],,ASC,Dense)
Calculate measures like these and take diff
This Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Period'),'Period'[Period Rank]=max('Period'[Period Rank])))
Last Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Period'),'Period'[Period Rank]=max('Period'[Period Rank])-1))- Anonymous5 years ago
Hi Anonymous ,
You could transform the table by unpiovting , adding a Level column by Attribute column...
The transformed table looks like this:
Here is the whole M syntax:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUpMSgaShkBsoGcKJI3AvFgdiGxaCoqkCVhBbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Name = _t, P1 = _t, P2 = _t, P3 = _t, P4 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Name", type text}, {"P1", Int64.Type}, {"P2", type number}, {"P3", Int64.Type}, {"P4", Int64.Type}}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Group", "Name"}, "Attribute", "Value"), #"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Other Columns", "Attribute", "Attribute - Copy"), #"Replaced Value" = Table.ReplaceValue(#"Duplicated Column","P","",Replacer.ReplaceText,{"Attribute - Copy"}), #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute - Copy", Int64.Type}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute - Copy", "level"}}) in #"Renamed Columns"Then you could using the following formula to create a meaure :
Change in = VAR aft = CALCULATE ( SUM ( 'Table2'[Value] ), FILTER ( ALL ( 'Table2' ), 'Table2'[level] = MAX ( 'Table2'[level] ) + 1 ) ) RETURN DIVIDE ( aft - SUM ( Table2[Value] ), SUM ( Table2[Value] ) )Hope this is what you are expected:
Please take a look at the pbix file here.
Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
2 Replies
- amitchandakSuper User
Anonymous , In power bi; as of, we do not have the row or column operations. So you have to create a diff .
Create a separate period table or Year period table
Have a column like this in that Period table
Period Rank = RANKX(all('Period'),'Period'[year period],,ASC,Dense)
Calculate measures like these and take diff
This Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Period'),'Period'[Period Rank]=max('Period'[Period Rank])))
Last Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Period'),'Period'[Period Rank]=max('Period'[Period Rank])-1)) - AnonymousNot applicable
Hi Anonymous ,
You could transform the table by unpiovting , adding a Level column by Attribute column...
The transformed table looks like this:
Here is the whole M syntax:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUpMSgaShkBsoGcKJI3AvFgdiGxaCoqkCVhBbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Name = _t, P1 = _t, P2 = _t, P3 = _t, P4 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Name", type text}, {"P1", Int64.Type}, {"P2", type number}, {"P3", Int64.Type}, {"P4", Int64.Type}}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Group", "Name"}, "Attribute", "Value"), #"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Other Columns", "Attribute", "Attribute - Copy"), #"Replaced Value" = Table.ReplaceValue(#"Duplicated Column","P","",Replacer.ReplaceText,{"Attribute - Copy"}), #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute - Copy", Int64.Type}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute - Copy", "level"}}) in #"Renamed Columns"Then you could using the following formula to create a meaure :
Change in = VAR aft = CALCULATE ( SUM ( 'Table2'[Value] ), FILTER ( ALL ( 'Table2' ), 'Table2'[level] = MAX ( 'Table2'[level] ) + 1 ) ) RETURN DIVIDE ( aft - SUM ( Table2[Value] ), SUM ( Table2[Value] ) )Hope this is what you are expected:
Please take a look at the pbix file here.
Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.