Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Change the date if it is before today

Hi all!  This is my first post and I hope you can help me. I have the following problem to solve. I have this date column and my goal is that if the date in the "Fecha" (date) column is less than today, the value is replaced by today. If the date is equal to today or greater than the date is kept. This is in PowerQuery

 

Thanks you all!
Regards
Bruno Bentancor.-

  • Hi Anonymous

    You can create a custom column with code like this:

    if [Fecha] >= Date.From(DateTime.LocalNow()) then [Fecha]  else Date.From(DateTime.LocalNow())  

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

3 Replies

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

    Hi Anonymous

    You can create a custom column with code like this:

    if [Fecha] >= Date.From(DateTime.LocalNow()) then [Fecha]  else Date.From(DateTime.LocalNow())  

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Excellent AlB ! Thank you very much for your help 😄
      It worked for me! 

      Regards!
      Bruno Bentancor.-

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

    Hello Anonymous 

     

    if you want to avoid to multiple columns handling, you can transform the existing one

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTVMzTQMzJQitUBciyQOMYGSBwDQz0DS2SOBZgTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Fecha = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Fecha", type date, "de-DE"}}),
        TransformFecha = Table.TransformColumns
        (
            #"Changed Type",
            {
                {
                    "Fecha",
                    each if _ >= Date.From(DateTime.LocalNow()) then _ else Date.From(DateTime.LocalNow())
                }
            }
        )
    in
        TransformFecha

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy