这是我刚刚将混合格式文本转换为数字的 Power Query 函数。它将最后一次出现的点或逗号视为小数分隔符,只要该字符在文本中仅出现一次。
要使用此功能,请转到“Power Query”功能区选项卡,单击“From Other Sources”>“Blank Query”。然后转到“高级编辑器”并将下面的脚本复制粘贴到编辑器并保存。然后,您可以返回主查询并单击“添加列”>“添加自定义列”。“=”之后的公式是:toNumber([column name])
。
let
toNumber = (text) =>
let
//remove characters that occur more than once as they can't be decimal separators
text1 = if List.Count(Text.PositionOf(text, ",", Occurrence.All)) > 1
then Text.Replace(text, ",", "") else text
, text2 = if List.Count(Text.PositionOf(text1, ".", Occurrence.All)) > 1
then Text.Replace(text1, ".", "") else text1
//if there are still more than one potential decimal separator, remove the kind that occurs first
//let's assume the last one is the actual decimal separator
, text3 = if List.Count(Text.PositionOfAny(text2, {",","."}, Occurrence.All)) > 1
then Text.Replace(text2, Text.At(text2, Text.PositionOfAny(text2, {",","."}, Occurrence.First)), "")
else text2
//cast as number (try different decimal separators)
, number =
try Number.ToText(Number.From(Text.Replace(text3,",",".")))
otherwise Number.ToText(Number.From(Text.Replace(text3,".",",")))
in
number
in
toNumber