Skip to main content
Announcements
Qlik Connect 2025: 3 days of full immersion in data, analytics, and AI. May 13-15 | Orlando, FL: Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to calculate days since last event

Hello,

I need some help.  I need to calculate the number of days since the last incident recorded.  My data looks something like this.  I have people filing out these forms on a monthly basis and just need to show the number of days since there was an incident.  Any help would be much appreciated.

SegmentMonthYearNumber of Incidents
CORP420151
CORP520150
CORP620150
CORP720150
CORP820150
CORP920150
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

In the script:

OriginalData:

Load

    *,

    MakeDate (IncidentYear,IncidentMonth,1) as IncidentMonthDate;

Load * Inline [

Segment, IncidentMonth, IncidentYear, NoOfIncidents

CORP,4,2015,1

CORP,5,2015,0

CORP,6,2015,0

CORP,7,2015,1

CORP,8,2015,0

CORP,9,2015,0

];

MostRecentIncident:

Load

    Segment as MaxIncidentSegment

   ,max (IncidentMonthDate) as MaxIncidentMonthDate

   ,Today () - max (IncidentMonthDate) as DaysSinceMaxIncident

Resident OriginalData

Where NoOfIncidents > 0

Group by Segment

;  

This will return the number of days since the last incident (sort of; since your data is missing the day of the month, the first is assumed). If the number of incidents is equal to 0, the month is ignored.

Were you instead looking for the number of days since the prior incident?

View solution in original post

6 Replies
swuehl
MVP
MVP

I would start with creating a date field in the data model made of year and month:

LOAD *,

  MakeDate(Year, Month) as Monthstart;

LOAD Segment,

     Month,

     Year,

     [Number of Incidents]

FROM

[https://community.qlik.com/thread/189662]

(html, codepage is 1252, embedded labels, table is @1);

Then the days since last incident can be calculated in a text box like:

=Interval(Today()-(Max({<[Number of Incidents] = {">0"}>} Monthstart)),'D') &  ' days'

Anonymous
Not applicable
Author

Maybe something like this :

     =  today() -max ( {< [Number of Incidents]={'1'} >} MakeDate ( Year , Month ) )

Anonymous
Not applicable
Author

In the script:

OriginalData:

Load

    *,

    MakeDate (IncidentYear,IncidentMonth,1) as IncidentMonthDate;

Load * Inline [

Segment, IncidentMonth, IncidentYear, NoOfIncidents

CORP,4,2015,1

CORP,5,2015,0

CORP,6,2015,0

CORP,7,2015,1

CORP,8,2015,0

CORP,9,2015,0

];

MostRecentIncident:

Load

    Segment as MaxIncidentSegment

   ,max (IncidentMonthDate) as MaxIncidentMonthDate

   ,Today () - max (IncidentMonthDate) as DaysSinceMaxIncident

Resident OriginalData

Where NoOfIncidents > 0

Group by Segment

;  

This will return the number of days since the last incident (sort of; since your data is missing the day of the month, the first is assumed). If the number of incidents is equal to 0, the month is ignored.

Were you instead looking for the number of days since the prior incident?

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try like this in script

LOAD

Num(Today() - LastEventDate) AS Days

LOAd

Segment,

Month,

Year,

[Number of Incidents] ,

MakeDate(Year, Month) AS LastEventDate

FROM DataSource;

Now use below expression

=Only({<LastEventDate={"$(=Max({<[Number of Incidents]={'>0'}>}LastEventDate))"}>} Days)

Hope this helps you.

Regards,

Jagan.

Not applicable
Author

This worked like a charm.  Thank you.

Not applicable
Author

Nice one!