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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
cathoms
Responsive Resident
Responsive Resident

Error with date function - calculated date column from text

I have a column of dates in text format YYYMMDD and want to create a calculated column that converts those to date format. The following should work:

 

 

ApptCreationDate =
DATE(
     LEFT(Visits[AppointmentCreationDateKey],4),
     MID(Visits[AppointmentCreationDateKey],5,2),
     RIGHT(Visits[AppointmentCreationDateKey],2)
)

 

 

Unfortunately, there are some bad data in the column that need to be filtered out. I tried the following but it isn't working. I get a "Operator or expression'()' is not supported in this context. What am I doing wrong? 

 

ApptCreationDate = DATE(
    IF(Visits[AppointmentCreationDateKey]=-1, 
        FALSE(),
        (LEFT(Visits[AppointmentCreationDateKey],4),
        MID(Visits[AppointmentCreationDateKey],5,2),
        RIGHT(Visits[AppointmentCreationDateKey],2)
        )
    )
)

 

I also tried the following with the same result:

ApptCreationDate = IF(
    Visits[AppointmentCreationDateKey]=-1,
    BLANK(),
    DATE(
        (LEFT(Visits[AppointmentCreationDateKey],4),
        MID(Visits[AppointmentCreationDateKey],5,2),
        RIGHT(Visits[AppointmentCreationDateKey],2)
        )
    )
)

 

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@cathoms try this, if there is error in conversion it will return blank() as catch-all errors otherwise return a date

 

ApptCreationDate = 
    IFERROR (  
        DATE 
        (
           LEFT(Visits[AppointmentCreationDateKey],4),
           MID(Visits[AppointmentCreationDateKey],5,2),
           RIGHT(Visits[AppointmentCreationDateKey],2)
        ), BLANK()
    )

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

7 REPLIES 7
parry2k
Super User
Super User

@cathoms try this, if there is error in conversion it will return blank() as catch-all errors otherwise return a date

 

ApptCreationDate = 
    IFERROR (  
        DATE 
        (
           LEFT(Visits[AppointmentCreationDateKey],4),
           MID(Visits[AppointmentCreationDateKey],5,2),
           RIGHT(Visits[AppointmentCreationDateKey],2)
        ), BLANK()
    )

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

cathoms
Responsive Resident
Responsive Resident

Thanks for the great help!

 

Cheers   - Christopher

cathoms
Responsive Resident
Responsive Resident

That works, but it is returning a column with a date time stamp. So the date is fine but they all indicate 12:00:00 AM. Documentation on the DATE function says it is returned as datetime. I don't want that visible so I changed the format to m/d/yyy in the Column Tools tab:

cathoms_1-1629299378797.png

 

cathoms
Responsive Resident
Responsive Resident

There's some bad data in this column. Unfortunately I'm connecting to this dataset and can't transform it directly, so I'm not entirely sure what all the bad values are. Looks like there is at least a -1 and maybe a " instead of the text values...

 

So, while Pat's DAX allows me to create the column, it doesn't work in the table visual. Instead I get this:

cathoms_0-1629291082360.png

 

mahoneypat
Microsoft Employee
Microsoft Employee

It would be much easier to do this in the query editor, but you need to wrap each of your text strings you parsed out with VALUE() to convert them to a number for the DATE function to use.

 

Date = DATE(VALUE(LEFT(DateText[DateText],2)), VALUE(MID(DateText[DateText],5,2)), VALUE(RIGHT(DateText[DateText],2)))
 
Pat




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


PaulDBrown
Community Champion
Community Champion

Please follow the recommendations in this thread to help us help you:

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1964858#M7448... 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






parry2k
Super User
Super User

@cathoms I guess it is a text column, try this;

 

ApptCreationDate = DATE(
    IF(Visits[AppointmentCreationDateKey]="-1", 
        BLANK(),
        (LEFT(Visits[AppointmentCreationDateKey],4),
        MID(Visits[AppointmentCreationDateKey],5,2),
        RIGHT(Visits[AppointmentCreationDateKey],2)
        )
    )
)

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.