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
OytunKarabulut
Frequent Visitor

Circular Reference Error for an Iterative Formula Applied Over Different Rows of the Same Column

Hi all,

I'm trying to build an iterative logic with DAX to calculate Opening and Closing Stocks for specific material & rank combinations. How it should go is that;
- first, I vlookup the Opening Stock value for Rank 1 from a different table. 
- then, I subtract the Consumption from the Opening Stock to come up with the Closing Stock. The Consumptions are dependent on the request, as well as the availability of the opening stock.
- lastly, I need to assign the Closing Stock of Rank 1 as the Opening Stock of Rank 2, and then the same calculation of Closing Stock should happen for Rank 2.

- and, Rank 2 Closing Stock should be equal to Rank 3 Opening Stock, and so on and so forth...

OytunKarabulut_0-1677246281120.png

 

I've created a basic example in Excel where I can refer to individual cells. However, I face a circular reference error when I try to build the same with DAX in Power Pivot or Power BI. I know that DAX calculations are done for each column (and not for each individual cell), however, I still wanted to ask the same to find out if there are any workarounds for this.

When I try to build the same in the Data Modal of the Test File below, I face the circular reference error.

OytunKarabulut_0-1677246806485.png


You can see this test file below. The DAX formulas are in the Data Model through the Power Pivot tab.

Test File.xlsx

Thank you very much for your support everyone,
Oytun

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @OytunKarabulut 

Recursion is not possible in DAX, as neither measures nor calculated columns can refer to themselves (indirectly or directly).

 

However, for this particular calculation, you can use a cumulative sum to produce the same results.

Alternatively, you may want to consider calculating this in Power Query with List.Generate.

 

I have edited your file and attached an example of how this can be done in DAX.

 

Opening Stock =
VAR CurrentRank = Table1[Rank]
VAR CurrentMaterial = Table1[Material]
VAR OpeningStockLookup =
    CALCULATE (
        SUM ( Table2[Opening Stock] ),
        Table2[Material] = CurrentMaterial
    )
VAR CumulativeConsumption =
    CALCULATE (
        SUM ( Table1[Consumption] ),
        ALLEXCEPT (
            Table1,
            Table1[Material]
        ),
        Table1[Rank] < CurrentRank
    )
RETURN
    OpeningStockLookup - CumulativeConsumption
Closing Stock =
Table1[Opening Stock] - Table1[Consumption]

 

  • Opening Stock subtracts cumulative Consumption (excluding current Rank) from the original Opening Stock from Table2.
  • Closing Stock subtracts Consumption for current Rank from Opening Stock.

There are some possible variations on the DAX code above, but the logic should be similar regardless.

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi @OwenAuger ,

In the first version of the problem I've written, your solution is applicable, however, when I include another formula to the consumption column, then again I'm stuck with the circular reference error. Can you check this again?

Thanks a lot,

Oytun

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.