Forum Discussion

shuijgen's avatar
shuijgen
Frequent Visitor
3 years ago
Solved

Calculate new stock level

I have a table in PowerBi (D_Calculatie (2)) with all kind of partnumbers and customer orders with delivery dates. I have stock level for this part coming from ERP, but is only valid for the first order line. After delivery of the 61 pieces for this part the new stock level for the next orderline is -13 and so on. How to calculate this in a calc. column?

 

Delivery datePartnrOrder QuantityVoorraadIndexNew Stock levelresult from code
07/09/202312345661481477-13-13
13/09/202312345647481479-601
20/09/202312345660482895-120-12
22/09/202312345629484792-14919
29/09/202312345691483761-240-43
06/10/202312345646484989-2862
20/10/202312345610482765-29638
20/10/202312345691482761-387-43
27/10/202312345691482769-478-43

 

I have tried this but result is in the right column in above table.

 

 

 

 

 

 

New StockLevel =
VAR _product = 'D_CALCULATIE (2)'[ARTIKELNR]
VAR _date = 'D_CALCULATIE (2)'[Delivery date]
VAR _order = 'D_CALCULATIE (2)'[Order Quantity]
VAR _previousStock = 'D_CALCULATIE (2)'[Voorraad]
VAR _currentStock =
    IF (
        NOT ISBLANK(_date),
        CALCULATE (
            SUM('D_CALCULATIE (2)'[Order Quantity]),
            FILTER (
                ALL('D_CALCULATIE (2)'),
                'D_CALCULATIE (2)'[ARTIKELNR] = _product &&
                'D_CALCULATIE (2)'[Delivery date] < _date
            )
        )
    )
RETURN
    IF (
        ISINSCOPE('D_CALCULATIE (2)'[Index]),
        0,
        _previousStock- _order
        
    )

 

 

 

 

 

 

 

  • Can you share your data ? Otherwise try the following : 

    New Stock Level = 
    VAR _product = 'D_CALCULATIE (2)'[Partnr]
    VAR _date = 'D_CALCULATIE (2)'[Delivery date]
    VAR _order = 'D_CALCULATIE (2)'[Order Quantity]
    VAR _initialStock = CALCULATE(MIN('D_CALCULATIE (2)'[Voorraad]), FILTER('D_CALCULATIE (2)', 'D_CALCULATIE (2)'[Partnr] = _product))
    
    VAR _previousOrders = 
        CALCULATE(
            SUM('D_CALCULATIE (2)'[Order Quantity]),
            FILTER(
                ALL('D_CALCULATIE (2)'),
                'D_CALCULATIE (2)'[Partnr] = _product && 
                'D_CALCULATIE (2)'[Delivery date] < _date
            )
        )
    
    VAR _currentStock = _initialStock - _previousOrders
    
    RETURN
        _currentStock - _order
    

10 Replies

  • The key issue with your current calculation is that you are calculating a 'running sum' of orders, but not considering the current order in the calculation. Here's how you can potentially fix it:

    Firstly, sort your table by 'Delivery date' and 'Index' to make sure it is in the correct order.

    Now, you can create a new column with the following DAX code:

    New Stock Level =
    VAR _product = 'D_CALCULATIE (2)'[Partnr]
    VAR _date = 'D_CALCULATIE (2)'[Delivery date]
    VAR _order = 'D_CALCULATIE (2)'[Order Quantity]
    VAR _previousStock = 'D_CALCULATIE (2)'[Voorraad]
    
    VAR _sumOfOrdersUpToDate =
    CALCULATE (
    SUM('D_CALCULATIE (2)'[Order Quantity]),
    FILTER (
    ALL('D_CALCULATIE (2)'),
    'D_CALCULATIE (2)'[Partnr] = _product &&
    'D_CALCULATIE (2)'[Delivery date] <= _date // Include current date
    )
    )
    
    RETURN
    _previousStock - _sumOfOrdersUpToDate
    • shuijgen's avatar
      shuijgen
      Frequent Visitor

      unfortunately not the correct result.

      I would expect that for the first row the result is : -13. Stock =48 and order quantity = 61. So 13 short.

      Next line (in order of delivery date) has than a stock of -13 and if order quantity = 57, the shortage (new stock level) will be -13 -57 = -70. 

       

      from your code i get:

      -196 first line and -380 next line...

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

        Can you share your data ? Otherwise try the following : 

        New Stock Level = 
        VAR _product = 'D_CALCULATIE (2)'[Partnr]
        VAR _date = 'D_CALCULATIE (2)'[Delivery date]
        VAR _order = 'D_CALCULATIE (2)'[Order Quantity]
        VAR _initialStock = CALCULATE(MIN('D_CALCULATIE (2)'[Voorraad]), FILTER('D_CALCULATIE (2)', 'D_CALCULATIE (2)'[Partnr] = _product))
        
        VAR _previousOrders = 
            CALCULATE(
                SUM('D_CALCULATIE (2)'[Order Quantity]),
                FILTER(
                    ALL('D_CALCULATIE (2)'),
                    'D_CALCULATIE (2)'[Partnr] = _product && 
                    'D_CALCULATIE (2)'[Delivery date] < _date
                )
            )
        
        VAR _currentStock = _initialStock - _previousOrders
        
        RETURN
            _currentStock - _order
        
  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    shuijgen Try:

    Column = 
        VAR __Partnr = [Partnr]
        VAR __DeliveryDate = [Delivery date]
        VAR __MinDate = MINX(FILTER('Table', [Partnr] = __Partnr), [Delivery date])
        VAR __Voorraad = MINX(FILTER('Table', [Partnr] = __Partnr && [Delivery date] = __MinDate), [Voorraad])
        VAR __Table = FILTER('Table', [Partnr] = __Partnr && [Delivery date] <= __DeliveryDate)
        VAR __Delivered = SUMX(__Table, [Order Quantity])
        VAR __Result = __Voorraad - __Delivered
    RETURN
        __Result
    • shuijgen's avatar
      shuijgen
      Frequent Visitor

      unfortunately not. i get some strange big negative results here

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        shuijgen It works for your sample data. Is there something else going on with your data model? Or can you provide a larger sample size? See attached PBIX file below that returns the correct results based upon your sample data.