Forum Discussion

AGo's avatar
AGo
Icon for Post Patron rankPost Patron
9 years ago

Stock cumulative total DAX

Hi! I'm trying to DAX a calculated column for the stock cumulative total by product. When present the first movement of the year of a product has Initial Inventory="Y" so the cumulative calculation resets and begins from that quantity value, if there's not a Initial inventory movement the calculation won't reset, how can I write this formula?

Another problem is that I could have multiple movements of the same product in the same date with the same moved quantity, I have to use a row index number logic?

Consider that this database has several years of data.

Someone can please help me?

21 Replies

  • Michiel's avatar
    Michiel
    Icon for Resolver III rankResolver III

    First, the best way to implement this logic is to use a measure, not a calculated column. To do this, create a separate date table and a relationship from Stock[Date] to Date[Date].

    Use this formula to create a measure:

    CumulativeStock = 
    VAR MaxDate = MAX('Date'[Date])
    VAR InitialDate = MAXX(FILTER(ALL(Stock);Stock[Date]<=MaxDate && Stock[Initial Inventory]="Y");Stock[Date])
    RETURN
    CALCULATE(SUM(Stock[Quantity]);ALL('Date');'Date'[Date]>=InitialDate && 'Date'[Date]<=MaxDate)

    In this formula, the variable MaxDate contains the last selected date. The variable InitialDate determines the last initial inventory date before the last selected date. After this, the measure calculates the total quantity of stock lines between the initial date and the max date.

    For your sample data, this results in something like:

    • AGo's avatar
      AGo
      Icon for Post Patron rankPost Patron

      Hi Michiel,

      It is not working, I need a calculated column, and it returns wrong and big results (maybe it consider also other products)

      • tringuyenminh92's avatar
        tringuyenminh92
        Icon for Memorable Member rankMemorable Member

        Hi AGo,

         

        Did you use the Date column of Dates table or Date column in your fact table?

  • Hi AGo,

     

    please try calculated measure with expression:

     

    Stock Cummulative = CALCULATE(sum(Stock[Quantity]),filter(ALL(Dates), Dates[Date].[Year] = MAX(Dates[Date].[Year])  &&  Dates[Date]<= MAX(Dates[Date])  )) 

     

    To understand more about cummulative with time, you could refer topic: http://www.daxpatterns.com/time-patterns/

     

    If this works for you please accept it as solution and also like to give KUDOS.

    Best regards
    Tri Nguyen

    • AGo's avatar
      AGo
      Icon for Post Patron rankPost Patron

      tringuyenminh92 I need a calculated column (also because I need to recreate a historical column and othe calculations over it of the stock entity by product). I read that page before this post, but it doesn't consider the complexity of my case (multiple products, conditional initial inventory...)

      • tringuyenminh92's avatar
        tringuyenminh92
        Icon for Memorable Member rankMemorable Member

        Hi AGo,

         

        please try Michiel's solution, in case it's not working with your expectation, i will try another expression with calculated column.