Forum Discussion

satubuku83's avatar
satubuku83
Frequent Visitor
8 years ago
Solved

Calculate % Changes based on group

Hi

 

Assuming I have this table.

 

MetricGL CodeAmount
Premium10010000
Premium2005000
Premium300400
Reinsurance4005000
Reinsurance500

5000

 

And I would like to build a new measure that keeps track of the % change (different formulae depending on the metric) as below. There will be cases new metric is added and % change(for checking) for that metric will be anoter division over group.

 

For the case below, no checking is needed for premium, but a % checking for reinsurance (total reinsurance/total premium)

 

 Total Amount% Change (for checking)
Premium15400-
Reinsurance1000065%

 

 

Would appreciate if there is some light shed in this? Not too sure how filters would be able to help in this.

 

Regards

CS

  • Anonymous's avatar
    Anonymous
    8 years ago

    HI satubuku83,

     

    Actually, your requirement not very suitable for power bi. I think it should be more simply to achieve in excel pivot table.

    I also shared power bi version below:

     

    1. Grouping and transform table.

     

    2. Write measure formulas:

    Condition Total = 
    VAR categoryLevel =
        CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
            - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" )
    VAR premium =
        CALCULATE (
            CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
                - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" ),
            GL[Categroy] = "Premium"
        )
    VAR claim =
        CALCULATE (
            CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
                - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" ),
            GL[Categroy] = "Claim"
        )
    RETURN
        IF (
            ISFILTERED ( GL[Categroy] ),
            IF (
                ISFILTERED ( GL[SubCategory] ),
                SUM ( GL[Amount] ),
                CALCULATE ( categoryLevel, VALUES ( GL[Categroy] ) )
            ),
            premium - claim
        )
    
    
    Changes = 
    IF (
        ISFILTERED ( GL[SubCategory] ),
        SWITCH (
            SELECTEDVALUE ( GL[Categroy] ),
            "Premium", IF (
                SELECTEDVALUE ( GL[SubCategory] ) <> "In Stock",
                CALCULATE (
                    SUM ( GL[Amount] ),
                    VALUES ( GL[Categroy] ),
                    VALUES ( GL[SubCategory] )
                )
                    / CALCULATE (
                        SUM ( GL[Amount] ),
                        VALUES ( GL[Categroy] ),
                        GL[SubCategory] = "In Stock"
                    )
            ),
            "Claim", IF (
                SELECTEDVALUE ( GL[SubCategory] ) <> "In Stock",
                CALCULATE (
                    SUM ( GL[Amount] ),
                    VALUES ( GL[Categroy] ),
                    VALUES ( GL[SubCategory] )
                )
                    / CALCULATE (
                        SUM ( GL[Amount] ),
                        GL[Categroy] = "Premium",
                        VALUES ( GL[SubCategory] )
                    )
            )
        ),
        IF (
            SELECTEDVALUE ( GL[Categroy] ) = "Claim",
            DIVIDE (
                CALCULATE ( [Condition Total], GL[Categroy] = "Claim" ),
                CALCULATE ( [Condition Total], GL[Categroy] = "Premium" ),
                -1
            )
        )
    )
    

     

     

    3. Create matrix visual.

     

    Full query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCihKzc0szVXSUTI0MICQQDpWB1nGCCxjiilhDJYwQRVXCErNzCsuLUrMS05V8MwrTyxKgSrCNANFrX9pCVSxKVixGUyxc05iZq6CS2ZRanIJRBzqXJySRiiSWB1kDlZojFMhwjUWCF/GAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Metric = _t, #"GL Code" = _t, Amount = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Metric", type text}, {"GL Code", Int64.Type}, {"Amount", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Categroy", each Text.Split([Metric]," "){0}),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"Categroy"}, {{"Content", each Table.AddColumn(_, "SubCategory", each Text.AfterDelimiter([Metric],"Reinsurance ")), type table}}),
        #"Expanded Content" = Table.ExpandTableColumn(#"Grouped Rows", "Content", {"GL Code", "Amount", "SubCategory"}, {"GL Code", "Amount", "SubCategory"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Expanded Content",{"Categroy", "SubCategory", "GL Code", "Amount"}),
        #"Replaced Value" = Table.ReplaceValue(#"Reordered Columns","","In Stock",Replacer.ReplaceValue,{"SubCategory"})
    in
        #"Replaced Value"

     

    Regards,

    Xiaoxin Sheng

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi satubuku83,

     

    Since power bi tables not contains column index and row index, I think you need to add a index column to achieve this requirement.

     

    Steps:

    1. Add group index.

    2. Write measure.

    % Changes = 
    VAR index =
        MAX ( Metric[Index] )
    VAR previous =
        LOOKUPVALUE ( Metric[Metric], [Index], index - 1 )
    RETURN
        DIVIDE (
            CALCULATE ( SUM ( Metric[Amount] ), VALUES ( Metric[Metric] ) ),
            CALCULATE ( SUM ( Metric[Amount] ), Metric[Metric] = previous ),
            BLANK ()
        )
    

    3. Create matrix visual.

     

    Regards,

    Xiaoxin Sheng

    • satubuku83's avatar
      satubuku83
      Frequent Visitor

      Hi Xiao Xin/ Anonymous

       

      Always happy to hear from you.  

       

      Currently i am using Contains, will try out the method you suggested too!

      % of Metric (Checking) = IF(CONTAINS('DIM_SOURCE',DIM_SOURCE[Metric],"Reinsurance"),DIVIDE([MTD Closing Balance],[MTD Gross Premium]),
      if(CONTAINS('DIM_SOURCE',DIM_SOURCE[Metric],"Premium"),BLANK()))

       

      Anyway, do you have any good way to do GL statement in Power BI? Its driving me crazy.

       

      Assuming same data structure with additional data.

       

      Metric

      GL CodeAmount
      Premium10010000
      Premium2005000
      Premium300400
      Premium Reinsurance Inward4005000
      Premium Reinsurance Outward5006000
      Claim Direct600100
      Claim Direct600200
      Claim Reinsurance Inward700300
      Claim Reinsurance Outward800400

       

      Any clever way using DAX/ modeling to achieve the following? (ignore the remark - for your understanding purposes on the formulae)

       

      Really appreciate for your time!

       

      Remark for AmountMetricAmount% ChangesRemark for % Changes
       Premium15400  
       Premium Reinsurance Inward500032%Premium Reinsurance Inward / Premium
       Premium Reinsurance Outward600039%Premium Reinsurance Outward / Premium
      Premium + Premium Reinsurance Inward - Premium Reinsurance OutwardGross Premium14400  
           
       Claim Direct3002%Claim Direct/ Premium
       Claim Reinsurance Inward3006%Claim Reinsurance Inward/ Premium Reinsurance Inward
       Claim Reinsurance Outward4007%Claim Reinsurance Inward/ Premium Reinsurance Outward
      Claim Direct + Claim Reinsurance Inward - Claim Reinsurance OutwardGross Claim2001%Gross Claim/ Gross Premium
           
      Gross Premium - Gross ClaimNet Amount14200  

       

      Regards

      CS

      • Anonymous's avatar
        Anonymous
        Not applicable

        HI satubuku83,

         

        Actually, your requirement not very suitable for power bi. I think it should be more simply to achieve in excel pivot table.

        I also shared power bi version below:

         

        1. Grouping and transform table.

         

        2. Write measure formulas:

        Condition Total = 
        VAR categoryLevel =
            CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
                - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" )
        VAR premium =
            CALCULATE (
                CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
                    - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" ),
                GL[Categroy] = "Premium"
            )
        VAR claim =
            CALCULATE (
                CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] <> "OutWard" )
                    - CALCULATE ( SUM ( GL[Amount] ), GL[SubCategory] = "OutWard" ),
                GL[Categroy] = "Claim"
            )
        RETURN
            IF (
                ISFILTERED ( GL[Categroy] ),
                IF (
                    ISFILTERED ( GL[SubCategory] ),
                    SUM ( GL[Amount] ),
                    CALCULATE ( categoryLevel, VALUES ( GL[Categroy] ) )
                ),
                premium - claim
            )
        
        
        Changes = 
        IF (
            ISFILTERED ( GL[SubCategory] ),
            SWITCH (
                SELECTEDVALUE ( GL[Categroy] ),
                "Premium", IF (
                    SELECTEDVALUE ( GL[SubCategory] ) <> "In Stock",
                    CALCULATE (
                        SUM ( GL[Amount] ),
                        VALUES ( GL[Categroy] ),
                        VALUES ( GL[SubCategory] )
                    )
                        / CALCULATE (
                            SUM ( GL[Amount] ),
                            VALUES ( GL[Categroy] ),
                            GL[SubCategory] = "In Stock"
                        )
                ),
                "Claim", IF (
                    SELECTEDVALUE ( GL[SubCategory] ) <> "In Stock",
                    CALCULATE (
                        SUM ( GL[Amount] ),
                        VALUES ( GL[Categroy] ),
                        VALUES ( GL[SubCategory] )
                    )
                        / CALCULATE (
                            SUM ( GL[Amount] ),
                            GL[Categroy] = "Premium",
                            VALUES ( GL[SubCategory] )
                        )
                )
            ),
            IF (
                SELECTEDVALUE ( GL[Categroy] ) = "Claim",
                DIVIDE (
                    CALCULATE ( [Condition Total], GL[Categroy] = "Claim" ),
                    CALCULATE ( [Condition Total], GL[Categroy] = "Premium" ),
                    -1
                )
            )
        )
        

         

         

        3. Create matrix visual.

         

        Full query:

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCihKzc0szVXSUTI0MICQQDpWB1nGCCxjiilhDJYwQRVXCErNzCsuLUrMS05V8MwrTyxKgSrCNANFrX9pCVSxKVixGUyxc05iZq6CS2ZRanIJRBzqXJySRiiSWB1kDlZojFMhwjUWCF/GAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Metric = _t, #"GL Code" = _t, Amount = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Metric", type text}, {"GL Code", Int64.Type}, {"Amount", Int64.Type}}),
            #"Added Custom" = Table.AddColumn(#"Changed Type", "Categroy", each Text.Split([Metric]," "){0}),
            #"Grouped Rows" = Table.Group(#"Added Custom", {"Categroy"}, {{"Content", each Table.AddColumn(_, "SubCategory", each Text.AfterDelimiter([Metric],"Reinsurance ")), type table}}),
            #"Expanded Content" = Table.ExpandTableColumn(#"Grouped Rows", "Content", {"GL Code", "Amount", "SubCategory"}, {"GL Code", "Amount", "SubCategory"}),
            #"Reordered Columns" = Table.ReorderColumns(#"Expanded Content",{"Categroy", "SubCategory", "GL Code", "Amount"}),
            #"Replaced Value" = Table.ReplaceValue(#"Reordered Columns","","In Stock",Replacer.ReplaceValue,{"SubCategory"})
        in
            #"Replaced Value"

         

        Regards,

        Xiaoxin Sheng