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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
dnd588
Regular Visitor

Circular Dependency in Power Bi calculated column

Hi Community, 

I have been trying all sort of tricks with DAX, but just cannot get to a solution to my challenge. Would appreciate some help. 

I am trying to create an investment schedule in the Power BI data view. I have no. of daily purchase & sale units in the “number of units” column and daily purchase and sales amount in the “Market Value” column.

 

1. We need to calculate Changes in closing investment which is in case of

Sales Transaction = “Number of units with Direction” * “Previous Day NAV”

Purchase Transaction = Purchase/ Sale Value..

Daily Changes column will be used for calculating closing investments.

 

 

Changes in Cl invst = if('Investment Schedule'[Direction_4]=-1,'Investment Schedule'[Previous day NAV]*'Investment Schedule'[NOUWdirection],'Investment Schedule'[Purchase/ Sales Value]) 

 

 

 

 

2. Previous Day Weighted average cost is nothing else, but the last day weighted average cost

 

Previous day Weigh Avg Cost = 
Var Previousrow = CALCULATE(
    Max('Investment Schedule'[Rank]),
    Filter(
        ALLEXCEPT('Investment Schedule','Investment Schedule'[id_number]),
        'Investment Schedule'[Rank] < EARLIER('Investment Schedule'[Rank])
    ))
    Return
    CALCULATE(Sum('Investment Schedule'[Weigh Avg Cost]),FILTER(ALLEXCEPT('Investment Schedule','Investment Schedule'[id_number]),'Investment Schedule'[Rank] = Previousrow))
    //Previous day NAV is weighted average NAV of previous day.

 

 

 

 

 

3. Weighted Average cost = Closing investment/ Closing units

 

 

Weigh Avg Cost = 'Investment Schedule'[Closing Invst]/'Investment Schedule'[Closing units]
// It is divison of Closing investment by closing units.

 

 

 

4. For calculating the Closing units on the day end I have used the running total of “Number of units_with direction”

 

 

Closing units = SUMX (
    FILTER (
        'Investment Schedule',
        EARLIER ( 'Investment Schedule'[id_number]) = 'Investment Schedule'[id_number]
            && EARLIER ( 'Investment Schedule'[Rank]) >= 'Investment Schedule'[Rank]
    ),
    'Investment Schedule'[NOUWdirection]
)
//It is the runnning total of NOUWdirection

 

 

 

5. For calculating the Closing investment on the day end I have used running total of “Changes in Closing investment”.

 

 

Changes in Cl invst = if('Investment Schedule'[Direction_4]=-1,'Investment Schedule'[Previous day NAV]*'Investment Schedule'[NOUWdirection],'Investment Schedule'[Purchase/ Sales Value]) 

 

 

 

QUERY

I am having problem in calculating Changes in closing investment for day 2 & subsequent days as the input for calculating changes in closing investment is day 1 weighted average NAV which was calculated based on the changes in closing investment of Day 1 and causes a circular dependency. 

 

I am attaching a table with similar calculation & link for the powerbi report for your reference & to make it more clear. I am not able to compute daily changes in power bi but it can be computed in excel. 

 

Hoping for a positive response.

Thanks 

 

Link for powerbi report : https://drive.google.com/file/d/19Fps9BkRakwqYmr52-JDYpedumc69v-K/view?usp=drivesdk

 

Company CodeTransactionDateID_numberRankFlowDirection_4 Purchase/ Sales Value No. of unitsNOUW DirectionClosing units Price per unit  Changes in Closing Investment  Closing Investment  Weighted Average Cost  Previous Day Weighted Avg Cost  Cost of investment sold  Profit  ROI 
                   
20001101/04/2023ADITYABMF1Purchase 1       100,000.00100010001000              100.00        100,000.00                 100,000.00                                100.00                                -                                            -                -  0.00%
20001202/04/2023ADITYABMF2Sale-1         22,000.00200-200800              110.00         (20,000.00)                   80,000.00                                100.00                       100.00                          (20,000.00)  2,000.0010.00%
20001303/04/2023ADITYABMF3Purchase 1         31,500.003003001100              105.00          31,500.00                 111,500.00                                101.36                       100.00                                          -                -  0.00%
20001404/04/2023ADITYABMF4Sale-1         10,600.00100-1001000              106.00         (10,136.36)                 101,363.64                                101.36                       101.36                          (10,136.36)     463.644.57%
20001505/04/2023ADITYABMF5Sale-1         32,400.00300-300700              108.00         (30,409.09)                   70,954.55                                101.36                       101.36                          (30,409.09)  1,990.916.55%
20001606/04/2023ADITYABMF6Purchase 1         30,900.003003001000              103.00          30,900.00                 101,854.55                                101.85                       101.36                                          -                -  0.00%
20001707/04/2023ADITYABMF7Purchase 1         26,000.002502501250              104.00          26,000.00                 127,854.55                                102.28                       101.85                                          -                -  0.00%
20001808/04/2023ADITYABMF8Sale-1         75,600.00700-700550              108.00         (71,598.55)                   56,256.00                                102.28                       102.28                          (71,598.55)  4,001.455.59%

 

 

