Forum Discussion

patelrr's avatar
patelrr
Regular Visitor
6 years ago
Solved

Change the date based on time stamp

Hello,   I need to add a custom column to my data on Power BI Desktop where the date is adjusted based on the time stamp of a different column.   For example: I have a Date column that displays ...
  • Nathaniel_C's avatar
    Nathaniel_C
    6 years ago

    Hi mailo2000 ,

    If I understand your question, you would like a new column that uses a day earlier if the hour is 6 am or less.

    You would like to go from the first pic to the second?

    Here is what you need to paste into the Adv Editor. in Power Query

    Basically I created a new col with a date a day earlier, then extracted the hour, then wrote a conditional statement that said if the hour is less than or equal to 6, select the earlier day, if not select the time stamp day.

     

    If you are a beginner let me highly recommend KenPuls book M is for (Data) Monkey  and MattAllington book Supercharge Power BI

    If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
    Nathaniel

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8k2sVDA01VEwMjC0VLBIzFWK1YEKmkEFDUytjA3AwrkgYUOosKGJlQFQOBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Time = _t]),
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Time", type datetime}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each Date.AddDays([Time],-1)),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type datetime}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Custom", "1 day earlier"}}),
    #"Added Custom1" = Table.AddColumn(#"Renamed Columns", "time only", each DateTime.Time([Time])),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom1", "Newdate", each if [time only] <= #time(6, 0, 0) then [1 day earlier] else [Time]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Conditional Column",{{"time only", type time}, {"Newdate", type date}})
    in
    #"Changed Type2"