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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Prov
Regular Visitor

Calculating Stock Level

Hi,
I'm pretty new to Power BI.
I'm trying to calculate the stock level of a product at any given date.
I've generated tables for when an item is put in stock and for any stock variations, appended them in power query and built a running total  (Variation and Stock Levels in my example file).

Prov_0-1636533357853.png


Then, to have the product levels at any given date, I built Dates and Products tables and Crossjoined them into an Output table.
What I'm failing to do is to feed correctly this table with inventory levels of each product for each date.

I've been trying to follow this post to reach a solution:
https://community.powerbi.com/t5/Desktop/Calculating-stock-level/m-p/1693852/highlight/true#M673067

Here is my sample file:
https://drive.google.com/file/d/1UWVSCX03KW7LXU8YaRvgYVreMHhSLD_x/view?usp=sharing

Hope anyone can help me, I'm pretty confused right now on why I'm not being able to get this right.

1 ACCEPTED SOLUTION
HotChilli
Super User
Super User

stock in the output table was written like a measure, not a calculated column.

I've made some changes which look ok but please test at your side:

stock = 
VAR _product = Output[Product]
VAR _date = Output[Date]
VAR _stLevDate = CALCULATE (
        MAX ( 'Stock Levels'[Date] ),
        'Stock Levels'[Date] <= _date,
        'Stock Levels'[Product] = _product
    )
VAR _stock =
    IF (
        ISBLANK ( _stLevDate ),
        0,
        CALCULATE (
            MAX ( 'Stock Levels'[Stock]),
            'Stock Levels'[Date] = _stLevDate,
            'Stock Levels'[Product] = _product
        )
    )
RETURN
    _stock

View solution in original post

2 REPLIES 2
HotChilli
Super User
Super User

stock in the output table was written like a measure, not a calculated column.

I've made some changes which look ok but please test at your side:

stock = 
VAR _product = Output[Product]
VAR _date = Output[Date]
VAR _stLevDate = CALCULATE (
        MAX ( 'Stock Levels'[Date] ),
        'Stock Levels'[Date] <= _date,
        'Stock Levels'[Product] = _product
    )
VAR _stock =
    IF (
        ISBLANK ( _stLevDate ),
        0,
        CALCULATE (
            MAX ( 'Stock Levels'[Stock]),
            'Stock Levels'[Date] = _stLevDate,
            'Stock Levels'[Product] = _product
        )
    )
RETURN
    _stock

This works! Thank you very much, now that I see the solution I understand my mistake.
Still need to learn a lot about DAX. Thanks!

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.