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
Anonymous
Not applicable

Replace a set of date/time entries

Hello. I have a set of excel files which I compile in Access in order to analyse them. After append the 5 files into a access table, I import to Power Query in order to manipulate them. 

 

The problem is the data format, it's not the same in the 5 files. One of the errors is in a Date Column.  This date column is in text format but when I convert to date/time format, I have a lot of errors. In the Date Column, I have fields with date and time, only time and these kind of stuff:

Date_Column

2020-06-19 17:27:51.130000000

 

First: I want to convert all of these rows that have 2020-06-19 17:27:51.130000000 format to 2020-06-19 17:27:51. So, remove the numbers after the dot. 

Second: Convert all entries into Date/time format even with the only date entries.

 

Anyone can help me?

 

Thank you!

1 ACCEPTED SOLUTION

Hi @Anonymous, let me see if I understand. You are using this formula:

 

= DateTime.Date(DateTime.FromText([YourColumnHere], "pt-PT"))

 

But your issue is the hours/min/sec are all zeros even if they exist.

If I have that right, here is why.

  1. The outer function, DateTime.Date() stripped all time from the field after DateTime.FromText() got it.
  2. You later formatted that custom column as type datetime, so it automatically puts the time out there, and if none, it will have 12am with zeros for minutes and seconds.

You could trap this with something like this in a new Custom Column:

 

let
     varDateTime = DateTime.FromText([DataEncomenda], "pt-PT"),
     varDate = DateTime.Date(varDateTime)
in
     if varDateTime = DateTime.From(varDate)
     then varDate
     else varDateTime

 

It returns this from your sample data that I tweaked on the first two rows. I used variables above because I didn't want to type those functions in a dozen times.

edhans_0-1598478863805.png

What my formula did is:

  • let it do the DateTime.FromText() conversion - stored in varDateTime
  • Then converted to date only with DateTime.Date(varDateTime) - stored in varDate
  • Then I compared the varDateTime to varDate converte back to Datetime - using DateTime.From(varDate)
  • If varDateTime has bunch of zeros, then they are the same, because DateTime.From(varDate) has no time, so just zeros.
  • If they are different, then varDateTime must have valid time codes.

I keep varDateTime if there are valid times, and keep varDate if there are none or the times are all zeros.

 

Make sense?
Also note that If I then format that final column as DateTime, PQ will put the zero times back in. It essentially does a DateTime.From() on the entire column.

edhans_0-1598479154382.png

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

11 REPLIES 11

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.

Top Solution Authors
Top Kudoed Authors