Forum Discussion

Mugsy's avatar
Mugsy
Regular Visitor
2 years ago
Solved

Dynamic Measure to Carry Over Ending Balance to become Beginning Balance of Next Month

Hello Team, I need help creating a dynamic roll-forward measure for beginning and ending balances. That follow the pattern below:

 

 

The end balance for month 0 is rolled forward to become the beginning balance for  month 1 and the end balance for month 1 is rolled forward to become the beginning balance for month 2. Does any one have some insights on how I can achieve this?

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Mugsy ,

    Below is my table:

    The following M code will help you:

    let 
    
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlDSUQIiQyOlWJ1oJUMQ0xgkBOIZgXiGMB5I2NAExgMxDE3BvFgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Import = _t, Value = _t]), 
    
        ChangedType = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Import", Int64.Type}, {"Value", Int64.Type}}), 
    
        InitialEnding = 9800, 
        InitialBegin = 10000,
    
        AccumulateColumns = List.Accumulate( 
    
            List.Zip({ChangedType[Index], ChangedType[Import]}), 
    
            {[Index=0, Begin=InitialBegin, Export=null, Ending=InitialEnding]}, 
    
            (state, current) => 
    
            let 
    
                CurrentIndex = current{0}, 
    
                CurrentImport = current{1}, 
    
                PreviousEnding = if CurrentIndex = 0 then InitialBegin else state{CurrentIndex}[Ending], 
    
                CurrentExport = if CurrentIndex = 0 then null else (PreviousEnding + CurrentImport) / 2, 
    
                CurrentEnding = if CurrentIndex = 0 then InitialEnding else PreviousEnding + CurrentImport - CurrentExport, 
    
                CurrentRecord = [Index=CurrentIndex, Begin=PreviousEnding, Export=CurrentExport, Ending= CurrentEnding] 
    
            in 
    
                state & {CurrentRecord} 
    
        ), 
    
        AccumulatedTable = Table.FromRecords(AccumulateColumns), 
    
        JoinedTable = Table.NestedJoin(ChangedType,{"Index"},AccumulatedTable,{"Index"},"NewColumns",JoinKind.LeftOuter), 
    
        ExpandedTable = Table.ExpandTableColumn(JoinedTable, "NewColumns", {"Begin",  "Ending"}), 
    
        FinalTable = ExpandedTable, 
    
        #"Removed Top Rows" = Table.Skip(FinalTable,1) 
    
    in 
    
        #"Removed Top Rows"

    The final output is shown in the following figure:

    Best Regards,

    Xianda Tang

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

    • Mugsy's avatar
      Mugsy
      Regular Visitor

      Thank you so much for your response. I was able to implement the solution by Ashish_Mathur using Dax. I am grateful that you took the time to help me out.  Thank you!