You need to account for all three dates when comparing. If Date3 is the latest date, as is, your code won’t return it, you’ll get the largest of Date1 or Date2 even before looking at Date3.
What if all three dates are null? If you just want to display Date3 (null), then you are technically covered (though I would prefer to see it spelled out so future developers clearly see the intent).
Instead of trying to do the null logic and comparison logic all together, I’d split it up. Create a variable for each date that handles the nulls. Then create the comparison using the variables.
Noticed you are using > operators instead of >=. If Date1 = Date2, you’ll result won’t be returned properly.
Hierarchical if then statements typically use “ElseIf” vs “Else If”. Just something to watch out for, especially if your statements get more complex with layers of parenthesis involved.
Here’s a quick stab at it. May need some tweaking.
–set the nulls to 1/1/1800 (or some early date) so comparisons will work
v_D1: = if isnull( [Date1] ) then todate(“01/01/1800” ; “MM/dd/yyyy”) else [Date1]
v_D2: = if isnull( [Date2] ) then todate(“01/01/1800” ; “MM/dd/yyyy”) else [Date2]
v_D3: = if isnull( [Date3] ) then todate(“01/01/1800” ; “MM/dd/yyyy”) else [Date3]
–get the largest date
= if [v_D1] >= [v_D2] and [v_D1] >= [v_D3] then [v_D1]
elseif [v_D2] >= [v_D3] then [v_D2]
–set the 1/1/1800 back to null
= if [v_D] > todate(“01/01/1800” ; “MM/dd/yyyy”) then [v_D]