Forum Discussion

arhomberg's avatar
arhomberg
Helper I
4 years ago
Solved

Iterative Calculation Over Time

Iterative Calculation   Hello all!   I could use some help with doing a calculation which I believe will end up being iterative.   In the table below, I would like to populate the count column ...
  • lbendlin's avatar
    4 years ago

    Thank you for providing sample data.  (Next time please sanitize it)

     

    This is impossible to do in Power BI DAX but relatively easy to achieve in Power Query M.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc67DcAwCATQXagtzOH4wyyR918jTuMkKDRc8Q7EeZJoFmRVSqRsuGP0NYXNaKZVKE8B3GTlh4/NXmoobQuEUT3310vDvI7wrG0RbsUpJNoDQtFfmRc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fulldate = _t, close_count = _t, count = _t, retention = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"fulldate", type date}, {"close_count", type number}, {"count", type number}, {"retention", type number}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,0,Replacer.ReplaceValue,{"close_count"}),
        #"Added Index" = Table.AddIndexColumn(#"Replaced Value", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Count2", each List.Accumulate({1..[Index]},287,(state,current)=> state * #"Added Index"{current-1}[retention] + #"Added Index"{current-1}[close_count]))
    in
        #"Added Custom"
    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".