Showing results for 
Search instead for 
Did you mean: 
New 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)




I loaded it into Power BI and trying to create a matrix table with 'Metric' as column and 'Spend' as value. So i should get some output (image below)




Now i need to add a third column in this matrix table named "Difference" that dynamically calculates the difference between the values in Target and Actuals columns. In this example it should be 0. 


How to create such column in matrix table in Power BI? 

Super User IV
Super User IV

Re: Dynamically calculating difference between two columns in a matrix table

I'm not sure that your data is in the best format for what you are trying to accomplish, but you could do something like this and add it to Value area:


Measure = CALCULATE(SUM(Difference[Spend]),Difference[Metric]="Target") - CALCULATE(SUM(Difference[Spend]),Difference[Metric]="Actual") 


Putting square pegs in round holes since 1972.

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

MarcelBeug Community Champion
Community Champion

Re: Dynamically calculating difference between two columns in a matrix table

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):


    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)
    #"Inserted Subtraction"


Specializing in Power Query Formula Language (M)

Helpful resources

‘Better Together’ T-Shirt Contest – Winner Announced!

‘Better Together’ T-Shirt Contest – Winner Announced!

And the winner is...

Announcing the New Spanish Forum

Announcing the New Spanish Forum

Do you need help in Spanish? Check out our new Spanish community section.

April 2020 Community Highlights

April 2020 Community Highlights

Info on our Super Users, MBAS content and badges, and updates to our support articles. - Read the full Community Highlights.

MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

Top Solution Authors
Top Kudoed Authors