Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      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
  • 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 )