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: 
Not applicable

Help with a If calculation

Hi

I have two datefields and want the diffrens in between them. I do this by Stopdate - StartDate.

But in some fields the StopDate are wrong. So I only want the calculation for the fields were Stopdate are larger then Startdate. I do the caluclation in the script as  Date(Date#(StopDate, 'YYYYMMDD'), 'YYYY-MM-DD') - Date(Date#(StartDate, 'YYYYMMDD'), 'YYYY-MM-DD') as DayDiff

I guess I should use a IF statemeant, can anyone help me?

Thanks!

1 Solution

Accepted Solutions
Miguel_Angel_Baeyens

Hi,

If I understand correctly, you only need to add a conditional so the expression is only calculated when StopDate is greater than StartDate, so the following should do it:

If(StopDate > StartDate,

     Date(Date#(StopDate, 'YYYYMMDD'), 'YYYY-MM-DD') - Date(Date#(StartDate, 'YYYYMMDD'), 'YYYY-MM-DD')

     , 0) as DayDiff // 0 is the default value when StopDate is not greater than StartDate: change it according to your needs

Hope that helps.

Miguel

View solution in original post

3 Replies
Miguel_Angel_Baeyens

Hi,

If I understand correctly, you only need to add a conditional so the expression is only calculated when StopDate is greater than StartDate, so the following should do it:

If(StopDate > StartDate,

     Date(Date#(StopDate, 'YYYYMMDD'), 'YYYY-MM-DD') - Date(Date#(StartDate, 'YYYYMMDD'), 'YYYY-MM-DD')

     , 0) as DayDiff // 0 is the default value when StopDate is not greater than StartDate: change it according to your needs

Hope that helps.

Miguel

CELAMBARASAN
Partner - Champion
Partner - Champion

Hi,

     Try with this.

     If(Date(Date#(StopDate, 'YYYYMMDD'), 'YYYY-MM-DD')>Date(Date#(StartDate, 'YYYYMMDD'), 'YYYY-MM-DD'),Interval(Date(Date#(StopDate, 'YYYYMMDD'), 'YYYY-MM-DD')-Date(Date#(StartDate, 'YYYYMMDD'), 'YYYY-MM-DD'),'D')) as DayDiff

Hope it helps

Celambarasan

Not applicable
Author

Thanks to both of you!