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
numpy
Frequent Visitor

Dynamic Measure Calculation Power BI DAX

Hi All,

 

I am like just getting started with power bi and am real newbie to DAX.

 

While I was exploring Power BI I came across a question/problem. Whether the measures can have dynamic calculations,

 

Problem :

 

errortest.PNG 

 

 

 

 

 

Above is made up data for explaining the problem.

 

I want a measure to dynaically calculate % with respect to selection made in slicer.

 

Slicer 1: Category selection

Slicer 2 : Orange , Red , blue , green(mange to get it into slicer by doing some secondary table)

 

Calculation needed:

 

If Orange selected in slicer 2 measure should be Sum(Orange)/Sum(Total Orange Action)

 

i.e if slicer 1 has clothes and food selected : (1+1)/ (10+12) = 0.0909

 

If Orange and Some other value(for example taking red) is selected measure should be sumthing like :

 

Sumx(Orange + Red)/ sumx(total orange action + total red action) 

 

i.e if slicer 1 has clothes and food selected : ((1+23)+(1+43))/ ((10+200)+(12+250))= 68/472 = 0.144

 

And so on if three value selected:

 

Sumx(all three value) / sumx(total three values action)

 

I am not sure whether this is possible or not and would appretiate any sort of help. I want to use this in a line chart where the user can select the measures and see the percentage against time.

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

HI @numpy,

 

I'd like to suggest you do 'unpivot columns' on query editor site to convert your table to category, color, value, action color, action value.

Unpivot columns (Power Query)

Then you can simply use measure and slicer to achieve your requirement.

 

Sample:

 

Transformed query table

19.PNG

 

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY69CoAwDITfJXOH/kVxFtzc3IqDoKBQKKj4/PbSdshxbb5cEgKNMb3n8ZAik8u6LB4GYjSc1s2yplUFmlLaK+/BS+FlLHgWnrN0hV/u7TsiWgAdktFl2zgvK1xZWWbm63lrNOO/R9wg51RKzqkD6w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t, orange = _t, red = _t, blue = _t, green = _t, #"Total Orange action" = _t, #"Total red action" = _t, #"Total blue action" = _t, #"Total green action" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"orange", Int64.Type}, {"red", Int64.Type}, {"blue", Int64.Type}, {"green", Int64.Type}, {"Total Orange action", Int64.Type}, {"Total red action", Int64.Type}, {"Total blue action", Int64.Type}, {"Total green action", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Category", "Total Orange action", "Total red action", "Total blue action", "Total green action"}, "Color", "Value"),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Category", "Color", "Value"}, "Attribute2", "Action"),
    #"Replaced Value"  = Table.ReplaceValue(Table.ReplaceValue(#"Unpivoted Columns1","Total ","",Replacer.ReplaceText,{"Attribute2"})," action","",Replacer.ReplaceText,{"Attribute2"}),
    #"Filtered Rows" = Table.SelectRows(#"Replaced Value", each [Color] = Text.Lower([Attribute2])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute2"})
in
    #"Removed Columns"

Measure formula:

Percent = 
CALCULATE (
    SUM ( 'Sample'[Value] ),
    VALUES ( 'Sample'[Category] ),
    VALUES ( 'Sample'[Color] )
)
    / CALCULATE (
        SUM ( 'Sample'[Action] ),
        VALUES ( 'Sample'[Category] ),
        VALUES ( 'Sample'[Color] )
    )

20.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
v-shex-msft
Community Support
Community Support

HI @numpy,

 

I'd like to suggest you do 'unpivot columns' on query editor site to convert your table to category, color, value, action color, action value.

Unpivot columns (Power Query)

Then you can simply use measure and slicer to achieve your requirement.

 

Sample:

 

Transformed query table

19.PNG

 

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY69CoAwDITfJXOH/kVxFtzc3IqDoKBQKKj4/PbSdshxbb5cEgKNMb3n8ZAik8u6LB4GYjSc1s2yplUFmlLaK+/BS+FlLHgWnrN0hV/u7TsiWgAdktFl2zgvK1xZWWbm63lrNOO/R9wg51RKzqkD6w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t, orange = _t, red = _t, blue = _t, green = _t, #"Total Orange action" = _t, #"Total red action" = _t, #"Total blue action" = _t, #"Total green action" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"orange", Int64.Type}, {"red", Int64.Type}, {"blue", Int64.Type}, {"green", Int64.Type}, {"Total Orange action", Int64.Type}, {"Total red action", Int64.Type}, {"Total blue action", Int64.Type}, {"Total green action", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Category", "Total Orange action", "Total red action", "Total blue action", "Total green action"}, "Color", "Value"),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Category", "Color", "Value"}, "Attribute2", "Action"),
    #"Replaced Value"  = Table.ReplaceValue(Table.ReplaceValue(#"Unpivoted Columns1","Total ","",Replacer.ReplaceText,{"Attribute2"})," action","",Replacer.ReplaceText,{"Attribute2"}),
    #"Filtered Rows" = Table.SelectRows(#"Replaced Value", each [Color] = Text.Lower([Attribute2])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute2"})
in
    #"Removed Columns"

Measure formula:

Percent = 
CALCULATE (
    SUM ( 'Sample'[Value] ),
    VALUES ( 'Sample'[Category] ),
    VALUES ( 'Sample'[Color] )
)
    / CALCULATE (
        SUM ( 'Sample'[Action] ),
        VALUES ( 'Sample'[Category] ),
        VALUES ( 'Sample'[Color] )
    )

20.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Thanks alot. To be honest i didn't think of this. Very helpful.
parry2k
Super User
Super User

@numpy



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.