Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Rollover Function Needed Within Matrix

Hello   I have a table with the below metrics. Stock On Hand is always the current month, then Total Receipts and Total Demand for each month. Net Inventory is a measure that is Stock On Hand + Tot...
  • Jihwan_Kim's avatar
    2 years ago

    Hi,

    I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

    Please check the below picture and the attached pbix file.

    I hope the below can provide some ideas on how to create a solution for your semantic model.

     

     

     

     

    Total receipts: = 
    SUM( Data[receipt] )

     

    Total demand: = 
    SUM( Data[demand] )

     

    WINDOW function (DAX) - DAX | Microsoft Learn

     

    Net inventory: = 
    VAR _t =
        ADDCOLUMNS (
            WINDOW (
                1,
                ABS,
                0,
                REL,
                ALL ( 'Calendar'[Month Year], 'Calendar'[Month Year sort] ),
                ORDERBY ( 'Calendar'[Month Year sort], ASC )
            ),
            "@dec2023",
                CALCULATE (
                    SUM ( Data[stockonhand] ),
                    KEEPFILTERS ( 'Calendar'[Month Year sort] = DATE ( 2023, 12, 31 ) )
                ),
            "@receipt", [Total receipts:],
            "@demand", [Total demand:]
        )
    RETURN
        SUMX ( _t, [@dec2023] + [@receipt] - [@demand] )

     

    Stock on hand: = 
    VAR _t =
        ADDCOLUMNS (
            WINDOW (
                1,
                ABS,
                -1,
                REL,
                ALL ( 'Calendar'[Month Year], 'Calendar'[Month Year sort] ),
                ORDERBY ( 'Calendar'[Month Year sort], ASC )
            ),
            "@dec2023",
                CALCULATE (
                    SUM ( Data[stockonhand] ),
                    KEEPFILTERS ( 'Calendar'[Month Year sort] = DATE ( 2023, 12, 31 ) )
                ),
            "@receipt", [Total receipts:],
            "@demand", [Total demand:]
        )
    RETURN
        IF (
            SELECTEDVALUE ( 'Calendar'[Month Year sort] ) = DATE ( 2023, 12, 31 ),
            CALCULATE (
                SUM ( Data[stockonhand] ),
                KEEPFILTERS ( 'Calendar'[Month Year sort] = DATE ( 2023, 12, 31 ) )
            ),
            SUMX ( _t, [@dec2023] + [@receipt] - [@demand] )
        )