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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.