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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
naoyixue1
Post Patron
Post Patron

How to do running total calculation with zero out negative value from last time period

Hey there,

 

I'm working on one project to calculate the ending inventory. 

https://app.powerbi.com/links/60g4xsKrxq?ctid=59034451-9b9d-4095-95cb-cb280da28cc6&pbi_source=linkSh...

naoyixue1_0-1740447877336.png

 

Instead of table A's way of calculation, I want to use table B, which will zero out last time ending inventory, if it's negative. In other words, it will take 0 as starting point to calculate this week ending inventory, instead of taking the last week negative ending inventory as the initial value.

 

Ending Inventory = Beginning Inv (last week ending Inv) + Supply - Demand

 

I tried the dax, but it seems that doesn't work. Would you help to take a look into that? 

 

6 REPLIES 6
naoyixue1
Post Patron
Post Patron

Hey all, to make it easy I created a excel tabl here at below link. 

 

All columns highligted in yellow are the raw data. I just added another two calculated columns there in blue, which I want to calculate in Power BI. Actually, I just want to calculate the ideal ending inventory, which is on hand + supply - demand. And then carry over currnet week ending inventory to next week as the beginning inventory, if current ending inventory is greater than 0. If it's negative, I will use 0 as the starting inventory in that week to repeat the calculation. Due to that approach, we will see the differnce. (see below example)

Item A:

Week: 4/30/2025

Week: 5/7/2025

Normal Calculation:

 

Ending Inventory in the week of 4/30 = Last week ending invnetory (16 in the week of 4/23/2025) - demands in the week of 4/30 (37) + supply in the week of 4/30 (20) = 16 - 37 +20 = -1

 

Ending Inventory in the week of 5/7 = Last week ending invnetory (-1 in the week of 4/30/2025) - demands in the week of 5/7 (14) + supply in the week of 5/7 (21) = -1-14+21 = 6

 

 

Ideal Calculation:

 

Ending Inventory in the week of 4/30 = Last week ending invnetory (16 in the week of 4/23/2025) - demands in the week of 4/30 (37) + supply in the week of 4/30 (20) = 16 - 37 +20 = -1

 

Since -1 is less than 0, so I will take 0 as the beginning inventory in the week of 5/7

 

Ending Inventory in the week of 5/7 = Last week ending invnetory (0 in the week of 4/30/2025) - demands in the week of 5/7 (14) + supply in the week of 5/7 (21) = 0-14+21 = 7

naoyixue1_0-1741030279727.png

 

Would you let me know how I can achieve that calculation in Power BI? Thanks a lot!


https://docs.google.com/spreadsheets/d/1eKMw9d6sxwJEusqXo0Rt2mSLcHKDZ-9L/edit?usp=sharing&ouid=10682...

sevenhills
Super User
Super User

when clicked the file link: getting error:

Selected user account does not exist in tenant 'Pharmavite LLC' and cannot access the application '871c010f-5e61-4fb1-83ac-98610a7e9110' in that tenant. The account needs to be added as an external user in the tenant first. Please use a different account.

 

Just remove the sensitive data and have some sample data and provide the link/file here ... 

I worked on manually typing using your image data:

Data table Input:

sevenhills_0-1740470803182.png

 

Added measures: I added lot more measures to go through the data! Remove those not needed! 

 

 

 

 MEASURE 'Table1'[Day Supply] = sum(Table1[Supply])

    MEASURE 'Table1'[Day Demand] = sum(Table1[Demand])

    MEASURE 'Table1'[Day Balance Adjusted] = [Day Supply] - [Day Demand]  

	MEASURE 'Table1'[CumulativeSupply] = CALCULATE(SUM('Table1'[Supply]), FILTER( ALL('Table1'), 'Table1'[Date] <= MAX('Table1'[Date])))

	MEASURE 'Table1'[CumulativeDemand] = CALCULATE(SUM('Table1'[Demand]), FILTER( ALL('Table1'), 'Table1'[Date] <= MAX('Table1'[Date]))) 
  
MEASURE 'Table1'[Adjusted Beginning Balance] =  
	VAR CurrentDate = MAX('Table1'[Date])
	VAR PreviousDate = CALCULATE(MAX('Table1'[Date]), FILTER(ALL('Table1'), 'Table1'[Date] < CurrentDate))
	RETURN IF( ISBLANK(PreviousDate), 0,
        VAR PreviousClosingBalance = 
            SUMX(
                FILTER( ALL('Table1'), 'Table1'[Date] = PreviousDate),
                VAR _DayBeforeDate = CALCULATE(MAX('Table1'[Date]), FILTER(ALL('Table1'), 'Table1'[Date] < PreviousDate))
				VAR _TempCalc1 = CALCULATE([Day Balance Adjusted], FILTER(ALL('Table1'), 'Table1'[Date] = _DayBeforeDate))   
                VAR PreviousBeginningBalance = IF(ISBLANK(_DayBeforeDate), 0, IF( _TempCalc1 > 0,_TempCalc1, 0) )
                RETURN PreviousBeginningBalance + CALCULATE([Day Balance Adjusted], FILTER(ALL('Table1'), 'Table1'[Date] = PreviousDate))
            )
        RETURN
            IF( PreviousClosingBalance > 0, PreviousClosingBalance, 0 )
    )
	
	
	
	
	MEASURE 'Table1'[ClosingBalance] = [Adjusted Beginning Balance] + [Day Balance Adjusted] 
	 
	MEASURE 'Table1'[BeginningBalance] = -- Unadjusted Beginning Balance = 
		var _sel = MAX( Table1[Date] )
		var _calc2 = SUMX( FILTER( all('Table1'), Table1[Date] < _sel), COALESCE([Day Balance Adjusted], 0) )
		return COALESCE(_calc2, 0)
		

 

 

 

 

Sample output:
Regular matrix:
sevenhills_2-1740470902793.png

Matrix ... switch values to rows: 

sevenhills_3-1740470998304.png

 

sevenhills_1-1740470873775.png

 

hope this helps!

@sevenhills I tried, but still doesn't work. I just reuploaded my file to the google drive and shared the link. Would you mind checking that for me? Thank you so much!

Example of Ending Inv Calculation.pbix

@sevenhills  Any thoughts? Thanks!

@sevenhills Thanks a lot. I will try that approach and accept that as a soltuon if it works. Again, thank you so much!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.