Forum Discussion
Convert different date formats which are in string format to a single common date format
Convert different date formats which are in string format to a single common date format called CommonDate. How do we write it in DAX for the same. There can be different formats, ho do we achive a common date format as a new column
| OriginalDate | Invoice | Amount | Date Format |
| 11/11/2023 | B354 | 999 | MM/DD/YYYY |
| 10.10.2023 | 3456t | 9875 | DD.MM.YYYY |
| 09.09.2023 | C876 | 876 | DD.MM.YYYY |
| 01-12-2023 | F654 | 6577 | DD-MM-YYYY |
| 06.30.2023 | U5678 | 9122 | MM.DD.YYYY |
| 2023.12.31 | UX123 | 8765 | YYYY.MM.DD |
| 8.8.2023 | FR143 | 1234 | D.M.YYYY |
2 Replies
- speedramps
Super User
It is best pratoce to data cleanse your source data before importing it.
In your example there is no automated method to decide if 11/11/2023 is dd/mm/yy or mm/dd/yy. - danextian
Super User
hi manjeshjk ,
My proposal is to use M instead of DAX as using the latter would result to quite a lengthy formula.
Thes first sample below dynamically extracts that date elements from a text string and assigns them to Y, M or D column depending on whether the text strings between each delimiter contains such a letter. Those columns are then assigned to #date(year, month, day).
The second one returns the same result but is less dynamic such that it is dependent on the provided date format. If the data updates and there is a new date format, the result becomes blank.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc5NDgIhDAXgu7AeCuWfrZLZsTEx0UzmFt4/viJiImn6Nh9tj0MxG5SzzqtNXXwMiForeu+mNfPEU+cGaAk1oQ8xvUSWHBGtUe+0qK2EmvRackJ8+j9kzU5PuKexPMWch9S9659M5Nf2e0y5yHZ2bhxKmLuoIGJHnoU+eHzBejlUEA0/aKHynbnfOEiCyxW4c0483w==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OriginalDate = _t, Invoice = _t, Amount = _t, #"Date Format" = _t]), #"Replaced Value" = Table.ReplaceValue(Source,"/"," ",Replacer.ReplaceText,{"OriginalDate", "Date Format"}), #"Duplicated Column" = Table.DuplicateColumn(#"Replaced Value", "Date Format", "Date Format 2"), #"Replaced Value1" = Table.ReplaceValue(#"Duplicated Column","."," ",Replacer.ReplaceText,{"OriginalDate", "Date Format 2"}), #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","-"," ",Replacer.ReplaceText,{"OriginalDate", "Date Format 2"}), #"Added Custom" = Table.AddColumn(#"Replaced Value2", "YrMoDay", each let split = Text.Split([Date Format 2], " ") in List.Transform(split, each if Text.Contains(_, "Y") then "Y" else if Text.Contains(_, "M") then "M" else if Text.Contains(_, "D") then "D" else null)), #"Added Custom1" = Table.AddColumn(#"Added Custom", "DateElements", each Text.Split([OriginalDate], " ")), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Table", each Table.PromoteHeaders(Table.Transpose(Table.FromColumns({ [YrMoDay], [DateElements]})))), #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Date", each let y = Number.From([Table][Y]{0}), m = Number.From([Table][M]{0}), d = Number.From([Table][D]{0}) in #date(y,m,d),type date), #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"YrMoDay", "DateElements", "Table", "Date Format 2"}) in #"Removed Columns"let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc5NDgIhDAXgu7AeCuWfrZLZsTEx0UzmFt4/viJiImn6Nh9tj0MxG5SzzqtNXXwMiForeu+mNfPEU+cGaAk1oQ8xvUSWHBGtUe+0qK2EmvRackJ8+j9kzU5PuKexPMWch9S9659M5Nf2e0y5yHZ2bhxKmLuoIGJHnoU+eHzBejlUEA0/aKHynbnfOEiCyxW4c0483w==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OriginalDate = _t, Invoice = _t, Amount = _t, #"Date Format" = _t]), #"Replaced Value" = Table.ReplaceValue(Source,"/"," ",Replacer.ReplaceText,{"OriginalDate"}), #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","."," ",Replacer.ReplaceText,{"OriginalDate"}), #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","-"," ",Replacer.ReplaceText,{"OriginalDate"}), #"Added Custom" = Table.AddColumn(#"Replaced Value2", "DateElements", each Text.Split([OriginalDate], " ")), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Date", each let x = [Date Format], e1 = Number.From([DateElements]{0}), e2 = Number.From([DateElements]{1}), e3 = Number.From([DateElements]{2}) in if x = "MM/DD/YYYY" or x = "MM.DD.YYYY" then #date(e3, e1, e2) else if x = "DD.MM.YYYY" or x = "DD-MM-YYYY" or x = "D.M.YYYY" then #date(e3, e2, e1) else if x = "YYYY.MM.DD" then #date(e1, e2, e3) else null, type date), #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"DateElements"}) in #"Removed Columns"