Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
mohdhaniff
Creator
Creator

Qlik View Expression for Max Year (-1) and Max Date

Hi,

Currently i got multiple records in database. How can I create expression to retrieve data with specific Year and Max Date.

Sample Data:

LOAD * INLINE [

    Record, Date, Year

    abc, 7/9/2015, 2015

    abc, 6/8/2015, 2015

    abc, 4/1/2015, 2015

    abc, 12/3/2016, 2016

    abc, 16/2/2016, 2016

    abc, 6/1/2016, 2016

    abc, 1/1/2017, 2017

    abc, 6/2/2017, 2017

];

I only able to get the required Year but not the max date:

My Expression:

=count({$<Year={'$(=max(Year)-1)'}>} Record)

My Result:

1.JPG

My Expectation Result:

1.JPG

1 Solution

Accepted Solutions
jonathandienst
Partner - Champion III
Partner - Champion III

Your problem is around date formats. The dates are loading as 'd/M/yyyy', but your environment setting is probably 'dd/MM/yyyy'. Try:

SET DateFormat='dd/MM/YYYY';

=Count({<Date = {"$(=Date(Max({<Year={'$(=Max(Year)-1)'}>} Date), 'd/M/yyyy'))"}>} Record)

or

SET DateFormat='d/M/YYYY';

=Count({<Date = {"$(=Date(Max({<Year={'$(=Max(Year)-1)'}>} Date)))"}>} Record)

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein

View solution in original post

6 Replies
khalander
Creator II
Creator II

Hi,

Please try below expression

=count({$<Year={'$(=max(Year)-1)'},Date={'$(=max(Date))'}>} Record)

mohdhaniff
Creator
Creator
Author

Hi dada khalander,

Thanks but the formula not work. I got zero (0) result

rahulpawarb
Specialist III
Specialist III

Hello Haniff,

As a workaround I have created script level solution for this problem. Created a Resident Load to pull maximum date for each Record and Year (refer below given sample script).

Data:

LOAD * INLINE [

    Record, Date, Year

    abc, 7/9/2015, 2015

    abc, 6/8/2015, 2015

    abc, 4/1/2015, 2015

    abc, 12/3/2016, 2016

    abc, 16/2/2016, 2016

    abc, 6/1/2016, 2016

    abc, 1/1/2017, 2017

    abc, 6/2/2017, 2017

];

NoConcatenate

NewData:

LOAD

Record AS NewRecord,

Year AS NewYear,

Date(Max(Date),'MM/DD/YYYY') AS NewDate

Resident Data

Group By Record, Year;

Regards!

Rahul

mohdhaniff
Creator
Creator
Author

Hi Rahul,

Sorry.  I use QV Personal Edition and cannot open your attachment.

By using the script provided by you, now I got 2 tables and the data become bigger. Can you help me to copy the expression script from your sample?

Regards.

jonathandienst
Partner - Champion III
Partner - Champion III

Your problem is around date formats. The dates are loading as 'd/M/yyyy', but your environment setting is probably 'dd/MM/yyyy'. Try:

SET DateFormat='dd/MM/YYYY';

=Count({<Date = {"$(=Date(Max({<Year={'$(=Max(Year)-1)'}>} Date), 'd/M/yyyy'))"}>} Record)

or

SET DateFormat='d/M/YYYY';

=Count({<Date = {"$(=Date(Max({<Year={'$(=Max(Year)-1)'}>} Date)))"}>} Record)

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
mohdhaniff
Creator
Creator
Author

Thanks Jonathan for your solution.