如何在Power Query中将排名数字转换为序数形式
在Power Query中将数字转换为序数形式的方法
要把排名数字转换成1st、2nd这类序数格式,可以通过自定义M函数实现,核心是根据数字的末尾1-2位判断后缀:
创建自定义函数
在Power Query编辑器中,点击「主页」→「新建源」→「空白查询」,将查询重命名为ConvertToOrdinal,然后在高级编辑器中替换为以下代码:let ConvertToOrdinal = (number as number) as text => let numText = Text.From(number), lastDigit = Text.End(numText, 1), lastTwoDigits = if Text.Length(numText) >= 2 then Text.End(numText, 2) else lastDigit, suffix = if List.Contains({"11", "12", "13"}, lastTwoDigits) then "th" else if lastDigit = "1" then "st" else if lastDigit = "2" then "nd" else if lastDigit = "3" then "rd" else "th", result = numText & suffix in result in ConvertToOrdinal应用函数到目标列
回到你的数据查询,点击「添加列」→「自定义列」,在公式框中输入:ConvertToOrdinal([排名])替换
[排名]为你实际的数字列名称,确认后即可生成序数格式的新列。规则验证
该函数会自动处理特殊情况:- 11、12、13无论末尾是1/2/3,都用
th后缀(如11th、13th) - 末尾为1的数字(非11)用
st(如1st、31st) - 末尾为2的数字(非12)用
nd(如2nd、52nd) - 末尾为3的数字(非13)用
rd(如3rd、23rd) - 其余数字统一用
th(如4th、10th、20th)
- 11、12、13无论末尾是1/2/3,都用
内容的提问来源于stack exchange,提问作者val
相关产品推荐
相关产品推荐

