Forum Discussion
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 :
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.
- Anonymous7 years ago
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.
Then you can simply use measure and slicer to achieve your requirement.
Sample:
Transformed query table
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] ) )Regards,
Xiaoxin Sheng
3 Replies
- parry2kSuper User
- AnonymousNot 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.
Then you can simply use measure and slicer to achieve your requirement.
Sample:
Transformed query table
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] ) )Regards,
Xiaoxin Sheng
- numpyFrequent VisitorThanks alot. To be honest i didn't think of this. Very helpful.