Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

SUM different row

Hi,

I'm a beginner in Power BI and I have (I think?) a simple question:

 

I have currently this table (Total is a calculated column)

 

Col1ValueTotal
A1100 
A2200 
B1300 
B2400 
A.Total  
B.Total  

 

And I need to calculate the Total column as follow:

 

A.Total = Value[A1] + Value[A2] 

B.Total = Value[B1] + Value[B2]

 

final result:

 

Col1ValueTotal
A1100 
A2200 
B1300 
B2400 
A.Total 300
B.Total 700

 

Im not sure if I should use a measure or a calculated column for this calculation, for now: I'm trying with a Custom Column, Can you please advise if this script is correct ?

 

 

Calculte(SUM(["A1"]+["A2"]),FILTER(["Total"]="A.Total"))

and 

Calculte(SUM(["B1"]+["B2"]),FILTER(["Total"]="B.Total"))

 

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi,

     

    Sorry for the late reply, I was in vacation.

     

    I'm not sure if this is the right method by the admin to mark a reply as solved, that's definetly NOT the solution that I was looking for. Having admin rights doesn't mean you can do whatever you want...

     

    The right way that I did is:

    1) Pivot those rows into columns in Power BI Queries with language M.

    2) Create a calculated column to sum up the new pivoted columns.

    3) Unpivot those columns into rows.

     

    This wroked great !

11 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Quick question, do you want the summed up data listed in the table itself or during runtime in a visual?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi epappu,

       

      In the table itself.

       

      Visual function are used for sum only, the issue is that I need to use some soustraction and divide formula as well.

       

      Thanks

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Sabri - 

         

        I tried to do a similar dataset in a local pbi file, and please see the screengrab below. I think this will help you get your totals irrespective of how many rows you have starting with A or B.

         

         

        If this helped you, please accept this as solution.

         

        Regards

  • dax's avatar
    dax
    Community Support

    Hi Sabri,

    You could try to achieve this in M code like below

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjRU0lEyNDBQitUBcoyAHCMoxwkkYwzjgGRMQJxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Col1 = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Value", Int64.Type}}),
        #"Inserted First Characters" = Table.AddColumn(#"Changed Type", "First Characters", each Text.Start([Col1], 1), type text),
        #"Grouped Rows" = Table.Group(#"Inserted First Characters", {"First Characters"}, {{"sumtotal", each List.Sum([Value]), type number}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Col1", each [First Characters] & "'s sum total"),
        #"Appended Query" = Table.Combine({#"Inserted First Characters", #"Added Custom"}),
        #"Removed Columns" = Table.RemoveColumns(#"Appended Query",{"First Characters"})
    in
        #"Removed Columns"

    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

     

    Sorry for the late reply, I was in vacation.

     

    I'm not sure if this is the right method by the admin to mark a reply as solved, that's definetly NOT the solution that I was looking for. Having admin rights doesn't mean you can do whatever you want...

     

    The right way that I did is:

    1) Pivot those rows into columns in Power BI Queries with language M.

    2) Create a calculated column to sum up the new pivoted columns.

    3) Unpivot those columns into rows.

     

    This wroked great !

    • dax's avatar
      dax
      Community Support

      Hi Sabri, 

      I am sorry for my solution not  helping you and I  accept  it as solution(it might just suitable for my scenario), if this is not the solution, you coud unmark this case. By the way, it seems that you solved this problem, if so and if you'd like to, you could mark corresponding post as answer or share your solutions. That way, people who in this forum and have similar issue will benefit from it.

      Best Regards,
      Zoe Zhi

      If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Zoe,

         

        I will. I'm still learning PBI and your answers helped me a lot for understanding M mechanism, thank you so much for this !

         

        Have a nice day/evening !