Forum Discussion

D_Gibson's avatar
D_Gibson
Frequent Visitor
3 years ago
Solved

Starting and closing balances generate circular reference

I am attempting to build a report in Power BI to display inventory balances for selected SKUs for the current day through to 14 days into the future. On any given day I am interested in calculating p...
  • D_Gibson's avatar
    D_Gibson
    3 years ago

    Thank you again Jianbo. Although I'm not an expert on the DAX language I know that row-context is a fundamental part of the DAX language, in which case row co-ordinates are important. Whenever a measure is placed in a table it will be evaluated against each row in the table according to any defined relationships. It is not simply calculated once for the column or table. 

     

    Perhaps it would be more accurate to say that 'DAX does not evaluate row-context before deciding whether there is a circular dependency'. 

     

    If DAX were to evaluate row-context there would be no dependency between my StartingBalanceMeasure and PreviousDayClosingBal.

     

    In any case, a colleague has suggested a different approach and it has done the job nicely. Essentially, starting and closing balance are calculated independently of each other using the VAR function. A total of 9 lines of code is required for each. Below is the starting balance calculation: 

     

    Weight (Starting Balance) =
    VAR vD = MAX('Date (Reporting)'[Date])
    Var vBalance = Calculate(SUM('Balances'[Weight]),'Balances'[Source] = "Inventory")
    var vProd = Calculate(SUM('Balances'[Weight]),'Balances'[ArrvlDate] < vD, 'Balances'[Source] = "Production")
    var vSupply = CALCULATE([Weight (Supply)], ALL('Date (Reporting)'),'Date (Reporting)'[Date] < vD)
    var vTransOut = CALCULATE([Weight (Transfers Out)],ALL('Date (Reporting)'),'Date (Reporting)'[Date] < vD)
    var vTransIn = CALCULATE([Weight (Transfers In)],ALL('Date (Reporting)'),'Date (Reporting)'[Date] < vD)
    var vExpire = CALCULATE([Weight (Expire)],ALL('Date (Reporting)'),'Date (Reporting)'[Date] < vD)
     return vBalance + vProd -vSupply +vTransIn- vTransOut - vExpire