Forum Discussion

jefflynn's avatar
jefflynn
Frequent Visitor
1 year ago
Solved

Find previous row using index on a filtered table

I have the following table in Power BI named Query1. I want to add the CostToComplete column which adds the current EstCostAgg to previous EstCostAgg then subtracts current ActualCostAgg + the previ...
  • ArwaAldoud's avatar
    1 year ago

    Hi jefflynn 

    If I understand correctly, can you achieve this with a calculated column using DAX

    CostToComplete_Column =
    VAR PreviousRow =
    MAXX(
    FILTER(Query1, Query1[Index] < EARLIER(Query1[Index])),
    Query1[Index]
    )

    VAR PreviousEstCostAgg =
    LOOKUPVALUE(Query1[EstCostAgg], Query1[Index], PreviousRow)

    VAR PreviousActualCostAgg =
    LOOKUPVALUE(Query1[ActualCostAgg], Query1[Index], PreviousRow)

    RETURN
    IF(
    SEARCH("SUB LABOR", Query1[Description2], 1, 0) > 0,
    PreviousEstCostAgg + Query1[EstCostAgg] - (Query1[ActualCostAgg] + PreviousActualCostAgg),
    BLANK()
    )

    Find the seccenshot below 

    If this response was helpful, please accept it as a solution and give kudos to support other community members