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 previous ActualCostAgg. The current row is found where Description2 contains "SUB LABOR". The previous row will never contain “SUB LABOR”. The Index values are not sequential, there are gaps due to filters on the visual. The index values are sorted in ascending order. 

PhaseDescription2EstCostAggCostToCompleteActualCostAggIndex
031006.  1DRILLED PIERS - PLACING18614 17267.431741
031006.  3DRILLED PIERS - PLACING (SUB LABOR)0-10687.9312034.51747
031010.  1DRILLED PIERS - FORM INSTALL/STRIP3597 01781
031106.  1FOOTINGS - PLACING/FINISHING31360 4520.371989
031106.  3FOOTINGS - PLACING/FINISHING (SUB LABOR)015479.63113602005
031108.  1FOOTINGS - FORM INSTALL/STRIP23846.5 3336.22037
031108.  3FOOTINGS - FORM (SUB LABOR)0 19719.52080
031126.  1GRADE BEAMS - PLACING/FINISHING10599 2087.42258
031126.  3GRADE BEAMS - PLACING/FINISHING (SUB LABOR)01477.670342268
031130.  1GRADE BEAMS - FORM INSTALL/STRIP57881 14216.212296
031130.  3FOOTINGS (GRADE BEAMS) - FORM SIDES (SUB LABOR)05812.79378522309

 

 

 

  • 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

4 Replies

  • 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

  • Hi,

    Not very clear with what you want.  Share the download link of an Excel file with your excel formula written there.  I will understand the logic from there and convert those formulas to measures in PowerBI.