Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
jvafb
Contributor
Contributor

Get the data of a year prior to the selected date

Hello

I have created a table where I want to show the sum of the RATIO measure of the selected date (field DATE) and the data of one year before to such date. For the selected date I use:

Sum(RATIO)

It works fine, but for the previous year I use:

Sum({<DATE= {"$(=Date(AddMonths(Max(DATE), -12)))"}>} RATIO)

But it shows zero (0), could you help me with that formula?

Thank you

 

Labels (1)
6 Replies
sidhiq91
Specialist II
Specialist II

@jvafb  The expression is perfect, even I tried the same with the below expression and it works perfectly fine.

Sum({<OrderDate={"$(=Addmonths(Max(OrderDate),-12))"}>}LineSalesAmount)

Just check if there is data for that particular date when it goes to the previous year.

If you still have any confusion, please attach the sample data and I will look into it.

vinieme12
Champion III
Champion III

Use AddYears() instead


Sum({<DATE= {“$(=Date(AddYears(Max(Date),-1)))”}>} RATIO)

Or specify the date field format of DATE field 

Sum({<DATE= {“$(=Date(AddYears(Max(Date),-1),’YYYY/MM/DD’))”}>} RATIO)

replace YYYY/MM/DD With the date format as in DATE field

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
jvafb
Contributor
Contributor
Author

I use a fixed date:

Sum({<DATE={'2022-03-31 00:00:00.000000'}>} RATIO)

And it works, I think it is failing because I am using a Date type instead of a timestamp, how can I transform a date to a timestamp with hours 00:00:00.000000?

vinieme12
Champion III
Champion III

Would rather Convert your timestamp field into a date field or create a new field with only the date part

 

Date(Floor(DATE)) as Datefield 

Then just use below

Sum({<Datefield= {“$(=Date(AddYears(Max(Datefield),-1)))”}>} RATIO)

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
jvafb
Contributor
Contributor
Author

I load data with a sql query in the editor, where can I convert or create the field?

vinieme12
Champion III
Champion III

In the Data load Editor add a preceding load as below highlighted

 

TAbleName:

LOAD

*

,Date(Floor(timestampField)) as Datefield 

;

SQL 

Select field1,field2,timestampField

From db.tablename;

 

 

 

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.