cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ansonee1
Helper I
Helper I

Separate Date and Time From Timestamp in Calculated Column

I apologize in advance if this question has been aked before, but I've looked around and can't find the anwser that quite addresses my issue...

I have a table in a tabula model with several datetime columns. What I need to do is "parse" those columns. For example, I have a column called "DepartureDateTime".  I've created two additional columns - DateDeparture and TimeDeparture. I'd like to split the main column into those two columns. Both respectively need to be a time and a date.

Thanks in advance!

11 REPLIES 11
Icey
Community Support
Community Support

Hi @ansonee1 ,

 

If you want to create calculated columns, try this:

 

Date = [DateTime]
Time = FORMAT([DateTime],"hh:mm:ss")

 

DateTime.gif

 

 

If you want to create custom columns, try this:

Date = Date.From([DateTime])
Time = Time.From([DateTime])

DateTime-pq.gif

 

 

Best regards

Icey

 

If this post helps, then consider Accepting it as the solution to help other members find it faster.

Thank you, but I'm not doing this directly in Power Query/Power BI. I'm creating a tabular model in Visual Studio, so the process to create those columns don't exactly translate. M is how those expressions are written, the steps aren't the same.

PhilipTreacy
Super User III
Super User III

Hi @ansonee1 

OK so going back to PQ, if you start with this

Source = #"SQL/azsds001d database windows net;EDW",
ops_DimFlightSchedule = Source{[Schema="ops",Item="DimFlightSchedule"]}[Data],
#"Filtered Rows" = Table.SelectRows(ops_DimFlightSchedule, each ([Cancelled] <> 2))
in
#"Filtered Rows"

then change it to this

let
   Source = #"SQL/azsds001d database windows net;EDW",
   ops_DimFlightSchedule = Source{[Schema="ops",Item="DimFlightSchedule"]}[Data],
   #"Filtered Rows" = Table.SelectRows(ops_DimFlightSchedule, each ([Cancelled] <> 2)),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Filtered Rows", "DepartureDateTime", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"DepartureDateTime.1", "DepartureDateTime.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"DepartureDateTime.1", type text}, {"DepartureDateTime.2", type time}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"DepartureDateTime.1", type date}}, "en-US"),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type with Locale",{{"DepartureDateTime.1", "DateDeparture"}, {"DepartureDateTime.2", "TimeDeparture"}})
in
    #"Renamed Columns"

To give this

date-time-cols2.png

Note that my date format is d/m/y 

If you want to repeat these steps in another query then copy/paste everything after the #"Filtered Rows" line

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thanks again for the reply! So, I went through the process of updating my PowerQuery query in Visual Studio and have this now:

 

 ops_DimFlightSchedule1 = Source{[Schema="ops",Item="DimFlightSchedule"]}[Data],
    #"Filtered Rows" = Table.SelectRows(ops_DimFlightSchedule1, each ([Cancelled] <> 2)),
    #"Duplicated Column" = Table.DuplicateColumn(#"Filtered Rows", "ScheduleDepartureDatetimeZ", "ScheduleDepartureDatetimeZ - Copy"),
    #"Extracted Time" = Table.TransformColumns(#"Duplicated Column",{{"ScheduleDepartureDatetimeZ - Copy", DateTime.Time, type time}}),
    #"Renamed Columns" = Table.RenameColumns(#"Extracted Time",{{"ScheduleDepartureDatetimeZ - Copy", "ScheduleDepartureTimeZ"}}),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Renamed Columns", "ScheduleDepartureDatetimeLocal", "ScheduleDepartureDatetimeLocal - Copy"),
    #"Renamed Columns1" = Table.RenameColumns(#"Duplicated Column1",{{"ScheduleDepartureDatetimeLocal - Copy", "ScheduleDepartureTimeLocal"}}),
    #"Extracted Time1" = Table.TransformColumns(#"Renamed Columns1",{{"ScheduleDepartureTimeLocal", DateTime.Time, type time}}),
    #"Duplicated Column2" = Table.DuplicateColumn(#"Extracted Time1", "OutZ", "OutZ - Copy"),
    #"Renamed Columns2" = Table.RenameColumns(#"Duplicated Column2",{{"OutZ - Copy", "ActualDepartureTimeZ"}}),
    #"Extracted Time2" = Table.TransformColumns(#"Renamed Columns2",{{"ActualDepartureTimeZ", DateTime.Time, type time}}),
    #"Duplicated Column3" = Table.DuplicateColumn(#"Extracted Time2", "OutLocal", "OutLocal - Copy"),
    #"Renamed Columns3" = Table.RenameColumns(#"Duplicated Column3",{{"OutLocal - Copy", "ActualDepartureTimeLocal"}}),
    #"Extracted Time3" = Table.TransformColumns(#"Renamed Columns3",{{"ActualDepartureTimeLocal", DateTime.Time, type time}}),
    #"Duplicated Column4" = Table.DuplicateColumn(#"Extracted Time3", "OutZ", "OutZ - Copy"),
    #"Renamed Columns4" = Table.RenameColumns(#"Duplicated Column4",{{"OutZ - Copy", "ActualDepartureDateZ"}}),
    #"Extracted Date" = Table.TransformColumns(#"Renamed Columns4",{{"ActualDepartureDateZ", DateTime.Date, type date}}),
    #"Duplicated Column5" = Table.DuplicateColumn(#"Extracted Date", "OutLocal", "OutLocal - Copy"),
    #"Renamed Columns5" = Table.RenameColumns(#"Duplicated Column5",{{"OutLocal - Copy", "ActualDepartureDateLocal"}}),
    #"Extracted Date1" = Table.TransformColumns(#"Renamed Columns5",{{"ActualDepartureDateLocal", DateTime.Date, type date}}),
    #"Duplicated Column6" = Table.DuplicateColumn(#"Extracted Date1", "InZ", "InZ - Copy"),
    #"Renamed Columns6" = Table.RenameColumns(#"Duplicated Column6",{{"InZ - Copy", "ActualArrivalDateZ"}}),
    #"Extracted Date2" = Table.TransformColumns(#"Renamed Columns6",{{"ActualArrivalDateZ", DateTime.Date, type date}}),
    #"Duplicated Column7" = Table.DuplicateColumn(#"Extracted Date2", "InLocal", "InLocal - Copy"),
    #"Renamed Columns7" = Table.RenameColumns(#"Duplicated Column7",{{"InLocal - Copy", "ActualArrivalDateLocal"}}),
    #"Extracted Date3" = Table.TransformColumns(#"Renamed Columns7",{{"ActualArrivalDateLocal", DateTime.Date, type date}}),
    #"Duplicated Column8" = Table.DuplicateColumn(#"Extracted Date3", "InZ", "InZ - Copy"),
    #"Renamed Columns8" = Table.RenameColumns(#"Duplicated Column8",{{"InZ - Copy", "ActualArrivalTimeZ"}}),
    #"Extracted Time4" = Table.TransformColumns(#"Renamed Columns8",{{"ActualArrivalTimeZ", DateTime.Time, type time}}),
    #"Duplicated Column9" = Table.DuplicateColumn(#"Extracted Time4", "InLocal", "InLocal - Copy"),
    #"Renamed Columns9" = Table.RenameColumns(#"Duplicated Column9",{{"InLocal - Copy", "ActualArrivalTimeLocal"}}),
    #"Extracted Time5" = Table.TransformColumns(#"Renamed Columns9",{{"ActualArrivalTimeLocal", DateTime.Time, type time}})
