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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

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
Anonymous
Not applicable

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

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

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

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
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors