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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
thatjoey20
Helper I
Helper I

Subtracting stock by date and product

Hello everybody, 

I have a problem that I trying to solve using DAX code, that is the following case:

In my table I have, Date, Stock, Product and Quantity. I need a measure that subtracts the stock  by the quantity, and be cummulative, for example:

thatjoey20_0-1711111970205.png

 

This needs to be ordered by date and per product, so only first date 20/03 will have stock to sell having 21 remaining. 

2 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

Hi,

Ensure there is a Calendar Table.  Create a relationship (Many to One and Single) from the Date column of the Data Table to the Date column of the Calnedar Table.  To your visual, drag Date from the Calendar Table.  Assuming you have already written measures of Stock and Quantity, write these measures:

Cumulative quantity = calculate([quantity],datesbetween(calendar[date],minx(all(calendar),calendar[date]),max(calendar[date])))

Diff = [Stock]-[Cumulative Quantity]

The Cumulative quantity measure will calculate the quantity from the very inception.

Hope this helps.

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

v-xinruzhu-msft
Community Support
Community Support

Hi @thatjoey20 

The solution @Ashish_Mathur  provided is excellent, and you can also refer to the following solution.

Sample data

vxinruzhumsft_0-1711334104021.png

Create a measure.

MEASURE =
VAR a =
    CALCULATE (
        SUM ( 'Table'[Quantity] ),
        ALLSELECTED ( 'Table' ),
        'Table'[Date] <= MAX ( 'Table'[Date] ),
        'Table'[Product Code] IN VALUES ( 'Table'[Product Code] )
    )
RETURN
    MAX ( 'Table'[Stock] ) - a

Output

vxinruzhumsft_2-1711334200997.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

View solution in original post

4 REPLIES 4
v-xinruzhu-msft
Community Support
Community Support

Hi @thatjoey20 

The solution @Ashish_Mathur  provided is excellent, and you can also refer to the following solution.

Sample data

vxinruzhumsft_0-1711334104021.png

Create a measure.

MEASURE =
VAR a =
    CALCULATE (
        SUM ( 'Table'[Quantity] ),
        ALLSELECTED ( 'Table' ),
        'Table'[Date] <= MAX ( 'Table'[Date] ),
        'Table'[Product Code] IN VALUES ( 'Table'[Product Code] )
    )
RETURN
    MAX ( 'Table'[Stock] ) - a

Output

vxinruzhumsft_2-1711334200997.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

Thank you @v-xinruzhu-msft 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ashish_Mathur
Super User
Super User

Hi,

Ensure there is a Calendar Table.  Create a relationship (Many to One and Single) from the Date column of the Data Table to the Date column of the Calnedar Table.  To your visual, drag Date from the Calendar Table.  Assuming you have already written measures of Stock and Quantity, write these measures:

Cumulative quantity = calculate([quantity],datesbetween(calendar[date],minx(all(calendar),calendar[date]),max(calendar[date])))

Diff = [Stock]-[Cumulative Quantity]

The Cumulative quantity measure will calculate the quantity from the very inception.

Hope this helps.

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.