Home » excel » sql server – How to convert a date by using DAX functions?

sql server – How to convert a date by using DAX functions?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m working on a PowerPivot project, and I need to convert a date to another format.
My SQL Server Analysis Cube provides me a Time dimension including a date attribute.
I want to convert it to a dd/mm/yyyy format to create a reference to another data source (Excel file).

enter image description here

I tried to convert it by using standard DAX date functions but it’s not recognized as a date, it’s seems it is due to the name of the day added as a prefix.
How can I transform it to the dd/mm/yyyy format ? How to extract the sub string after the comma ?

Thanks !

How to&Answers:

I used the following data to test my solution out.

enter image description here

You can use the SEARCH function to find the first instance of a string. So I can parse just the date portion out with the following formula:

=right([Datefield],(LEN([Datefield])-SEARCH(",",[Datefield])-1))

It gets the substring starting at the character after the comma through the end of the string.

The DATEVALUE function takes a string that represents a date and turns it into a date. I can combine that with my previous function:

=datevalue(right([Datefield],(LEN([Datefield])-SEARCH(",",[Datefield])-1)))

In the picture below, the first column is the original data. The second column is the function that parses out the substring for the date. The third column takes the datevalue of that date string in the second column. The fourth column is the all in one formula with both the substring and the datevalue.

enter image description here

Answer:

If you will load the data from the database regularly, than I suggest you use Power Query to load data into PowerPivot Module.

Here is the sample code that can do the result for you.

Data look like this (Table2):

Date
Monday, January 12, 2014
Tuesday, January 13, 2014
Wednesday, January 14, 2014
let  
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Split Column by Delimiter" = Table.SplitColumn(Source,"Date",Splitter.SplitTextByEachDelimiter({","}, null, false),{"Date.1", "Date.2"}),
    #"Change to Date Format" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Date.1", type text}, {"Date.2", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Change to Date Format",{"Date.1"})
in 
    #"Removed Columns"

Answer:

As @mmarie explained, the folowing formula works well (need some changes) :

=datevalue(right([Datefield];(LEN([Datefield])-SEARCH(“,”;[Datefield])-1)))

<a href="https://exceptionshub.com/category/sql-server”>More Answers>