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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
YoannC
New Member

Start Stock and End stock line by line

Hello everyone,

 

I need your help.

i've this kind of table where start stock and end stock need to be calculate.

 

LineWeekStart StocksalesreturnEnd Stock
145622182
2462   
347    

 

- Start Stock is 6 for first line and after it's the value on the previous line of end stock.

- End Stock is Start Stock - Sales + Return of each line.

 

I try a lot of things 😞

 

Thanks for your Help 🙂

1 ACCEPTED SOLUTION
gmsamborn
Super User
Super User

Hi @YoannC 

 

Try these measures and take a look at the example pbix below.

 

Sales = SUM( 'Table'[Sales] )

Returns = SUM( 'Table'[Returns] )

Start Stock = 
VAR _1stWkStock =
    CALCULATE(
        SUM( 'Table'[Start Stock] ),
        'Table'[Week] = MIN( 'Table'[Week] )
    )
VAR _CurrWk = SELECTEDVALUE( 'Table'[Week] )
VAR _PrevWkTD =
    CALCULATE(
        [Returns] - [Sales],
        FILTER(
            ALLSELECTED( 'Table' ),
            'Table'[Week] < _CurrWk
        )
    )
VAR _Result = 
    IF(
        HASONEVALUE( 'Table'[Week] ),
        _1stWkStock + _PrevWkTD
    )
RETURN
    _Result

End Stock = [Start Stock] + [Returns] - [Sales]

 

 

Start-stock and End-stock.pbix

 

Since DAX can't easily look up the previous row like Excel, these calculations are done differently.  So [Start Stock] can't refer to [End Stock] since [Start Stock] is used in the calculation of [End Stock]. (circular)

 

I'm really having a tough time putting this into words.

 

Maybe somone else can help with the explanation.

 



Proud to be a Super User!

daxformatter.com makes life EASIER!

View solution in original post

1 REPLY 1
gmsamborn
Super User
Super User

Hi @YoannC 

 

Try these measures and take a look at the example pbix below.

 

Sales = SUM( 'Table'[Sales] )

Returns = SUM( 'Table'[Returns] )

Start Stock = 
VAR _1stWkStock =
    CALCULATE(
        SUM( 'Table'[Start Stock] ),
        'Table'[Week] = MIN( 'Table'[Week] )
    )
VAR _CurrWk = SELECTEDVALUE( 'Table'[Week] )
VAR _PrevWkTD =
    CALCULATE(
        [Returns] - [Sales],
        FILTER(
            ALLSELECTED( 'Table' ),
            'Table'[Week] < _CurrWk
        )
    )
VAR _Result = 
    IF(
        HASONEVALUE( 'Table'[Week] ),
        _1stWkStock + _PrevWkTD
    )
RETURN
    _Result

End Stock = [Start Stock] + [Returns] - [Sales]

 

 

Start-stock and End-stock.pbix

 

Since DAX can't easily look up the previous row like Excel, these calculations are done differently.  So [Start Stock] can't refer to [End Stock] since [Start Stock] is used in the calculation of [End Stock]. (circular)

 

I'm really having a tough time putting this into words.

 

Maybe somone else can help with the explanation.

 



Proud to be a Super User!

daxformatter.com makes life EASIER!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.