带有数字和文本的 Power Query 自定义列

Power Query Custom Column with Numbers and Text

我正在尝试通过将某些值转换为数字“2”来清理我的数据,但需要保留剩余的数据和数据类型。 我在 power 查询 (excel) 中将以下代码与自定义列步骤结合使用,但在返回数字时出现错误。

Number.From([Values_old]) otherwise if Text.Contains([Value_old],"not required",Comparer.OrdinalIgnoreCase) or Text.Lower([Value_old]) = "N/A" or Text.Lower([Value_old]) ="NA" or [Value_old] = "100" or [Value_old] = 100 then 2 else [Value_old]

查看步骤的结果:

我的条件栏基于我在论坛中找到的以下评论:https://community.powerbi.com/t5/Desktop/data-type-which-contains-both-test-and-number/m-p/55785/highlight/true#M22664

但是,这似乎也打破了我的 if then else 条件。

很简单。您使用了错误的列名称,Values_old 而不是 Value_old

但是你的公式也不会起作用,因为这些都不会起作用:

Text.Lower([Value_old]) = "N/A" 或 Text.Lower([Value_old]) "NA"

因为您正在将刚转换为小写的内容与大写进行比较

所以您可能想要下面的内容,其中包括您似乎从代码中遗漏的 try 部分

= Table.AddColumn(#"Changed Type", "Custom", each try Number.From([Value_old]) otherwise if Text.Contains([Value_old],"not required",Comparer.OrdinalIgnoreCase) or Text.Lower([Value_old]) = "n/a" or Text.Lower([Value_old]) ="na" or [Value_old] = "100" or [Value_old] = 100 then 2 else [Value_old])