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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
anikettakalkar
New Member

Change/Convert SQL column having datetime stored as Text to DateTime in PowerBi Desktop

I am trying to visualize performance counters data stored in SQL tables using Power Bi. 

It has two tables 1. CounterData 2. CounterDetails

In CounterData table datetime is stored as text format, i want to use that data as DateTime in Power Bi Desktop. 

 

But unable to do so,  How can I convert the text column into dateTime using both query mode - Import & Direct Query ? 

 

 

1 ACCEPTED SOLUTION
v-yuezhe-msft
Employee
Employee

@anikettakalkar,

I make a test using the sample table below. 
1.PNG

In DirectQuery mode, create the following columns.

Left = LEFT(CounterData[RecordTime],FIND(" ",CounterData[RecordTime],1,0)-1)

Right = RIGHT(CounterData[RecordTime],LEN(CounterData[RecordTime])-LEN(CounterData[Left])-1)

After creating the above columns, change data type of Right column to Time. Then create the following columns.

Year = LEFT(CounterData[Left],4)

Day = RIGHT(CounterData[Left],LEN(CounterData[Left])-FIND("/",CounterData[RecordTime],6,1))

Month = MID(CounterData[Left],6,LEN(CounterData[Left])-6-LEN(CounterData[Day]))

Date = DATE(CounterData[Year],CounterData[Month],CounterData[Day])

DateTime = CounterData[Date]+CounterData[Right]
1.PNG

In Import mode, you can directly split the RecordTime column in Query Editor and the splited columns can be changed to Date and time format.
1-2.PNG
2.PNG


Then you can create a calculated column using DAX: DateTime=CounterData[RecordTime.1]+CounterData[RecordTime.2]

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yuezhe-msft
Employee
Employee

@anikettakalkar,

I make a test using the sample table below. 
1.PNG

In DirectQuery mode, create the following columns.

Left = LEFT(CounterData[RecordTime],FIND(" ",CounterData[RecordTime],1,0)-1)

Right = RIGHT(CounterData[RecordTime],LEN(CounterData[RecordTime])-LEN(CounterData[Left])-1)

After creating the above columns, change data type of Right column to Time. Then create the following columns.

Year = LEFT(CounterData[Left],4)

Day = RIGHT(CounterData[Left],LEN(CounterData[Left])-FIND("/",CounterData[RecordTime],6,1))

Month = MID(CounterData[Left],6,LEN(CounterData[Left])-6-LEN(CounterData[Day]))

Date = DATE(CounterData[Year],CounterData[Month],CounterData[Day])

DateTime = CounterData[Date]+CounterData[Right]
1.PNG

In Import mode, you can directly split the RecordTime column in Query Editor and the splited columns can be changed to Date and time format.
1-2.PNG
2.PNG


Then you can create a calculated column using DAX: DateTime=CounterData[RecordTime.1]+CounterData[RecordTime.2]

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.