Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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
  • Anonymous's avatar
    Anonymous
    6 years ago

    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's avatar
    Anonymous
    6 years ago

    Great thank you!

4 Replies

  • Anonymous's avatar
    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:

     

    Is this what you are after? 

     

    Kind regards,

    JB

    • Anonymous's avatar
      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's avatar
        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