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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Finding value on another row in the same table depending on date

Hi,

 

I have a work order table looking like this:

Work order IDInstallation site IDWork order typeWork order date
Workorder 1Site AInstallation2020-01-01
Workorder 2Site BInstallation2020-01-01
Workorder 3Site CInstallation2020-01-01
Workorder 4Site AInspection2020-01-02
Workorder 5Site BInspection2020-01-02
Workorder 6Site AInspection2020-01-05
Workorder 7Site AInstallation2020-01-05

 

I am looking to find a calculated column that for all work order types "Inspections" to return the latest "Installation" work order ID such as:

 

Work order IDInstallation site IDWork order typeWork order dateInspected Workorder
Workorder 1Site AInstallation2020-01-01 
Workorder 2Site BInstallation2020-01-01 
Workorder 3Site CInstallation2020-01-01 
Workorder 4Site AInspection2020-01-02Workorder 1
Workorder 5Site BInspection2020-01-02Workorder 2
Workorder 6Site AInspection2020-01-05Workorder 1
Workorder 7Site AInstallation2020-01-05 

 

 

I am struggling in finding a good solution. Note that multiple work orders can be found on the same installation site and also multiple inspections on the same site. 

 

Any good pointers/tips?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

[Installation Workorder] = // calculated column
if( T[Work order type] = "inspection",
    // Find the most recent workorder
    // for the same site where order type
    // is equal to "installation". This will
    // work OK only on condition that the
    // workorders can be unambiguously ordered
    // on the time axis.
    // Bear in mind that T[Work order date]
    // must be of the datetime/date type, not
    // a string.
    var __currentOrderDate = T[Work order date]
    var __currentSite = T[Installation site]
    var __potentialWorkorders =
        topn(1,
            filter(
                T,
                T[Installation site] = __currentSite
                &&
                T[Work order type] = "installation"
                &&
                T[Work order date] < __currentOrderDate
            ),
            T[Work order date],
            DESC
        )
    var __error =
        // Either there must be one order for the
        // above conditions or no such order. If
        // there is more than 1 such order, it's
        // an error condition. It might also be
        // that if there's no such order it's an
        // error condition. Then you have to change
        // this logical condition to suit your needs.
        // Bear in mind that you can also raise an
        // error with the ERROR() function if you
        // wish.
        COUNTROWS( __potentialWorkorders ) > 1
    var __workorder =
        if( not __error,
            MAXX(
                __potentialWorkorders,
                T[Work order ID]
            ),
            "not possible to determine"
        )
    return
        __workorder
)

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Works like a charm, thanks for the quick reply and awesome dax! 

Anonymous
Not applicable

[Installation Workorder] = // calculated column
if( T[Work order type] = "inspection",
    // Find the most recent workorder
    // for the same site where order type
    // is equal to "installation". This will
    // work OK only on condition that the
    // workorders can be unambiguously ordered
    // on the time axis.
    // Bear in mind that T[Work order date]
    // must be of the datetime/date type, not
    // a string.
    var __currentOrderDate = T[Work order date]
    var __currentSite = T[Installation site]
    var __potentialWorkorders =
        topn(1,
            filter(
                T,
                T[Installation site] = __currentSite
                &&
                T[Work order type] = "installation"
                &&
                T[Work order date] < __currentOrderDate
            ),
            T[Work order date],
            DESC
        )
    var __error =
        // Either there must be one order for the
        // above conditions or no such order. If
        // there is more than 1 such order, it's
        // an error condition. It might also be
        // that if there's no such order it's an
        // error condition. Then you have to change
        // this logical condition to suit your needs.
        // Bear in mind that you can also raise an
        // error with the ERROR() function if you
        // wish.
        COUNTROWS( __potentialWorkorders ) > 1
    var __workorder =
        if( not __error,
            MAXX(
                __potentialWorkorders,
                T[Work order ID]
            ),
            "not possible to determine"
        )
    return
        __workorder
)

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.