4 REPLIES 4
lbendlin
Super User
Super User

What's NAV?

 

Can your problem's sample data be reduced to this?

 

lbendlin_0-1682646447476.png

A circular reference in DAX can usually be avoided by implementing the logic in Power Query instead.

Hi @lbendlin 

We will also require Weighted Average NAV and daily changes column for further calculations.

NAV is value of each unit of investment. I am calculating weighted average NAV by dividing (Closing Investment) by (Closing no. of units).

 

I get rather different numbers:

 

lbendlin_0-1682781430775.png

 

"NAV cumul"  shows the NAV of the remaining units after each transaction.

 

Hi @lbendlin 

 

For your convenience, I've written the formulas in each column. I am encountering a circular dependency while calculating "Changes in closing investments" for "02/04/2023". Since the formula for calculating "Changes in closing investments" for "02/04/2023" is "NOUW Direction * weighted average cost of 01/04/2023," and weighted average cost of 01/04/2023 is determined by dividing "Closing Investment by Closing Units of 01/04/2023," 

Regards and thanks

Durgesh Gupta

*NOUW = Number of Units With

Company CodeTransactionDateID_numberFlowDirection_4 Purchase/ Sales Value No. of units Price per unit RankNOUW DirectionClosing units Changes in Closing Investment  Closing Investment  Weighted Average Cost  Previous Day Weighted Avg Cost  Cost of investment sold  Profit  ROI 
Formula    1= Purchase
-1 = Sales
 No of units purchased or sold  No of units × DirectionRunning total of "NOUW Direction" In case of :-
Purchase = Purchase/ Sales value
Sales = Previous day weighted avg cost" × NOUW Direction 
 Running Total of "Changes in closing investment"  Divison of "Closing investment" by "Closing units"  Previous day "Weighted average cost"  "Previous day weighted avg cost" × "NOUW Direction"  "Purchase/Sales Value" - "Cost of investment sold"  Division of "Profit" by "Cost of investment sold" 
                   
20001101/04/2023ADITYABMFPurchase 1       100,000.001000              100.00110001000                                           100,000.00                 100,000.00                                100.00                                -                                            -                                -  0.00%
20001202/04/2023ADITYABMFSale-1         22,000.00200              110.002-200800                                           (20,000.00)                   80,000.00                                100.00                       100.00                          (20,000.00)                  2,000.0010.00%
20001303/04/2023ADITYABMFPurchase 1         31,500.00300              105.0033001100                                             31,500.00                 111,500.00                                101.36                       100.00                                          -                                -  0.00%
20001404/04/2023ADITYABMFSale-1         10,600.00100              106.004-1001000                                           (10,136.36)                 101,363.64                                101.36                       101.36                          (10,136.36)                     463.644.57%
20001505/04/2023ADITYABMFSale-1         32,400.00300              108.005-300700                                           (30,409.09)                   70,954.55                                101.36                       101.36                          (30,409.09)                  1,990.916.55%
20001606/04/2023ADITYABMFPurchase 1         30,900.00300              103.0063001000                                             30,900.00                 101,854.55                                101.85                       101.36                                          -                                -  0.00%
20001707/04/2023ADITYABMFPurchase 1         26,000.00250              104.0072501250                                             26,000.00                 127,854.55                                102.28                       101.85                                          -                                -  0.00%
20001808/04/2023ADITYABMFSale-1         75,600.00700              108.008-700550                                           (71,598.55)                   56,256.00                                102.28                       102.28                          (71,598.55)                  4,001.455.59%
                                        (132,144.00)                  8,456.006.40%

 

dnd588_0-1682918801127.png

 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.

Top Solution Authors
Top Kudoed Authors