Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Rolling SUM in DAX

Hello all,

 

I have a table called FullView where i have information in the following format, that list for each pair of Ingredient-CalendarWeek, the existing stock plus the projected inbounds and outbounds/Forecast.

 

CW_ProdID Prod ID     Ingredient Description              Ingredient Name           Weight CW           Stock    Inbound Forecast Ordinal

40-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml40330800-3
40-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml40971800-3
41-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml41330800-2
41-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml41971800-2
42-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml42704903835-1
42-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml42330800-1
43-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml43308306390
43-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml437049000
44-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml44583001
44-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml447049001
45-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml457049054222
45-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml4558305462
46-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml46583045183
46-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml467049003
47-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml47583004
47-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml477049004
48-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml48583005
48-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml487049005
49-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml49583006
49-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml497049006
50-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml50583007
50-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml507049007
51-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml51583008
51-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml517049008
52-2093820938azijn, aceto balsamico, 25.0 mlazijn, aceto balsamico25.0 ml52583009
52-3078230782azijn, framboos, 25.0 mlazijn, framboos25.0 ml527049009

 

The ordinal is calculated based on the CW (CalendarWeek). Because current CW is 43, corresponds to Ordinal 0. Next week, will be Ordinal 1, etc.

 

I want to create a Pivot Table with a projected inventory view where based on the week, I can see the projected inventory.

 

For week with ordinal 3, the projected inventory would be (Stock at Week 0 + Inbounds(week 0,1,2,3) - Outbounds (0,1,2,3). I would like to create a measure that would give this information.

 

Until now I was able to come with:

 

CALCULATE(
IF(AVERAGE(WeekControl[Position])<0,
SUM(Full_View[Stock]),
IF(AVERAGE(WeekControl[Position])=0,
SUM(Full_View[Stock])+SUM(Full_View[Inbound])-SUM(Full_View[Forecast]),
CALCULATE(CALCULATE(SUM(Full_View[Stock]),ALL(Full_View[Ordinal])),Full_View[Ordinal]=0)
+CALCULATE(SUM(Full_View[Inbound]),FILTER(ALL(Full_View), Full_View[Ordinal] <= MAX(Full_View[Ordinal]))))))

 

But it's not giving the result I want. Can you help please?

I'm using Power Pivot in office365

  • Anonymous's avatar
    Anonymous
    8 years ago

    I ended up finding a 100% working solution:

     

    =CALCULATE (
        IF (
            AVERAGE ( WeekControl[Position] ) < 0,
            SUM ( Table1[Stock] ),
            IF (
                AVERAGE ( WeekControl[Position] ) = 0,
                SUM ( Table1[Stock] ) + SUM ( Table1[Total Units Ordered] )
                    - SUM ( Table1[Total # Units per Ingredient] ),
                CALCULATE (
                    CALCULATE ( SUM ( Table1[Stock] ), ALL ( Table1[Ordinal] ) ),
                    Table1[Ordinal] = 0
                )
                    + CALCULATE (
                        SUM ( Table1[Total Units Ordered] ),
                        FILTER (
                            FILTER (
                                ALLEXCEPT ( Table1, Table1[Ingredient Description] ),
                                Table1[Ordinal] <= MAX ( Table1[Ordinal] )
                            ),
                            Table1[Ordinal] >= 0
                        )
                    )
                    - CALCULATE (
                        SUM ( Table1[Total # Units per Ingredient] ),
                        FILTER (
                            FILTER (
                                ALLEXCEPT ( Table1, Table1[Ingredient Description] ),
                                Table1[Ordinal] <= MAX ( Table1[Ordinal] )
                            ),
                            Table1[Ordinal] >= 0
                        )
                    )
            )
        )
    )

4 Replies

  • Hey,

     

    I think this measure is pretty close, at least for the ordinal positions >= 0, I'm not sure about what should happen if the the ordinal position is less than 0.

     

    My measure is called stock projection ...

     

    stock projection=
    var currrentOrdinal = 
        IF(HASONEVALUE('Table1'[ordinal])
            ,MAX('Table1'[ordinal])
            ,BLANK()
        )
    var startOrdinal = 
        IF(HASONEVALUE('Table1'[ordinal]),
            IF(MAX('Table1'[ordinal]) >= 0, 0,-666)
            ,BLANK()
        )
    return
    
    IF(startOrdinal = 0,
        CALCULATE(
            SUM('Table1'[stock])
            ,'Table1'[ordinal] = startOrdinal
        )
        +
         CALCULATE(
            SUM('Table1'[inbound]) - SUM('Table1'[outbound])
            ,FILTER(ALL(Table1[ordinal])
                ,'Table1'[ordinal] >= startOrdinal && 'Table1'[ordinal] <= currrentOrdinal
            )
        )
        ,BLANK() -- here goes the formula for past weeks (ordinal less than zeor)
    )

    This allows to create a Pivot Table like so:

     

     

    Please be aware of the following

    Currently this measure will just work if the column ordinal is used as row or column header.

     

    i guess it should be easy to adjust the measure, if the pivot table looks into the past (ordinal below zero), just adjust the BLANK in my measure to your liking

     

    Hopefully this is what you are looking for.

     

    Regards,

    Tom

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi TomMartens,

       

      First, let me thank you for putting the effort in trying to help me! Your solution is not completely performing what I need, because it only shows the projected sale/inbound for the given ordinal week. So for outbound of ordinal week 3, it would need to sum all the outbounds for that ingredient of the ordinal week (0,1,2 and 3). The objective is that I can see clearly how the added outbounds and inbounds would progress in time.

       

       

      • TomMartens's avatar
        TomMartens
        Icon for Super User rankSuper User

        Hey,

         

        then you have to create an additional measure like so 

        outbound projection =
        var currrentOrdinal = 
            IF(HASONEVALUE('Table1'[ordinal])
                ,MAX('Table1'[ordinal])
                ,BLANK()
            )
        var startOrdinal = 
            IF(HASONEVALUE('Table1'[ordinal]),
                IF(MAX('Table1'[ordinal]) >= 0, 0,-666)
                ,BLANK()
            )
        return
        
        IF(startOrdinal = 0,
                 CALCULATE(
                SUM('Table1'[outbound])
                ,FILTER(ALL(Table1[ordinal])
                    ,'Table1'[ordinal] >= startOrdinal && 'Table1'[ordinal] <= currrentOrdinal
                )
            )
            ,BLANK()
        )

        and add this measure to your Pivot Table:

        Same becomes true for the inbound and maybe after all you have to iterate about the ordian using
        SUMX(VALUES('Table1'[ordinal]
        to get the total right

         

        If you need more help, please put the sample data into an Excel file, upload the Excel to onedrive or dropbox. This helps to avoid misunderstandings.

         

        Regards,

        Tom