Forum Discussion
Cumulative chart based on sorted column
- 5 years ago
Hi puchrova
The cumulative column is created in Power Query. It will automatically update when new data is loaded.
You need to open the PBIX file I linked to and then click on Transform Data. This will open the query editor and you can then open the Advanced Editor to see the M code that makes up the query.
Because you only provided an image as sample data I entered this by hand to create the example, but you will need to create your own query that loads you data - presumably from an Excel workbook?
When you open the Advanced Editor in my PBIX file you'll see this code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlXSUTIyUIrViVYyNACyDSFsYyDTBMI0AgubKsXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [price = _t, amount = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"price", Int64.Type}, {"amount", Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"price", Order.Descending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Added Index", "Cumulative Total", each List.Sum(List.FirstN(#"Added Index"[amount], [Index]))), #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Cumulative Total", Int64.Type}}) in #"Changed Type1"You need to replace the Source step with a couple of steps that loads your data. If your data comes from a workbook the new query will look something like this
let Source = Excel.Workbook(File.Contents("D:\temp\data.xlsx"), null, true), Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"price", Int64.Type}, {"amount", Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"price", Order.Descending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Added Index", "Cumulative Total", each List.Sum(List.FirstN(#"Added Index"[amount], [Index]))), #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Cumulative Total", Int64.Type}}) in #"Changed Type1"So in this new query th data isloaded from the file located at d:\temp\data.xlsx and you need to change that path/name to suit your file, and the data is in an Excel table called Table1
Regards
Phil
If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.
Please try this measure expression in your visual. Replace Price with your actual table name.
NewMeasure =
VAR vThisPrice =
MIN ( 'Price'[Price] )
RETURN
CALCULATE (
SUM ( 'Price'[Amount] ),
ALL ( 'Price'[Price] ),
'Price'[Price] >= vThisPrice
)
Regards,
Pat
Thanks but I'm not sure how to show it on the visual, it won't let me put the new measure with cummulative total on the x axis?