Forum Discussion

danilomorganti's avatar
danilomorganti
Frequent Visitor
5 years ago
Solved

Calculate cumulative reverse

Hello guys! I need your help about a reverse cumulative formula. I get data from file with the cumulative of value and I would like to decompose this value with day-by-day value. How can I do it?  ...
  • Jimmy801's avatar
    5 years ago

    Hello danilomorganti 

     

    you can try this approach but it doesn't look on the date but really on the prior cell value. It basically adds a new column with the cumulative-column but shifted down by one row. After that a new column is added where your needed calculation takes place

    Here a code example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc27DQAhDATRXhwjnT8Y3Aui/zbO0hKsNNFL5hxR+zpXNxmickeTE9mzYEvYJPOAJVlM2CLLBdtku2BFVv24Pw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Cumulative = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Cumulative", Int64.Type}}),
        GetNewColumnWithPreviousValue = Table.FromColumns
        (
            Table.ToColumns(#"Changed Type")&{{0}&List.RemoveLastN(#"Changed Type"[Cumulative])},
            Table.ColumnNames(#"Changed Type")&{"CumulativeShifted"}
        ),
        #"Added Custom" = Table.AddColumn(GetNewColumnWithPreviousValue, "Result", each [Cumulative]-[CumulativeShifted]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"CumulativeShifted"})
    in
        #"Removed Columns"

    Result

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • v-alq-msft's avatar
    5 years ago

    Hi, danilomorganti 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    You may create a custom column as below.

    let
    tab=Table.SelectRows(#"Changed Type",(x)=>Date.Month(x[Date])=Date.Month([Date]) and x[Date]<[Date]),
    m=try [Cumulative]-Table.Max(tab,"Date")[Cumulative] otherwise [Cumulative]-0
    in 
    m

     

    Result:

     

    Best Regards

    Allan

     

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