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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
PBI_newuser
Post Prodigy
Post Prodigy

How to calculate days difference based on slicer selection

Hi, I have a table below. I want to calculate the mean time between jobs based on serial number and task codes selected. For example, when I do not select any Task Code, the table will sort Serial Number and Restoration Date in ascending order, then calculate the days difference between current Notified Date and previous job's Restoration Date. If I select Task Code = T113, the data will rearrange based on Serial Number and Notified Date then calculate the days difference.

 

How to create such measure to calculate the days difference based on task codes selected?

 

Table:

Job NumberSerial NumberTask CodeNotified DateRestoration Date
Job-1SN-1T11001-Jan05-Jan
Job-2SN-1T11205-Jan07-Jan
Job-3SN-1T11311-Jan12-Jan
Job-4SN-1T11320-Jan03-Feb
Job-5SN-1T11222-Jan03-Mar
Job-6SN-1T11027-Jan28-Jan
Job-7SN-2T11201-Feb04-Feb
Job-8SN-2T11214-Feb04-Mar

 

Expected Output when Task Code is all selected:

Job NumberSerial NumberTask CodeNotified DateRestoration DatePrev Restoration DateDate Diff = Notified Date - Prev Restoration Date
Job-1SN-1T11001-Jan05-Jan  
Job-2SN-1T11205-Jan07-Jan05-Jan0
Job-3SN-1T11311-Jan12-Jan07-Jan4
Job-4SN-1T11027-Jan28-Jan12-Jan15
Job-5SN-1T11320-Jan03-Feb28-Jan-8
Job-6SN-1T11201-Feb04-Feb03-Feb-2
Job-7SN-2T11222-Jan03-Mar  
Job-8SN-2T11214-Feb04-Mar03-Mar-17

 

Expected Output when Task Code=T113 is selected:

Job NumberSerial NumberTask CodeNotified DateRestoration DatePrev Restoration DateDate Diff = Notified Date - Prev Restoration Date
Job-3SN-1T11311-Jan12-Jan  
Job-4SN-1T11320-Jan03-Feb12-Jan8
3 REPLIES 3
Ahmedx
Super User
Super User

Day = 
VAR _MinDate =
    CALCULATE (
        MIN ( 'Table'[Restoration Date] ),
        OFFSET (-1,SUMMARIZE (ALLSELECTED ( 'Table' ),
                'Table'[Job Number],
                'Table'[Serial Number],
                'Table'[Task Code],
                'Table'[Notified Date],
                'Table'[Restoration Date]
            ),
            ORDERBY ( 'Table'[Job Number] ),
            KEEP,
            PARTITIONBY ( 'Table'[Serial Number] )
        )
    )
VAR _Date =
    IF ( NOT ISBLANK ( _MinDate ), _MinDate, MAX ( 'Table'[Notified Date] ) )
RETURN
    DATEDIFF ( MAX ( 'Table'[Notified Date] ), _Date, DAY )
Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
https://1drv.ms/u/s!AiUZ0Ws7G26Rhk2uQYUTV9rgFi4q?e=wUR95a
Screen Capture #683.pngScreen Capture #684.png

Hi @Ahmedx , how to calculate the day difference between the current Notified Date and Restoration Date of last job? I need to sort it by serial number and restoration date then calculate the dates difference like the example below?

Job NumberSerial NumberTask CodeNotified DateRestoration DatePrev Restoration DateDate Diff = Notified Date - Prev Restoration Date
Job-3SN-1T11311-Jan12-Jan  
Job-4SN-1T11320-Jan03-Feb12-Jan8

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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