in
    #"Extracted Time5"

 When viewing the new table in PowerQuery, the columns display as expected:

Capture1.JPG

I create a table in te Tabular model using this same query. When I view the data in the tabular model, however, the date portion is still present - even though I format the column to JUST have the time:

 

ansonee1_0-1607655937362.png

The problem is that this column is used in a relationship with a Time Dimension table. Since the actual value isn't just the time, the relationship doesn't work.

Getting closer!!

 

 

 

 

watkinnc
Responsive Resident
Responsive Resident

Even easier to just click on the Add Columns tab and then go to the Date functions and select Date Only and then Time only from the options. If you are worried about folding, change the Date.DateTime function to Date.From

 

--Nate

PhilipTreacy
Super User III
Super User III

Hi @ansonee1 

Sample PBIX file here.

This will give you the Date column

 

Date = LEFT('Table'[DepartureDateTime], 10)

 

This will give you the Time column 

 

Time = MID('Table'[DepartureDateTime], 12, LEN('Table'[DepartureDateTime] - 12))

 

 To give

date-time-cols.png

Modify the format to suit.

Regards

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thanks, Philip fr the reply!

In looking at your solution, and after trying a couple of things, I realized the adding of hte columns in the tble definition as proposed earlier, is probably going to be the best option.

yours works great, but the LEFT portion won't work since our dates don't force an extra 0 (zero) for single days/months. So for example if the datetime string is 

7/6/2019 3:43:00 PM

, your calc returns 7/6/2019 3.

And the date and time columns are going to be used as foreign keys in a tabular model, so my solution I tried didn't work as I just fomatted the full datetime column as a date, time, etc. So it wasn't actually a date value, so the relatinship didn't work.

So, given the original reply and their solution, how would you wedge that into the following table definition:

 

Source = #"SQL/azsds001d database windows net;EDW",
ops_DimFlightSchedule = Source{[Schema="ops",Item="DimFlightSchedule"]}[Data],
#"Filtered Rows" = Table.SelectRows(ops_DimFlightSchedule, each ([Cancelled] <> 2))
in
#"Filtered Rows"

 

And I should correct myself!!! I need to edit the M expression that my table is based on! It's not DAX...sorry about the miscommunication!!

 

PhilipTreacy
Super User III
Super User III

Hi @ansonee1 

I'm a bit confused.  You've posted in he Power Query forum but you are referring to a Calculated Column which is what you get in DAX/Power Pivot.  Are you looking for a solution in DAX?  You use Custom Columns in Power Query.

Why do you need to add 8 new columns?  Why aren't you splitting your source columns into just 2 columns, one for date and one for time?

Can you please share soem source data and the expected outcome so we know what we are aiming to produce.

regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


I'm posting here beauase there is no Tabular Model / DAX board here...and I've had other questions on DAX answered here.

I need to have three columns: the original column which is a datetime, a column to hold the date, and a column to hold the time, so I am trying to do just what you said.

So essntially if you have 11/01/2020 10:00:00, you would then have 11/01/2020 in one column and 10:00:00 in a third.

 

CNENFRNL
Super User III
Super User III

Hi, @ansonee1 , you might want to add new columns this way,

#"Added DateDeparture" = Table.AddColumn(Source, "DateDeparture", each Date.From(DateTime.FromText([DepartureDateTime]))),

#"Added TimeDeparture" = Table.AddColumn(#"Added DateDeparture", "TimeDeparture", each Time.From(DateTime.FromText([DepartureDateTime])))

Screenshot 2020-12-08 194003.png

 

Thanks for the reply!

The issue withthis is that I am actually needing to add eight new columns, and a calculated column strikes me as the easier route, without having to go in and rewrite the entire table expression.

Also, there are several oter tables that will need this change, and if I have a "standard" calculation I can just modify based on input column, so much the easier!

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors