Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ALZEnith
Frequent Visitor

Matrix - Adding values from same excel row

Hi,

 

I have a simple table (below).

Power BI Table.PNG

I need to add a column to the Matrix which substract the "Red" Type 24/20 for September 2022 with the "Red" Type 24/20 for June 2022. Ideally, it would look something like the below:

Power BI Result.PNG

 

How would you go about doing this in DAX? The issue I have is that I am trying to pick and combine/substract two values from the same column.

 

Any help would be appreciated.

 

Thanks

 

Alexis

1 ACCEPTED SOLUTION
v-tangjie-msft
Community Support
Community Support

Hi @ALZEnith ,

 

According to your description, here are my steps you can follow as a solution.

(1)Click "Transform data" to enter the power query editor, click "Advanced Editor", and paste the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc07CoBADATQu2wtaD6jsbXwALaynRaClej93ayCKFhNBuaRcQzDPIUicMVctiWlkxoAnlbBOyux9347lj3E4kXqixDniaqBvSdDL9Ktx/x5QyySt9bWKUVhza+5/4jAsoH6lhQkj4kn", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t, Date = _t, #"24/20" = _t, #"24/30" = _t, #"24/40" = _t, #"Food type" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Date", type date}, {"24/20", Int64.Type}, {"24/30", Int64.Type}, {"24/40", Int64.Type}, {"Food type", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Type", "Date", "Food type"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Column Name"}})
in
    #"Renamed Columns"

vtangjiemsft_0-1669363623457.png

(2)Click "Apply and Close" to create a new calculated column in Desktop.

 Column = FORMAT( [Date] ,"mmm") & " " & [Type]

(3)Then we can create a new table 2.

Table 2 = UNION( VALUES('Table'[Column]) ,{"Red Difference"})

(4)Then we can create a measure.

Measure = var _coloum = SELECTEDVALUE('Table 2'[Column])

var _Sept_red =SUMX(FILTER('Table' , MONTH('Table'[Date]) = 9 && 'Table'[Type]="Red") , [Value])

var _June_red =SUMX(FILTER('Table' , MONTH('Table'[Date]) = 6 && 'Table'[Type]="Red") , [Value])

return

IF(_coloum ="Red Difference" ,_Sept_red-_June_red ,CALCULATE(SUM('Table'[Value]), TREATAS(VALUES('Table 2'[Column]) , 'Table'[Column] )))

(5)Finally, a matrix visual is created, and the result is shown in the following image.

vtangjiemsft_1-1669363867353.png

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

View solution in original post

1 REPLY 1
v-tangjie-msft
Community Support
Community Support

Hi @ALZEnith ,

 

According to your description, here are my steps you can follow as a solution.

(1)Click "Transform data" to enter the power query editor, click "Advanced Editor", and paste the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc07CoBADATQu2wtaD6jsbXwALaynRaClej93ayCKFhNBuaRcQzDPIUicMVctiWlkxoAnlbBOyux9347lj3E4kXqixDniaqBvSdDL9Ktx/x5QyySt9bWKUVhza+5/4jAsoH6lhQkj4kn", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t, Date = _t, #"24/20" = _t, #"24/30" = _t, #"24/40" = _t, #"Food type" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Date", type date}, {"24/20", Int64.Type}, {"24/30", Int64.Type}, {"24/40", Int64.Type}, {"Food type", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Type", "Date", "Food type"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Column Name"}})
in
    #"Renamed Columns"

vtangjiemsft_0-1669363623457.png

(2)Click "Apply and Close" to create a new calculated column in Desktop.

 Column = FORMAT( [Date] ,"mmm") & " " & [Type]

(3)Then we can create a new table 2.

Table 2 = UNION( VALUES('Table'[Column]) ,{"Red Difference"})

(4)Then we can create a measure.

Measure = var _coloum = SELECTEDVALUE('Table 2'[Column])

var _Sept_red =SUMX(FILTER('Table' , MONTH('Table'[Date]) = 9 && 'Table'[Type]="Red") , [Value])

var _June_red =SUMX(FILTER('Table' , MONTH('Table'[Date]) = 6 && 'Table'[Type]="Red") , [Value])

return

IF(_coloum ="Red Difference" ,_Sept_red-_June_red ,CALCULATE(SUM('Table'[Value]), TREATAS(VALUES('Table 2'[Column]) , 'Table'[Column] )))

(5)Finally, a matrix visual is created, and the result is shown in the following image.

vtangjiemsft_1-1669363867353.png

Best Regards,

Neeko Tang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.