You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power BI中多数据类型列转换为Fixed decimal number的方法求助

Power BI 转换混合类型old_price列为固定小数

解决方案(Power Query编辑器处理)

针对你列中的null、纯数字、SR XXXX、SR XXX/X pack等混合格式,可通过Power Query的M语言分步清洗转换:

步骤1:进入Power Query编辑器

点击Power BI界面顶部的转换数据,打开Power Query编辑器。

步骤2:添加自定义列处理数据

选中old_price列,点击添加列 → 自定义列,输入以下M代码(根据你的需求选择其一):

需求1:提取价格数值(忽略"/X pack"的数量,只取前面的价格)
let
    textValue = Text.From([old_price]),
    removeSR = Text.Replace(textValue, "SR ", ""),
    extractNum = Text.BeforeDelimiter(removeSR, "/"),
    cleanNum = Text.BeforeDelimiter(extractNum, " "),
    noComma = Text.Replace(cleanNum, ",", ""),
    result = try Number.From(noComma) otherwise null
in
    result
需求2:计算单价(比如SR 445/2 pack按445÷2=222.5处理)
let
    textValue = Text.From([old_price]),
    removeSR = Text.Replace(textValue, "SR ", ""),
    splitParts = Text.Split(removeSR, "/"),
    priceText = if List.Count(splitParts) > 1 then Text.BeforeDelimiter(splitParts{0}, " ") else Text.BeforeDelimiter(removeSR, " "),
    qtyText = if List.Count(splitParts) > 1 then Text.BeforeDelimiter(splitParts{1}, " ") else "1",
    cleanPrice = Text.Replace(priceText, ",", ""),
    cleanQty = Text.Replace(qtyText, ",", ""),
    result = try Number.From(cleanPrice) / Number.From(cleanQty) otherwise null
in
    result

步骤3:设置数据类型为固定小数

添加完自定义列后,选中新列,在转换选项卡的数据类型下拉菜单中选择固定小数。

步骤4:应用更改

点击编辑器顶部的关闭并应用,完成转换。

代码说明

  • Text.From([old_price]):统一将混合类型转为文本,避免类型冲突。
  • Text.Replace(..., "SR ", ""):移除价格前缀"SR "。
  • Text.BeforeDelimiter:提取分隔符(斜杠/空格)前的有效数字部分。
  • try ... otherwise null:处理异常格式数据,避免转换报错,返回null。

内容的提问来源于stack exchange,提问作者Tran Nhung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 04:17:22