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
Iamnvt
Continued Contributor
Continued Contributor

Sales - stock Calculation

hi,

 

I have a Sales table:

Order      Date               Product  Sales

111/25/2017A10
211/26/2017B20
311/27/2017A5

 

and a Stock table:

Product    Quantity

A1
B2

 

How can I make a DAX measure to calculate the Material Requirement for each date?

Eg. 

Date                Product   Requirement

11/25/2017A9
11/26/2017B18
11/27/2017A5

 

the tricky part is that the stock table does not have date column, meanwhile the Sales table has the Date column.

 

sample file is here:

https://1drv.ms/x/s!Aps8poidQa5zkvFwWm3YbzFb15bkTw

 

thanks

1 ACCEPTED SOLUTION

Hi @Iamnvt, I did some work on your sample workbook - please see:

 

Modified workbook

 

View solution in original post

8 REPLIES 8
DAX0110
Resolver V
Resolver V

Hi @Iamnvt, if you begin by creating a measure that sums the total product sales for each product before the current date, then you will quickly arrive at a simple formula for Material Requirement.

Iamnvt
Continued Contributor
Continued Contributor

Hi,

It is not working when you put the date field into the pivot.

Br

Hi @Iamnvt, can you upload the sample file elsewhere?  I can't download it from the current host.

Iamnvt
Continued Contributor
Continued Contributor

it is Onedrive link. My friend can download it well. Could you please try again? @DAX0110

Hi @Iamnvt, I did some work on your sample workbook - please see:

 

Modified workbook

 

Iamnvt
Continued Contributor
Continued Contributor

hi,

 

thanks for the answer. However, it doesn't give the correct result yet.

Order        Date             Product    Requirement

111/25/2017A9
211/26/2017B18
311/27/2017A5

 

I understand your approach of running total, and able to modify the DAX to give the correct result.

 

Questions regarding to the DAX:

1. What is the purpose of calling the variants with firstnonblank?

2. when I remove the date field out of the pivot. it seems not working. How can I fix that?

 

ProductPBI           Total Sold Qty       PBI Running Sold Qty      PBI Stock Qty     PBI Material Requirement

A1501 
B2002 

 

 

 

 

Hi @Iamnvt,

 

Good to see that you can debug my DAX formula - it's always harder to troubleshoot someone else's work than starting over from scratch yourself, so good work!

 

As for FIRSTNONBLANK - you can also use SELECTEDVALUE if doing this in Power BI Desktop.  It has to be used because there is only a filter context at that point. Try remove it and see what happens.

 

The pivot table after removing date?  Well it's still working.  This is my screenshot:

 

pbi x.png

Iamnvt
Continued Contributor
Continued Contributor

 

I think I found the correct debugged version now. It works even if I removed the date field out of the pivot.

 

=
VAR runningQty = [PBI Running Sold Qty]
VAR thisQty = [PBI Total Sold Qty]
VAR stockQty = [PBI Stock Qty]
VAR reqmt = IF( stockQty > runningQty
, IF(thisQty + runningQty > stockQty
, thisQty+runningQty - stockQty
,BLANK())
,thisQty
)
RETURN IF( ISBLANK( thisQty ), BLANK(), reqmt )

 

 Still few questions:

1. About the Running total formula, why do you use the VAR version to calculate that instead of the common cumulative pattern?  

=CALCULATE(Sales[PBI Total Sold Qty], FILTER(ALL('Date'), 'Date'[Date] <= MAX('Date'[Date])))

 

 I tried to replace  [PBI  Running Sold Qty] with the formula above. It gives the correct result, but when moving the Date field out, it is not correct anymore.

 

What is the correct Running total formula with FILTER if I don't use the VAR version?

 

2. Is there any other elegant solution? I think it is quite a simple problem; however, the solution seems not easy.

 

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.