Forum Discussion

9mikejacobs's avatar
9mikejacobs
Frequent Visitor
6 years ago
Solved

How to utilize previous lines record for next lines computation

Hello,    I have a data model that contains the below tables.   Inventory - snapshot of current inventory position as of today PO Table - Open PO's awaiting delivery Open Deliveries - Open deli...
  • v-robertq-msft's avatar
    6 years ago

    Hi, 9mikejacobs 

    Here are the steps you can follow to achieve

    1. sort the YearWeek column
    2. create an index column in the power query editor, you can follow this picture:

     

    3.create measure for Inventory Rolling

    Inventory Rolling = var _index=MAX('Table'[Index])
    
    var _inv=CALCULATE(sum('Table'[ Inventory ]),FILTER(ALL('Table'),'Table'[Index]<=_index))
    
    var _poq=CALCULATE(sum('Table'[ PO Order Qty ]),FILTER(ALL('Table'),'Table'[Index]<=_index))
    
    var _ooq=CALCULATE(sum('Table'[ Open Order Qty ]),FILTER(ALL('Table'),'Table'[Index]<=_index))
    
    var _odq=CALCULATE(sum('Table'[ Open Deliveries Qty ]),FILTER(ALL('Table'),'Table'[Index]<=_index))
    
    return _inv+_poq-_ooq-_odq

    4.create measure for inventory:

    Inventory = var _inv=CALCULATE([Inventory Rolling],FILTER(ALL('Table'),'Table'[Index]=MAX('Table'[Index])-1))
    
    return if(ISBLANK(_inv),CALCULATE(MAX('Table'[ Inventory ]),'Table'[Index]=1),_inv)

    And you can get your expected chart like this:

     

     

    Best Regards,

    Robert Qin

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