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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
AudreyWen
Helper I
Helper I

Formula for calculating available stock per order

Hi,

 

I need your help. I want to calculate the available stock for sales orders.

 

Beneath is an example

 

Stock table:

ArticleTotal stock
1234510
2345615
345673
4567850
567895

 

Sales order table:

OrderArticledelivery quantityDeliver dateAvailable stock for order
10101012345519-2-202010
40404012345522-2-20205
70707012345522-2-20200
90909012345528-2-2020-5
202020234561018-2-202015
222222234562520-2-20205
30303034567123-2-20203
50505034567123-2-20202
60606034567128-2-20201
808080456781019-2-202050
111111456781520-2-202040
33333345678522-2-202025
444444456781023-2-202020
55555545678523-2-202010
66666645678528-2-20205
77777756789222-2-20205
88888856789428-2-20203
9999995678961-3-2020-1

 

In the column "Available stock for order" I want a calculation that shows how much stock there is available for the sales order. The data that is now in this column, is the result I want. 

 

I realy cant figure out how i can get this result.

 

I hope someone can help me with this 😊

 

Greetings Audrey

 

 

 

 

 

1 ACCEPTED SOLUTION
JustJan
Responsive Resident
Responsive Resident

 hi @AudreyWen ,

 

It is best to add an index column to your order column. That makes it much easier to deal with orders on the same date. 

 

2020-02-14 10_07_39-20200212 - Power BI Desktop.png

 

I think this is the result you are looking for. 

 

The measure is in table is:

StockLevel BEFORE = 
var currentArticle = if (HASONEVALUE(StockOrders[Article]), min(StockOrders[Article]),0)
var StockLevel = CALCULATE(sum(Stock[Total stock]) , Stock[Article] = currentArticle)
var x =
CALCULATE(
    SUM('StockOrders'[delivery quantity]),
StockOrders[Article] = currentArticle,
    FILTER(
        ALL('StockOrders'),
        'StockOrders'[Index]< MAX('StockOrders'[Index])
    )
)
var result = StockLevel - x
return result

 

If you want th stock level AFTER, just change this line: 

 

        'StockOrders'[Index]< MAX('StockOrders'[Index])

to 

        'StockOrders'[Index]<=MAX('StockOrders'[Index])

 

hope this helps

 

Jan 

View solution in original post

7 REPLIES 7
JustJan
Responsive Resident
Responsive Resident

Hi @AudreyWen ,

 

I did not provide you with the relationship between my tables. 

 

2020-02-14 12_54_22-20200212 - Power BI Desktop.png

 

And my Index is not more than a sequencenumber on the table. 

 

2020-02-14 12_57_37-20200212 - Power BI Desktop.png

 

If you change the last lines of the measure to, for instance: 

 

var result = currentArticle    //StockLevel - x
return result
 
You can check if the correct artikelnummer is used.
 
Jan 
 

 

 

 

Jan 

Hi again Jan,

 

This time i've tried it in the simple test file and there it does work. Now i don't know why it keeps on running in the big work file. Maybe because there are many rows?

 

I have an column (Status levering) with a status of an row. In the page filter in the report i have filtered on only the rows with status "open" or "in levering" this reduce the number of rows a lot.

 

Can this be my problem?

JustJan
Responsive Resident
Responsive Resident

Hi Audrey, 

 

The measure does calculate a sort of running total. I have a similar problem where I need to calculate a running total over more than 50.000 records. The calculation uses all the computers internal memory (up to 12GB). 

So you could check you memory consumption in the task manager and see if that is also the problem. 

 

The only way I can think of at the moment is to calculate the totals outside PBI, but then you will loose a lot of the dynamics of a dashboard.

 

Not too sure if page filter are part of the issue, I'll check it on my data model too. 

 

Jan 

 

 

 

 

Hi Jan,

 

I only need the measure on those rows. Now i have made a sort of "between solution". In the report with orders i filter on the status "Open" or "In levering". Than i have the only rows i need for this measure. This report i export to an .CSV file. This .CSV file is now an Table in my file. That table i use now in a report and now it wordt great. But the "between solution" is not ideal because i have to do extra steps. I hope we can find a solution in wich the measure knows directly the rows that are needed. 

 

But still i'm very happy !!! 😁

Hi Jan,

 

Sorry, i couldn't test it this weekend.

 

I have made the relationship between those 2 and another index column (this time in the query).

 

Still it doesn't work. I have tried your suggestion with changing the last row in the messure to:

var result = currentArticle    //StockLevel - x
return result
And this is not a good result, I don't get the current article number but 0,00 with every row in the table.
 
Can you please help me again?
JustJan
Responsive Resident
Responsive Resident

 hi @AudreyWen ,

 

It is best to add an index column to your order column. That makes it much easier to deal with orders on the same date. 

 

2020-02-14 10_07_39-20200212 - Power BI Desktop.png

 

I think this is the result you are looking for. 

 

The measure is in table is:

StockLevel BEFORE = 
var currentArticle = if (HASONEVALUE(StockOrders[Article]), min(StockOrders[Article]),0)
var StockLevel = CALCULATE(sum(Stock[Total stock]) , Stock[Article] = currentArticle)
var x =
CALCULATE(
    SUM('StockOrders'[delivery quantity]),
StockOrders[Article] = currentArticle,
    FILTER(
        ALL('StockOrders'),
        'StockOrders'[Index]< MAX('StockOrders'[Index])
    )
)
var result = StockLevel - x
return result

 

If you want th stock level AFTER, just change this line: 

 

        'StockOrders'[Index]< MAX('StockOrders'[Index])

to 

        'StockOrders'[Index]<=MAX('StockOrders'[Index])

 

hope this helps

 

Jan 

Hi Jan,

 

I have used your messure in my report but it keeps running like it doesn't know what to do. I have changed al the tables and columns in the meassure to the real ones.

 

StockLevel BEFORE =
var currentArticle = if (HASONEVALUE(Schedullines[Artikelnr]); min(Schedullines[Artikelnr]);0)
var StockLevel = CALCULATE(sum(AvStock[LBKUM]); AvStock[Artikelnr.] = currentArticle)
var x =
CALCULATE(
SUM(Schedullines[Bevestigde hoeveelheid (BMENG)]);
Schedullines[Artikelnr] = currentArticle;
FILTER(
ALL(Schedullines);
Schedullines[Indexvrrd]< MAX(Schedullines[Indexvrrd])
)
)
var result = StockLevel - x
return result

 

can you tell me if i did anything wrong?

 

Thanx!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors