Forum Discussion

numpy's avatar
numpy
Frequent Visitor
7 years ago
Solved

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 calculat...
  • Anonymous's avatar
    Anonymous
    7 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.

    Unpivot columns (Power Query)

    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