Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
shuijgen
Frequent Visitor

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
        
    )

 

 

 

 

 

 

 

2 ACCEPTED SOLUTIONS

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

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

You can send it to me (message)


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

10 REPLIES 10
AmiraBedh
Super User
Super User

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

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

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...

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

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

When i made a new pbix file with just one partnumber in it and your code I get the correct output. See below. But in the original pbix with more different partnumbers this goes wrong. (see outcome in the column outcome of Amira). When i put an extra partnumber in the text pbix it seems ok. the original data is quite big and confidential. How to upload a test.pbix?

shuijgen_0-1695399346952.png

 

added the line voorraad <> blank() to the script and that did the trick.:

New Stock Level = 
VAR _product = 'D_CALCULATIE (2)'[ARTIKELNR]
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)'[ARTIKELNR] = _product))

VAR _previousOrders = 
    CALCULATE(
        SUM('D_CALCULATIE (2)'[Order Quantity]),
        FILTER(
            ALL('D_CALCULATIE (2)'),
            'D_CALCULATIE (2)'[ARTIKELNR] = _product && 
            'D_CALCULATIE (2)'[Delivery date] < _date &&
            'D_CALCULATIE (2)'[Voorraad] <> BLANK()
        )
    )

VAR _currentStock = _initialStock - _previousOrders

RETURN
    _currentStock - _order

I merged another table to this D_Calculatie table. That created extra rows with same partnumber with their individual sub partnumbers. The current stocklevel from ERP was only filled for the partnumber and not for all sub part numbers. The calculation did the sum of all rows including the sub part numbers where the stocklevel = blank. With this add in the calculation to exclude the blank ones solved the problem. 

You can send it to me (message)


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

i think where it goes wrong. Per partnumber there are more rows with some other info (columns with other data linked). for those rows the stocklevel is 0 in the data table, but the order quantity is the same. in the measure all rows with stocklevel blank should be eliminated.. but how?

Greg_Deckler
Super User
Super User

@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


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

unfortunately not. i get some strange big negative results here

@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.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

thnx Greg. The data model is now running well with the tips i got from Amira, but thnx anyway.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.