Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

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

  • What's NAV?

     

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

     

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      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).

       

      • lbendlin's avatar
        lbendlin
        Super User

        I get rather different numbers:

         

         

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