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
Anonymous
Not applicable

Cumulative with filters

Hi I have this dataset which i want to put into a cumulative graph when filtered on the event through a slicer. 

 

This will be put into a line graph which will change depending on which event is selected. 

 

It needs to show amount payed, by number of days by event. I.e if a payment came in on day 1 (column daydiff), and the next on day 3 the Cumulative sum needs to show that order so we can see a progress of payments over time for that event- so the days ascending. 

 

I need to show prov, paid and total paid so i will need 3 different dax measures i'm assuming. 

 

DayDiffprovpaidTotal_paidevent
122709795.779795.77Event3
109205999.285999.28Event3
2070619.83619.83Event3
2180-75-75Event3
4280-150-150Event3
6450-125-125Event3
479010001000Event 2
403051.913051.91Event 2
469206883.156883.15Event 2
443401521.751521.75Event 2
5090009000Event3
798022062.0222062.02Event3
0000Event3
14600006000Event3
16-600060000Event 1
1977000Event 1
8344103441Event 1
3-34410-3441Event 1
47-506505.83-0.17Event 1
15060506Event 1
2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @Anonymous 

 

Try this one- i am just repeating the cumulative step 3 times - this should work if I get the referensing right. Sorry I do not have access to pbi at the moment. Please let.me know if this does not work.

 

(MyTable as table) as table =>
let
    Source = Table.Buffer(MyTable),
    Add1=Table.AddColumn(Source, "Running Sum (paid)", each null, type number),
     Add2=Table.AddColumn(Add1, "Running Sum (prov)", each null, type number),
    Add3=Table.AddColumn(Add2, "Running Sum (total paid)", each null, type number),

    TableType = Value.Type(Add3),
    Sorted = Table.Sort(Source,{{"DayDiff", Order.Ascending}}),
    Cumulative1 = List.Skip(List.Accumulate(Sorted[paid],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    Cumulative2 = List.Skip(List.Accumulate(Sorted[prov],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    Cumulative3 = List.Skip(List.Accumulate(Sorted[total paid],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    AddedRunningSum = Table.FromColumns(Table.ToColumns(Sorted)&{Cumulative1}&{Cumulative2}&{Cumulative3}, TableType)
in
    AddedRunningSum

 

Kind regards,

JB

View solution in original post

Anonymous
Not applicable

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

 

Slightly adapting this: https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/td-p/290123

 

This is the modified cumulative function (AddedRunningSum😞

(MyTable as table) as table =>
let
    Source = Table.Buffer(MyTable),
    TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)),
    Sorted = Table.Sort(Source,{{"DayDiff", Order.Ascending}}),
    Cumulative = List.Skip(List.Accumulate(Sorted[paid],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    AddedRunningSum = Table.FromColumns(Table.ToColumns(Sorted)&{Cumulative},TableType)
in
    AddedRunningSum

This is how it is used in the main query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZI9DsMgDIWvUmUOyDYY4wP0FFHGrp2qnr8h/MRUGcBP8Mk8G2/bgkSyrAscS0XZi0zq+X29P2HZ14MEpUayqnrKkzIkQU+ZUH0OVlgMc8Oc8NgNEGkAyCYYJEUeCPEVbBbRhiCACSfyoMo0IgCjV5zUxKXRg5Rz8MiTmtAYelZkQn8WdymL9hK0GlPjr5Yg2htBBIk80CwNCg3sy35g8ZNqcrikJVLpYDs3ZDWLlVGRuzfadTEaYsTe0CotUabAGcTdMLE84RiKHwauo+PAo/y7OYHU5/JU437/AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [DayDiff = _t, prov = _t, paid = _t, Total_paid = _t, event = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DayDiff", Int64.Type}, {"prov", Int64.Type}, {"paid", type number}, {"Total_paid", type number}, {"event", type text}}),

    TableType = Value.Type(Table.AddColumn(#"Changed Type", "Running Sum", each null, type number)),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"event"}, {{"AllData", AddedRunningSum, TableType}}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"DayDiff", "prov", "paid", "Total_paid", "Running Sum"}, {"DayDiff", "prov", "paid", "Total_paid", "Running Sum"})
in
    #"Expanded AllData"

 

This is the result:

PBI.png

 

Is this what you are after? 

 

Kind regards,

JB

Anonymous
Not applicable

Hi- @Anonymous Nearly! I need to have a running total of paid, prov and total paid in three new columns The date order by event is correct though.

 

Thanks in advance 

Anonymous
Not applicable

Hi @Anonymous 

 

Try this one- i am just repeating the cumulative step 3 times - this should work if I get the referensing right. Sorry I do not have access to pbi at the moment. Please let.me know if this does not work.

 

(MyTable as table) as table =>
let
    Source = Table.Buffer(MyTable),
    Add1=Table.AddColumn(Source, "Running Sum (paid)", each null, type number),
     Add2=Table.AddColumn(Add1, "Running Sum (prov)", each null, type number),
    Add3=Table.AddColumn(Add2, "Running Sum (total paid)", each null, type number),

    TableType = Value.Type(Add3),
    Sorted = Table.Sort(Source,{{"DayDiff", Order.Ascending}}),
    Cumulative1 = List.Skip(List.Accumulate(Sorted[paid],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    Cumulative2 = List.Skip(List.Accumulate(Sorted[prov],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    Cumulative3 = List.Skip(List.Accumulate(Sorted[total paid],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
    AddedRunningSum = Table.FromColumns(Table.ToColumns(Sorted)&{Cumulative1}&{Cumulative2}&{Cumulative3}, TableType)
in
    AddedRunningSum

 

Kind regards,

JB

Anonymous
Not applicable

Great thank you!

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.

Top Solution Authors
Top Kudoed Authors