在Power Query中将文本格式错误地转换为数字

我从商业系统下载了一个价格列,根据国家的不同,这些数字有不同的格式,例如:

1.260,14 -> this is seen as text by Excel 1,280.14 -> this is seen as text by Excel 1280.14 -> this is the only correct one, seen as number 

我希望Power Query把所有的数字都转换成数字,这意味着所有的3个数字应该是:“1280.14”

1)在小数点位置分割列:按字符数分割列:2 – 一次尽可能正确

2)第一列:replace“。” 由“”(无)

3)第一列:用“”replace“,”(无)

4)用“。”合并两列。 作为分隔符并更改为十进制格式

如果您知道哪些文档来自哪个国家/地区,则可以在使用Table.TransformColumnTypes时考虑到区域设置。 您可以右键单击该列并select更改types| 使用区域设置…。 这将生成类似Table.TransformColumnTypes(Step, {{"Column Name", Currency.Type}}, "locale name")

下面是我刚才用混合格式文本作为数字强制查询function。 它认为点或逗号的最后一次出现是小数点分隔符,只要该字符在文本中只出现一次即可。

要使用此function,请转到“Power Query”function区选项卡,单击“从其他来源”>“空白查询”。 然后进入“高级编辑器”并复制下面的脚本到编辑器并保存。 然后,您可以返回到主查询,然后单击“添加列”>“添加自定义列”。 “=”后面的公式是: 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