Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
jefflynn
Frequent Visitor

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

 

jefflynn_0-1741379022455.png

 

 

1 ACCEPTED SOLUTION
ArwaAldoud
Super User
Super User

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 

ArwaAldoud_1-1741383678305.png

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

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

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.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
jefflynn
Frequent Visitor

Thank you. The numbers are off:

jefflynn_0-1741388768802.png

 

ArwaAldoud
Super User
Super User

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 

ArwaAldoud_1-1741383678305.png

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

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.