我已将Excel工作表导入PowerBI,并且试图将日期列从文本格式(2020年3月21日,00:00:000)转换为日期格式(21/03/2020)。 / p>
不幸的是,当我选择“检测类型”时,它仅选择文本,而选择“日期”则出现错误。我从列中删除了“ 00:00:00”,但仍然收到错误消息。
有人对我如何将该列转换为dd / mm / yyyy有任何建议吗?
非常感谢,
答案 0 :(得分:2)
在删除, 00:00:000
之后,您可以使用类似这样的转换来删除st
中的21st
部分,以便March 21st 2020 --> March 21 2020
可以自动转换为日期。
此转换将除去第一个空格之后的所有字符(空格和数字除外)
each Text.BeforeDelimiter(_, " ") & Text.Select(_, {" ", "0".."9"})
此字符将删除最后一个空格左侧的两个字符:
each Text.ReplaceRange(_, Text.PositionOf(_, " ", Occurrence.Last)-2, 2, "")
完整的查询(使用第一个选项)如下所示:
let
Source = <Your Data Source>,
Remove00 = Table.TransformColumns(Source, {{"DateText", each Text.BeforeDelimiter(_, ","), type text}}),
StripOrdinal = Table.TransformColumns(Remove00, {{"DateText", each Text.BeforeDelimiter(_," ") & Text.Select(_, {" ","0".."9"}), type text}}),
TextToDate = Table.TransformColumnTypes(StripOrdinal,{{"DateText", type date}})
in
TextToDate
答案 1 :(得分:1)
您可以使用以下PowerQuery脚本:
let
Source = Excel.Workbook(File.Contents("Dates.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Sheet1_Sheet,{{"Column1", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Column1", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
#"Added Month" = Table.AddColumn(#"Split Column by Delimiter", "Month", each if [Column1.1] = "January" then 1 else if [Column1.1] = "February" then 2 else if [Column1.1] = "March" then 3 else if [Column1.1] = "April" then 4 else if [Column1.1] = "May" then 5 else if [Column1.1] = "June" then 6 else if [Column1.1] = "July" then 7 else if [Column1.1] = "August" then 8 else if [Column1.1] = "September" then 9 else if [Column1.1] = "October" then 10 else if [Column1.1] = "November" then 11 else if [Column1.1] = "December" then 12 else 0),
#"Added Day" = Table.AddColumn(#"Added Month", "Day", each Text.Select([Column1.2], {"0".."9"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Day",{"Column1.1", "Column1.2"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1.3", "Year"}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns",{{"Year", Int64.Type}, {"Month", Int64.Type}, {"Day", Int64.Type}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type2", "Date", each Text.Combine({Text.From([Day], "de-DE"), "/", Text.From([Month], "de-DE"), "/", Text.From([Year], "de-DE")}), type text),
#"Changed Type3" = Table.TransformColumnTypes(#"Inserted Merged Column",{{"Date", type date}}),
#"Removed Columns2" = Table.RemoveColumns(#"Changed Type3",{"Year", "Month", "Day"})
in
#"Removed Columns2"
到最终数据格式的转换取决于您的本地设置,通常在PowerQuery之外完成。