Reply
Regular Visitor
Posts: 22
Registered: ‎07-25-2017
Accepted Solution

Calculate days between 2 events in inspection records

Hi Experts

 

I need to calculate the days between 2 events. It relates vehicles, and inspections are done weekly, but the vehicles are only sometimes serviced. When a vehicle is serviced, i need to calculate the days since it's last service.

 

My data looks as follows:

 

Vehicle   Date Inspected    Serviced    Days since previous service

  A           date 1                   Y              

  B           date 2                   N  

  A           date 3                   N

  B           date 4                   N

  A           date 5                   Y             date 5 - date 1

  B           date 6                  N

  A           date 7                  Y              date 7 - date 5

 

Now the DATEDIFF part is no problem. My problem is knowing how to find the PREVIOUS date serviced, for the vehicle.

 

My formula for trying to find the previous date currently looks like this:

 

Prev Service Date = 

     CALCULATE (

            MIN (table [Date Inspected] ),

            ALLEXCEPT (Table, Table[Vehicle]),

            FILTER (Table, Table[Date Inspected] <= Table[Date Inspected] && Table[Serviced]="Y"))

 

I do not get an error... but I get the same value for all the rows in the table

 

Any help will be much appreciated

 

 

 

 

 


Accepted Solutions
Super User
Posts: 1,713
Registered: ‎05-10-2016

Re: Calculate days between 2 events in inspection records

@roelf

 

 

Hi, try with this calculated column:

 

Days since Prev Service =
VAR DateInspected = Tabla1[Date Inspected]
VAR PreviosDay =
    CALCULATE (
        MAX ( Tabla1[Date Inspected] ),
        FILTER (
            Tabla1,
            Tabla1[Date Inspected] < DateInspected
                && Tabla1[Vehicle] = EARLIER ( Tabla1[Vehicle] )
                && Tabla1[Serviced] = "Y"
        )
    )
RETURN
    IF ( Tabla1[Serviced] = "Y", DATEDIFF ( PreviosDay, DateInspected, DAY ) )

Regards

 

Victor




Lima - Peru
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!




View solution in original post


All Replies
Super User
Posts: 1,713
Registered: ‎05-10-2016

Re: Calculate days between 2 events in inspection records

@roelf

 

 

Hi, try with this calculated column:

 

Days since Prev Service =
VAR DateInspected = Tabla1[Date Inspected]
VAR PreviosDay =
    CALCULATE (
        MAX ( Tabla1[Date Inspected] ),
        FILTER (
            Tabla1,
            Tabla1[Date Inspected] < DateInspected
                && Tabla1[Vehicle] = EARLIER ( Tabla1[Vehicle] )
                && Tabla1[Serviced] = "Y"
        )
    )
RETURN
    IF ( Tabla1[Serviced] = "Y", DATEDIFF ( PreviosDay, DateInspected, DAY ) )

Regards

 

Victor




Lima - Peru
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!




Regular Visitor
Posts: 22
Registered: ‎07-25-2017

Re: Calculate days between 2 events in inspection records

Thanks a lot !!

 

If you don't mind - won't you explain the use of "earlier" in the formula ?

 

I do not understand what "earlier" does... I have read somewhere using earlier is quite resource intensive?

 

Thanks again - works very well

Super User
Posts: 1,713
Registered: ‎05-10-2016

Re: Calculate days between 2 events in inspection records

@roelf

 

The EARLIER function take the value of the field before apply the formula context.

 

You can use a VAR to replace the EARLIER.

 

Regards

 

Victor




Lima - Peru
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!




Regular Visitor
Posts: 22
Registered: ‎07-25-2017

Re: Calculate days between 2 events in inspection records

Thanks for the reply.

 

Do I understand correctly that you mean do this :

 

Days since Prev Service =
VAR DateInspected = Tabla1[Date Inspected]
VAR PreviosVeh = Tabla1[Vehicle] VAR PreviosDay = CALCULATE ( MAX ( Tabla1[Date Inspected] ), FILTER ( Tabla1, Tabla1[Date Inspected] < DateInspected && Tabla1[Vehicle] = PreviosVeh ) && Tabla1[Serviced] = "Y" ) ) RETURN IF ( Tabla1[Serviced] = "Y", DATEDIFF ( PreviosDay, DateInspected, DAY ) )

 

If I do this - will this use less resource ?

 

Thanks

Roelf

Highlighted
Super User
Posts: 1,713
Registered: ‎05-10-2016

Re: Calculate days between 2 events in inspection records

@roelf

 

Is more legible and understable to EARLIER.

 

The VAR use less resource when you use it more than 1 time in the DAX Formula.

 

 

 

 




Lima - Peru
Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!