Forum Discussion

Rozers's avatar
Rozers
Frequent Visitor
1 year ago
Solved

Circular dependency error in Measures

Hi Everyone, Good day! In my report, I need to calculate COGS. I have the COGS value at the beginning of January 2023, from which we determine the COGS at the end of the month. For the foll...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi Rozers ,

    The circular dependency error occurs because the measures COGS Value at the End of the Month and COGS Value at the Start of the Month are referencing each other. This creates a loop where each measure depends on the other, making it impossible for Power BI to calculate the values.

     

    To resolve this, you need to break the circular reference. One way to do this is by using a different approach to calculate the COGS Value at the Start of the Month without directly referencing COGS Value at the End of the Month.

     

    So you can use these DAX codes below:

    PrevMonthCOGS = 
    CALCULATE(
        [COGS Value at the End of the Month],
        FILTER(
            ALL(Dim_Date),
            Dim_Date[YearMonth] = MAX(Dim_Date[YearMonth]) - 1
        )
    )
    COGS Value at the Start of the Month =
    VAR JanCOGS = SUM(COGS[start of the month cogs])
    RETURN
        IF( MAX(Dim_Date[YearMonth]) = 202301, JanCOGS, [PrevMonthCOGS] )
    COGS Value at the End of the Month =
    VAR Summary =
        SUMMARIZE(
            product,
            product[product_code],
            product[product_name],
            "RequiredQty", SUM(po[required_quantity]),
            "PlannedQty", SUM(po[planned_quantity]),
            "Wtavg", [Wt Avg PP],
            "Stock", [Starting_stock],
            "PrevMonthCOGS", [PrevMonthCOGS]
        )
    
    VAR Cal =
        SUMX(
            Summary,
            DIVIDE( ([Stock]*[PrevMonthCOGS]) +  ([Wtavg]*[RequiredQty]),
                    ([Stock] + [RequiredQty])
            )
        )
    RETURN
    Cal

     

    If this is not what you want either, please provide relevant example data for better research.

     

     

     

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.