Forum Discussion

Havard's avatar
Havard
Frequent Visitor
5 years ago
Solved

Find if a date from API is YTD or LYTD

I'm really new to this. I can't find how to do what's marked with BOLD. I would be very happy if anyone could help med with this:

 

if [postingDate] >= #date(ThisYear, 1, 1) and [postingDate] <= #date(ThisYear,Thismonth, ThisDay) 

then ‘’YTD’’

else if [postingDate] >= #date(ThisYear -1, 1, 1) and [postingDate] <= #date(ThisYear -1,ThisMonth, ThisDay) 

then ‘’LYTD’ 

else ‘’BLANK’’

  • That is how you do it. The clean code would be something like this Havard 

    let 
       varToday = Date.From(DateTimeZone.FixedUtcNow())
    in
    
    if [postingDate] >= Date.StartOfYear(varToday) and [postingDate] <= varToday
    
    then "YTD"
    
    else if [postingDate] >= Date.AddYears(varToday,-1) and [postingDate] <= Date.AddYears(varToday,-1)
    
    then "LYTD"
    



     

5 Replies

  • Havard's avatar
    Havard
    Frequent Visitor

    I Ifinally solved it, but it's bit of .."clumsy". I have seen a code with Let, but don't know how to go from there to if...

     

    if [postingDate] >= Date.StartOfYear(Date.From(DateTimeZone.FixedUtcNow())) and [postingDate] <= Date.From(DateTimeZone.FixedUtcNow())

    then "YTD"

    else if [postingDate] >= Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1) and [postingDate] <= Date.From(Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1))

    then "LYTD"

    else "BLANK"

    • edhans's avatar
      edhans
      Icon for Community Champion rankCommunity Champion

      That is how you do it. The clean code would be something like this Havard 

      let 
         varToday = Date.From(DateTimeZone.FixedUtcNow())
      in
      
      if [postingDate] >= Date.StartOfYear(varToday) and [postingDate] <= varToday
      
      then "YTD"
      
      else if [postingDate] >= Date.AddYears(varToday,-1) and [postingDate] <= Date.AddYears(varToday,-1)
      
      then "LYTD"
      



       

      • Havard's avatar
        Havard
        Frequent Visitor

        Thx a lot! It's easier to see the "language" now.