Forum Discussion

mburkett's avatar
mburkett
New Member
3 years ago
Solved

Date Transformation

I am pulling in a table from the web with only the day and month. Power query is guessing the date and setting everything to 2023. Is there a way to tell it to change to 2022 if the date is greater t...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi mburkett ,

     

    Suppose we have:

    Please follow these steps:

    1. Duplicated [Date] Column:

    2. Change [Date - Copy]'s Type from [Date] to [Date/Time]

    3. Add a custom column:

    if [#"Date - Copy"] > DateTime.LocalNow()
    then Date.AddYears([Date],-1)
    else [Date]

    4. Remove and rename columns.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc65DQAgDAPAXaiRwAnvLCj7r4EokOzyujsnoaBYNU+RH4zhjMbojMGYjMXYDFSRHCAJyALSwH/EBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
        #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Date", "Date - Copy"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Date - Copy", type datetime}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each if [#"Date - Copy"] > DateTime.LocalNow()
    then Date.AddYears([Date],-1)
    else [Date]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date", "Date - Copy"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}})
    in
        #"Renamed Columns"

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum