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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
GunnerJ
Post Patron
Post Patron

Date difference with sorting criteria

I need to find the time it takes to complete a task in a given service order. It'll effectively look to see if the Service Order Number is the same then look at all Sequence Numbers that have the same value and subtract the latest EVENT_DATE_TM from the earliest EVENT_DATE_TM to determine how long that Sequence number took to complete. This would only look at rows where "BI_WORK_EVENT_CD = "COMP"

 

*This code is similar to what I need and I think it is a good starting point but I'm having trouble converting it to the new set of criteria.*

Test Time = VAR temp =
TOPN ( 1, FILTER (
Workflow, Workflow[BI_SO_NBR] = EARLIER ( Workflow[BI_SO_NBR] )
&& Workflow[BI_EVENT_DT_TM] < EARLIER ( Workflow[BI_EVENT_DT_TM] ) ),
Workflow[BI_SO_NBR], DESC, Workflow[BI_EVENT_DT_TM], DESC, Workflow[BI_WRKFLW_TASK_SEQ_NBR], DESC )
RETURN IF ( ISEMPTY ( temp ), 0, DATEDIFF ( Workflow[BI_EVENT_DT_TM], MINX ( temp, Workflow[BI_EVENT_DT_TM] ), SECOND ) ) * -1

 

Here is a look at some sample data. As an example, when looking at lines 2,3, and 4 you can see the BI_SO_NBRs are the same as are the BI_WRKFLW_TASK_SEQ_NBRs. The formula would return the difference between lines 4 and 2 since they are the largest date and smallest date of that SO_NBR and SEQ_NBR subset.

Task Time.PNG

If anyone is able to help I would be greatly appreciative of your efforts. I'm still learning Dax but am trying to complete what seems to be a more difficult task.

 

Thank you!

2 REPLIES 2
dax
Community Support
Community Support

Hi GunnerJ,

Did you want to get result like below?

21.png

If so, you could try to use below expression to see whether it works or not(you could replace the “day” to “second” in datediff function)

Column 2 =
CALCULATE (
    DATEDIFF (
        MIN ( Workflow[BI_EVENT_DT_TM] ),
        MAX ( Workflow[BI_EVENT_DT_TM] ),
        DAY
    ),
    FILTER (
        ALL ( Workflow ),
        Workflow[BI_WRKFLW_TASK_SED_NBR] = EARLIER ( Workflow[BI_WRKFLW_TASK_SED_NBR] )
            && Workflow[BI_SO_NBR] = EARLIER ( Workflow[BI_SO_NBR] )
            && Workflow[BI_TASK_CD] = EARLIER ( Workflow[BI_TASK_CD] )
    )
)

Best Regards,

Zoe Zhi

 

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

@dax Thank you very much for the response. Your code does show me the correct times from the largest to smallest date.

 Task Times.PNG

I do have one question if you don't mind taking a look. When looking at the average time for task times I'm not seeing what I'd expect. The result I'd be looking for would be 121 seconds as that'd be the average between 122 and 120 seconds, but this shows 91.6 seconds for some reason.

Task Time Average Incorrect.PNG

I also was thinking if the values stay as they are I still might not get the exact average since an average would be looking at 5 values instead of just 2. Is there a way that you know of where the code can bring back the correct value but only for one row and leave the other's blank? That way it's able to caculate the true average.

 

Again thank you so much for your response you've gotten me SOOO much closer to my desired end result!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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