简体   繁体   中英

Power BI: Convert text (yyyymmdd) to date (dd/mm/yyyy)

I'm specifically trying to convert string in Column A : yyyymmdd to dd/mm/yyyy date format using Power Query Editor in Power BI. I can already perform this in Excel using the formula below: Any ideas

Excel

=DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2))

Solution 1 : Highlight the specific column , and on the Transform tab you can select Detect Data Type.
Solution 2 : Try to create a new column named [dateFormatted] and apply the formula as follows:

dateFormatted = Date(Left([date],4),Right(left([date],6),2),right([date],2)

select the new column and change its type to date as follows:

在此处输入图像描述

[dateFormatted] will now be of type date, formatted as: dd Mmm yyyy

Solved: Highlight column in question, and on the Transform tab, select Detect Data Type. This switches it from String to Whole number. Then alter Data Type to Date. format provided is dd/mm/yyyy. Hope someone else finds this useful

Just set the datatype to date.

M-Code although can be done through the GUI

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}})
in
    #"Changed Type"

使用示例中的插入列,我将DDMMYYYY (转换为文本)转换为DD/MM/YYYY (转换为日期),结果如下:

= Table.AddColumn(#"Changed Type", "OrganisationStartDate", each Text.Combine({Text.Start([organisation_start_date], 2), "/", Text.Middle([organisation_start_date], 2, 2), "/", Text.Middle([organisation_start_date], 4)}), type text)

您可以使用 Date.FromText 函数。

= Date.ToText( Date.FromText( "20221231" ), "dd/MM/yyyy" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM