Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
mrthomasshelby
Creator III
Creator III

How to Count only distinct years?

Hello,

I have a table as shown below:

NoCategoryDescriptionCustomerIndustryDurationValueLocationStart DateEnd DateContact
1RailingsAABCDSports24500000Australia3/25/20103/25/2012Z
2RailingsBEFGHOil3720000Indonesia6/12/20126/12/2015Y
3RailingsCIJKLMetals37250000Cambodia6/12/20126/12/2015X
4RailingsDMNOPSteel37450000Brunei6/12/20126/12/2015D

Now I want to define a measure called 'Experience' that counts the number of distinct years that the vendor above has provided the services in. For the example above, we can see that the vendor has provided these 4 services in the years: 2010,2011,2012,2013,2014,2015, so the value of 'Experience' will be 6. I am also doing this in the script part for your information. I have tried taking the sum of Year difference between the dates but the problem with this is it sums up even the non-distinct years and gives an inflated value. Please help me out with the logic for this. Thanks in advance!

stalwar1kaushik.solankitresesco

1 Solution

Accepted Solutions
sunny_talwar

Check this

T1:

LOAD No.,

    [Equipment Category],

    Description,

    [Name of Client],

    Industry,

    [Duration (No of months)],

    Value,

    [Location of Services],

    [Start Date],

    [End Date],

    [Client’s Contact Person / Person-in-Charge],

    Year([Start Date]) as StartYear,

    Year([End Date]) as EndYear

FROM

[Book1.xlsx]

(ooxml, embedded labels, table is Sheet1);


Dates:

LOAD DISTINCT StartYear,

EndYear,

StartYear + IterNo() - 1 as Year

Resident T1

While StartYear + IterNo() - 1 <= EndYear;

View solution in original post

13 Replies
isingh30
Specialist
Specialist

Where is year 2011,2013 & 2014?

Thank you!

wade12
Partner - Creator II
Partner - Creator II

use year() function to strip  out the year from the date and just work with that.  ... just a suggestion.

mrthomasshelby
Creator III
Creator III
Author

Hi! For service No.1, the start date is 3/25/2010 and End date is 3/25/2012. So the service was in years 2010,2011,2012. Similarly, for service No.2, start date is 6/12/2012 and End date is 6/12/2015. So the service was in the years 2012,2013,2014,2015. So overall the vendor has provided services in the years 2010,2011,2012,2013,2014,2015. Hope that makes it more clear.

sunny_talwar

Look at "Looping over the same record" section of the below blog

Loops in the Script

kaushiknsolanki
Partner Ambassador/MVP
Partner Ambassador/MVP

Try this,

(Year(EndDate) - Year(StartDate)) +1

as your new expression which will tell you how many numbers of year that vendor serviced you.

For example in your above case Row 1.

(2012-2010) + 1 will be 2+1 = 3

Regards,

Kaushik Solanki

Please remember to hit the 'Like' button and for helpful answers and resolutions, click on the 'Accept As Solution' button. Cheers!
mrthomasshelby
Creator III
Creator III
Author

This is exactly what I tried but the problem with this is it doesn't give the overall experience of the vendor, just experience at each line item level. I want to calculate the overall experience of a vendor. For example, this vendor has provided services in the years 2010,2011,2012,2013,2014,2015 from all the listed service numbers. Thanks!

mrthomasshelby
Creator III
Creator III
Author

Can you maybe give me an idea as to how I can use this in my case Sunny? Thanks!

sunny_talwar

Create Date, MonthYears, or Year from the date range and then you can then easily count the distinct years

mrthomasshelby
Creator III
Creator III
Author

Here's the script I tried:

T1:

LOAD Vendor,

     No.,

     [Equipment Category],

     Description,

     [Name of Client],

     Industry,

     [Duration (No of months)],

     Value,

     [Location of Services],

     [Start Date],

     [End Date],

     [Client’s Contact Person / Person-in-Charge]

FROM

(ooxml, embedded labels, table is Sheet1);

Dates:

Load Year([Start Date]+ IterNo() - 1 ) as Year

Resident T1

While IterNo() <= Year([End Date]) - Year([Start Date]) + 1 ;

But I'm not getting the correct result. Can you point what I'm doing wrong?Also, I want this measure to be associated with the 'Vendor'.   I'm attaching the app too just in case. Thanks a lot!