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
Anonymous
Not applicable

Circular dependency error while creating two inter dependent columns from other columns

Hi to all the members of enthusiastic and supportive community.

 

I have to create two columns "closing stock" and "opening stock" columns based on "date", "purchased stock", and "sold stock".

The data sample is like below:

h_padal_0-1612009856708.png

 

I need to get the table with calculated columns as below using DAX (in Power BI).

h_padal_1-1612009871817.png


I have used these formulae to create calculated columns but causing Circular Dependency Error.

Opening Stock = 
VAR PreviousRow =
TOPN (
1,
FILTER (
'Table1',
'Table1'[Date] < EARLIER ( 'Table1'[Date] )
),
'Table1'[Date], DESC
)
VAR PreviousValue =
IF(
MINX ( PreviousRow, 'Table1'[Closing Stock] ) = BLANK(),
[Purchased Stock],
MINX ( PreviousRow, 'Table1'[Closing Stock] )
)
RETURN
PreviousValue

---------------------------------

Closing Stock = IF(
[Purchased Stock] <> BLANK(),
[Purchased Stock] + [Opening Stock] - [Sold Stock],
[Opening Stock] - [Sold Stock]
)

I was able to get the logic for Opening Stock column from @Zubair_Muhammad. @Zubair_Muhammad, it would be great if you or anyone from the community helped me obtain the solution.

 

Please help me to arrive to a solution..

Thanks in Advance.

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Please try these column expressions instead, replacing StockSales with your actual table name.

 

Opening Stock = var vThisDate = StockSales[Date]
var vPurchases = CALCULATE(SUM(StockSales[Purchased Stock]), All(StockSales), StockSales[Date] <= vThisDate)
var vSales = CALCULATE(SUM(StockSales[Sold Stock]), all(StockSales), StockSales[Date] < vThisDate)
return vPurchases - vSales
 
Closing Stock = var vThisDate = StockSales[Date]
var vPurchases = CALCULATE(SUM(StockSales[Purchased Stock]), all(StockSales), StockSales[Date] <= vThisDate)
var vSales = CALCULATE(SUM(StockSales[Sold Stock]), All(StockSales), StockSales[Date] <= vThisDate)
return vPurchases - vSales
 
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
mahoneypat
Microsoft Employee
Microsoft Employee

Please try these column expressions instead, replacing StockSales with your actual table name.

 

Opening Stock = var vThisDate = StockSales[Date]
var vPurchases = CALCULATE(SUM(StockSales[Purchased Stock]), All(StockSales), StockSales[Date] <= vThisDate)
var vSales = CALCULATE(SUM(StockSales[Sold Stock]), all(StockSales), StockSales[Date] < vThisDate)
return vPurchases - vSales
 
Closing Stock = var vThisDate = StockSales[Date]
var vPurchases = CALCULATE(SUM(StockSales[Purchased Stock]), all(StockSales), StockSales[Date] <= vThisDate)
var vSales = CALCULATE(SUM(StockSales[Sold Stock]), All(StockSales), StockSales[Date] <= vThisDate)
return vPurchases - vSales
 
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi Pat ( @mahoneypat ), Thanks for the help recently.

 

I have a scenario where, the data is like this...

h_padal_1-1612235226581.png

which is clear that the purchased stock value is not available. But can be obtained and accounted for manually with the help of inventory department (which cannot be obtained while creating the above table). and for the first row in opening stock I can enter the opening stock value manually in Excel (as in the image below) but the same thing when I did in PowerBI, the things are breaking. and getting the negative values.

 

h_padal_0-1612235165177.png

 

Moreover, there are categories of stock to be accounted in this data, for example there are 3 categories of stock, whose count need to be tracked on perday basis. (not mandatory that each category of stock is sold on a particular date and also not mandatory that when stock is purchased all categories of stock are purchased on a particular date).

 

I would be very thankful if you could help me obtain solution for these. And help from any community members is verymuch invited.

 

Thanks in Advance.

Would this approach work, adding in an initial value?  It is hardcoded below but it could be a dynamic expression with a value from another table you bring in.

 

Opening Stock =
VAR vInitValue = 100 // or use dynamic expression
VAR vThisDate = StockSales[Date]
VAR vPurchases =
    CALCULATE (
        SUM ( StockSales[Purchased Stock] ),
        ALL ( StockSales ),
        StockSales[Date] <= vThisDate
    )
VAR vSales =
    CALCULATE (
        SUM ( StockSales[Sold Stock] ),
        ALL ( StockSales ),
        StockSales[Date] < vThisDate
    )
RETURN
    vInitValue + vPurchases - vSales

 
Pat




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks for the suggestion Pat ( @mahoneypat  ).

It certainly helped me and there were a few corrections to be made at other (prior) stages in my data. Now result is as expected after dealing with those corrections and changes.

Anonymous
Not applicable

That worked like a charm. Thanks a lot @mahoneypat.

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.