Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Julian_12
New Member

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.

 

Julian_12_1-1637935761678.png

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

Julian_12_0-1637936093873.png

 

Julian_12_0-1637935000605.png

 

1 ACCEPTED SOLUTION
v-henryk-mstf
Community Support
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"

vhenrykmstf_0-1638242759565.png

vhenrykmstf_1-1638242777759.png

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.

View solution in original post

5 REPLIES 5
v-henryk-mstf
Community Support
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"

vhenrykmstf_0-1638242759565.png

vhenrykmstf_1-1638242777759.png

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.

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
amitchandak
Super User
Super User

@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

Hi @amitchandak, yes, but the real question is how to calculate the second table from the first one. 

@Julian_12 , If only want to display matrix would do that ?

Date on row, Value brand per item on the column, and no of the purchased item on values.

 

Else unpivot the table in power query

https://radacad.com/pivot-and-unpivot-with-power-bi

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.