Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

v-alq-msft

Extract Date Time From Texts In Several Scenarios

Scenario 1: 
Some organizations save date time together with other details recorded in text type that cannot change the field type to Date/Time. For example,

a1.png

Requirements

How can we transfer the text field into date/time format to be used in later calculation? The most straight forward and commonly used ways is to remove the end strings that is not date/time, we want to remove the weekdays here.

 

Power Query Method

Using M code is pretty similar the DAX, we have Text.Start() Returns the first count characters of text as a text value, and Text.Length to return the length.

a2.png

We can tell from this example, that LEFT() in dax is matched with Text.Start(), and LEN() is similar to Text.Length. In addition, if the weekday is recorded in the start strings e.g. “Mon 2019 Dec 26 10:20:04”, we could replace the formulas with RIGHT() and Text.End() respectively.

 

Dax Method:

The LEFT() is used to return specified number of characters from the start of a text string, and LEN() returns the number of characters in a text string. As the result, the formulas removes the last 4 stings returns only date and time information, so we can just change the data type to date/time.

a3.png

Scenario 2: 

Suppose date time is recorded with others texts (e.g. names, products) when the text string length differs, in this case we cannot use the above method.

a4.png                              a5.png              

Power Query Method

The quick method is using split feature in Query Editor, split by the right most delimiter “ “ in this example, and then remove the end text column after splits.

a6.png

If the text is at the beginning, to remove it we should select the left most delimiter.

a7.png

 

DAX Method:

DAX method requires a dax formula compared with Power Query solution, also LEN() and SUBSTITUTE() would be used.

Text at the end:

Date/time = LEFT([DateTimeField],FIND("-",SUBSTITUTE([DateTimeField]," ","-",LEN([DateTimeField])-LEN(SUBSTITUTE([DateTimeField]," ",""))))-1)

a8.png

Text at the beginning:

Date/time = RIGHT([DateTimeField],LEN([DateTimeField])-SEARCH("-",SUBSTITUTE([DateTimeField]," ","-")))

a9.png

 

 

Scenario 3: 

Suppose there is more information recorded and saved together with date/time in a mixed order. How can we deal with this field? The requirement is basically take out the date time information and remove the rest redundant texts.

a10.png

Power Query Method:

The problem is there are other details recorded together in the field that is neither at the beginning or end. What we can do is use Split() function to split the column by the blanks in the cells, and track the year, month, day and time based on the split order.

 

To achieve this, we can create a custom column. The formula splits the column by “ ” (blank), and create a datetime using DateTime.From Text() function by taking out the split parts with given order.


= Table.AddColumn(#"Changed Type", "DateTime", each

let split = Text.Split( [DateTimeField], " " ),

            dateTime = DateTime.FromText( split{1} & " " & split{2} & " " & split{3} & " " & split{4} )

        in  dateTime)

 

In this sample, number 0 is the text sting before the first blank, and 1 is the year, 2 is the month, and so on. If the cell recorded in other format like “2020 XXX 12:00:00 19 Oct”, the number in the split{} should be changed accordingly.

a11.png

Of course, we would need to change it to date/time type afterwards. Once we have this datetime column, we should able to use it as a general datetime field for later calculations e.g.:(weekday, YearMonth,etc)

Weekday: = Table.AddColumn(#"Changed Type1", "Weekday", each Date.DayOfWeekName([DateTime], "en-us"))

YearMonth = Table.AddColumn(#"Added Custom1", "YearMonth", each Text.Combine({DateTime.ToText([DateTime], "yyyy"), "-", DateTime.ToText([DateTime], "%M")}), type text)

 

DAX Method

Using DAX in this scenario is rather complex, we could split the column by using PATHEITEM (). The function is used here to return the item at the specified position from a string resulting from evaluation of a PATH function. Positions are counted from left to right. Then we can use CONCATENATE to combine the parts together.

 

DAX datetime =

var p1 = PATHITEM( SUBSTITUTE( [DateTimeField], " ", "|" ) , 1)

var p2 = PATHITEM( SUBSTITUTE( [DateTimeField], " ", "|" ) , 2)

var p3 = PATHITEM( SUBSTITUTE( [DateTimeField], " ", "|" ) , 3)

var p4 = PATHITEM( SUBSTITUTE( [DateTimeField], " ", "|" ) , 4)

var p5 = PATHITEM( SUBSTITUTE( [DateTimeField], " ", "|" ) , 5)

Return    CONCATENATE(p2,CONCATENATE(" ",CONCATENATE(p3, CONCATENATE(" ",CONCATENATE(p4,CONCATENATE(" ",p5))))))

 a12.png

 

This is how one can extract date time from texts in several scenarios. Hope this article helps everyone with similar questions here. 

 

Author: Paul Zheng

Reviewer: Yu Yang