Forum Discussion
GaRaGe
9 years agoNew Member
Dynamically calculating difference between two columns in a matrix table
For illustration purpose, i am using excel but the solution i need is for Power BI. Also, this is a small sample used as an example, my real data is quite huge. I have 3 columns (image below) ...
MarcelBeug
9 years agoCommunity Champion
Alternatively, in the query editor, you can remove "Region", pivot on Metric, rename "Actual" to "Actuals", go to "Add Column", select both columns and choose option Standard - Subtract. Adjust the generated code to have the new column named "Difference".
Code generated (the first line is specific for me entering data):
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcizOTFTSUQpJLEpPLQEyDA0MlGJ14OKOySWliTlAhgVUODe1KDMZVYcRuhRck6EJUCoWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Region = _t, Metric = _t, Spend = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Region", type text}, {"Metric", type text}, {"Spend", type number}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Region"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Metric]), "Metric", "Spend", List.Sum),
#"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Actual", "Actuals"}}),
#"Inserted Subtraction" = Table.AddColumn(#"Renamed Columns", "Difference", each [Target] - [Actuals], type number)
in
#"Inserted Subtraction"