Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Learn how to migrate to Qlik Cloud Analytics™: On-Demand Briefing!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Calculating Months In Service in Load

hic have a fleet of vehicles that all have an in service date by unit #. I am needing to calculate the number of months they have been in service from their initial in service date so I can calculate fleet average age through previous months. I have an example of what I believe I am looking for below.

UnitMonth/YearIn Service DateMonths In Service
5962501Aug-158/18/20150
Sep-151
Oct-152
Nov-153
Dec-154
Jan-165
Feb-166
Mar-167
Apr-168
May-169

Any help would be much appreciated.

6 Replies
sunny_talwar

How does the raw data look like?

Not applicable
Author

UnitIn Service Date
2277011/13/2015
5968604/30/1999
438100911/20/2012
596156712/31/2006
596157012/31/2006
596157611/1/2006
59616259/30/2007
596164112/31/2007
59616595/1/2008
59616605/1/2008
59616635/1/2008
59616655/1/2008
59616665/1/2008
59616837/20/2008
59616858/15/2008
59616868/15/2008
596168710/1/2008
sunny_talwar

Like this?

Capture.PNG

Not applicable
Author

I believe so. Can you use that expression in the load script?

effinty2112
Master
Master

Hi Nathan,

Try this script:

Data:

LOAD * INLINE [

    Unit, In Service Date

    22770, 11/13/2015

    596860, 4/30/1999

    4381009, 11/20/2012

    5961567, 12/31/2006

    5961570, 12/31/2006

    5961576, 11/1/2006

    5961625, 9/30/2007

    5961641, 12/31/2007

    5961659, 5/1/2008

    5961660, 5/1/2008

    5961663, 5/1/2008

    5961665, 5/1/2008

    5961666, 5/1/2008

    5961683, 7/20/2008

    5961685, 8/15/2008

    5961686, 8/15/2008

    5961687, 10/1/2008

];

Earliest:

LOAD Min(Date([In Service Date])) as EarliestMonth Resident Data;

Let vEarliest = PEEK('EarliestMonth',0,'Earliest');

Let vIntEarliestMonth = MonthStart('$(vEarliest)');

Let vIntThisMonth = MonthStart(Today());

DROP Table Earliest;

[In Service Dates]:

LOAD

Unit,

[In Service Date],

MonthStart([In Service Date]) as InServiceMonth,

Floor(MonthStart([In Service Date])) as IntInServiceMonth,

Floor(Date('$(vIntThisMonth)')) as IntThisMonth

Resident Data;

Drop Table Data;

Calendar:

Load

Date,

Floor(Date) as IntDate;

Load

AddMonths('$(vIntEarliestMonth)',IterNo()-1) as Date

AutoGenerate 1 While AddMonths(Date('$(vIntEarliestMonth)'),IterNo()-1) <=Date('$(vIntThisMonth)');

IntervalMatch(IntDate) LOAD Distinct IntInServiceMonth, IntThisMonth Resident [In Service Dates];

Left Join([In Service Dates])

LOAD

* Resident [In Service Dates-1];

DROP Table [In Service Dates-1];

Left Join([In Service Dates])

LOAD * Resident Calendar;

DROP Table Calendar;

Result:

LOAD

Date(Date,'MM/YYYY') as Month,

Unit,

InServiceMonth,

(Year(Date) - Year(InServiceMonth))*12 +(Month(Date) - Month(InServiceMonth)) as [Months In Service]

Resident [In Service Dates];

DROP Table [In Service Dates];

Make this straight table, I've filtered the data to this year to save time, the last expression give the avg age of the fleet.

Month Count(DISTINCT Unit) Sum([Months In Service]) Sum([Months In Service]) / Count(DISTINCT Unit)
01/201617158193.0
02/201617159894.0
03/201617161595.0
04/201617163296.0
05/201617164997.0
06/201617166698.0

Cheers

Andrew

sunny_talwar

May be this:

Table:

LOAD *,

  AutoNumber(RowNo(), Unit)-1 as Requirement;

LOAD *,

  MonthName(AddMonths([In Service Date], + IterNo() - 1)) as MonthYear

While AddMonths([In Service Date], + IterNo() - 1) <= MonthName(Today());

LOAD * INLINE [

    Unit, In Service Date

    22770, 11/13/2015

    596860, 4/30/1999

    4381009, 11/20/2012

    5961567, 12/31/2006

    5961570, 12/31/2006

    5961576, 11/1/2006

    5961625, 9/30/2007

    5961641, 12/31/2007

    5961659, 5/1/2008

    5961660, 5/1/2008

    5961663, 5/1/2008

    5961665, 5/1/2008

    5961666, 5/1/2008

    5961683, 7/20/2008

    5961685, 8/15/2008

    5961686, 8/15/2008

    5961687, 10/1/2008

];


Capture.PNG