简体   繁体   English

电源查询

[英]Power Query excel

i am trying to use a if formula in power query.我正在尝试在电源查询中使用 if 公式。 For example, if the Column contains “guy” then value is male and the false value is “female”.例如,如果 Column 包含“guy”,则值为 male,false 值为“female”。 I tried different ways and I can't find the right formula to use in power query.我尝试了不同的方法,但找不到在幂查询中使用的正确公式。 Can anyone help me please?谁能帮帮我吗?

如果您将其输入到“ Add Custom Column ”对话框中,则类似于(不区分大小写):

= if Text.Contains([Column],"guy", Comparer.OrdinalIgnoreCase) then "male" else "female"

It sounds like you are wanting to conditionally replace values in a column, based on the existing values in that column?听起来您想根据该列中的现有值有条件地替换该列中的值? If so, you can use the Table.ReplaceValue function in Power Query:如果是这样,您可以在 Power Query 中使用 Table.ReplaceValue function:

= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else "female",
Replacer.ReplaceText,{"Gender"})

That will change all values of "guy" to "male', and ALL other values to "female", as you stated.正如您所说,这会将“guy”的所有值更改为“male”,并将所有其他值更改为“female”。

You can also leave values in place that don't meet the criteria, by simply referencing the column name instead of a specifying a new value:您还可以通过简单地引用列名而不是指定新值来保留不符合条件的值:

= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else [Gender],
Replacer.ReplaceText,{"Gender"})

Create a table with a column called Gender, and load it to power query.创建一个包含名为 Gender 的列的表,并将其加载到电源查询中。 Right-click on the column header and choose Replace Values to get the UI to build your statement for you, then replace the generated code with the above modification(s) and apply to your actual requirements.右键单击列 header 并选择替换值以获取 UI 来为您构建语句,然后将生成的代码替换为上述修改并应用于您的实际需求。 The key is using the each expression to tell Power Query to test at the row value level.关键是使用each表达式告诉 Power Query 在行值级别进行测试。 If you omit each , you'll see the error:如果省略each ,您将看到错误:

"Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?" “Expression.Error:有一个未知的标识符。您是否在“每个”表达式之外使用了 _[field] 的 [field] 简写?

= Table.AddColumn(#"Changed Type","ColumnName",each if[Column] ="""Guy""" then"""Male""" else"""Female""") = Table.AddColumn(#"Changed Type","ColumnName",each if[Column] ="""Guy""" then"""Male""" else"""Female""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM