Forum Discussion

Julian_12's avatar
Julian_12
New Member
4 years ago
Solved

Running total with group

Hi All, I have quite complicated issue. I have the presented below data table and I need to present the total number of items per value band in stacked area chart. Please note that at each date the number of product items can change and the value band of the product running total can change as well.

 

The result should look like this, but I do not know how to get there in Power Bi.

 

 

  • Hi Julian_12 ,

     

    According to your description, you need to use the pivot column function in the power query, and then replace the null values in it. Refer to the following screenshot.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY+xDoAgDET/hZkmbRXRUf0MwqCROOnA/w+CEURJmiaXl7trjRGjkIIwLEYmQALugti9c6ew8uEqcwXIFWdOnMLEsOVYnb/5FNE/3rstwzZ7EXCovFhEc/OpnoNS1emvPXLi4vRbpO5IdYYaqK/Nqiwv/rYX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, PurchaseItem = _t, PurchaseDate = _t, #"Value Band for product" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"PurchaseItem", Int64.Type}, {"PurchaseDate", type date}, {"Value Band for product", type text}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Product"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[#"Value Band for product"]), "Value Band for product", "PurchaseItem"),
        #"Sorted Rows1" = Table.Sort(#"Pivoted Column",{{"PurchaseDate", Order.Ascending}}),
        #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows1",null,0,Replacer.ReplaceValue,{"green", "amber", "red"})
    in
        #"Replaced Value"

    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


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

5 Replies

  • Julian_12 , The second screenshot seems like a Matrix visual- Date on row, Value brand per item on the column, and no of the purchased item on values.

     

    In case of stacked Area Value brand per item is a legend

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Community Support

    Hi Julian_12 ,

     

    According to your description, you need to use the pivot column function in the power query, and then replace the null values in it. Refer to the following screenshot.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY+xDoAgDET/hZkmbRXRUf0MwqCROOnA/w+CEURJmiaXl7trjRGjkIIwLEYmQALugti9c6ew8uEqcwXIFWdOnMLEsOVYnb/5FNE/3rstwzZ7EXCovFhEc/OpnoNS1emvPXLi4vRbpO5IdYYaqK/Nqiwv/rYX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, PurchaseItem = _t, PurchaseDate = _t, #"Value Band for product" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"PurchaseItem", Int64.Type}, {"PurchaseDate", type date}, {"Value Band for product", type text}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Product"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[#"Value Band for product"]), "Value Band for product", "PurchaseItem"),
        #"Sorted Rows1" = Table.Sort(#"Pivoted Column",{{"PurchaseDate", Order.Ascending}}),
        #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows1",null,0,Replacer.ReplaceValue,{"green", "amber", "red"})
    in
        #"Replaced Value"

    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


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

    • Julian_12's avatar
      Julian_12
      New Member

      Hi v-henryk-mstf and amitchandak 

       

      I really tried to make your solution works but I failed. Especially that ‘Value Band per Product’ is calculated in DAX. However, I realised that this problem can be solved in a much simpler way. If I calculate running total per group (see below table), then I can calculate the band values via a measure, which would identify the last row in each group by a date and sum the running total of the identified rows. The ‘Value Band per Product’ would be a legend to separate them on the graph. I believe that I identified the way to calculate it, but I have no idea how to do it in DAX. Would you be able to help please?

       

      ProductNo of Purchesed itemsRunning total per productPurchese DateValue band for product
      A101026/01/2021green
      A152502/05/2021green
      A224710/11/2021amber
      B2226/01/2021red
      B4609/10/2021amber
      B0623/11/2021green
      C505026/01/2021amber
      C126212/05/2021red
      C76918/07/2021amber
      C158422/11/2021green