昨天我发现并开始使用Power Query。我已经编写了一些查询来填充三个下拉列表,第二个和第三个依赖于第一个中选择的内容,第三个则进一步依赖于第二个中选择的内容。
当我在这些列表中创建可能的项目列表时,我在每个顶部添加一个“全部”记录作为我的查询的一部分。我在填充第二个列表时使用了if语句,如果用户在第一个框中选择了All,则基本上会显示,去获取所有记录,否则返回特定列中的值等于已选择的值的记录第一个清单。
但是,由于第三个列表需要检查是否在第一个和第二个列表框中都选择了“全部”,我相信我需要做两个if语句。我昨天整个下午都在互联网上搜索了两个嵌套if语句的例子,但是每当我尝试自己这样做时,我会得到各种语法错误,我错过了令牌Eof或令牌,或者逗号是失踪等等。
有人能告诉我一个如何布置嵌套if语句的简单示例吗?
谢谢
更新:
感谢您的回答。我还有真正的问题。这是我的查询,目前有效。
if Wholesaler_Cmbo_Lnkd_Cell <> "All wholesalers" then
let
Source = Table.SelectRows(Query_GSR, each [Wholesaler number] = Wholesaler_Cmbo_Lnkd_Cell),
#"GroupedSource" = Table.Group(Source, {"Month_Name", "Year_No", "Item Category", "Item Category Type", "Tariff status", "Weekday","Transaction type","Group number","Customer account number"}, {{"Gross sales", each List.Sum([Gross sales])},
{"Rebates", each List.Sum([Rebates])},
{"Net sales", each List.Sum([Net sales])},
{"Direct materials", each List.Sum([Direct materials])},
{"Packaging", each List.Sum([Packaging])},
{"Lab consumables", each List.Sum([Lab consumables])},
{"QC costs", each List.Sum([QC costs])},
{"Stock adjustments", each List.Sum([Stock adjustments])},
{"Product margin", each List.Sum([Product margin])},
{"Gross profit", each List.Sum([Gross profit])},
{"Carriage income", each List.Sum([Carriage income])},
{"Carriage expense", each List.Sum([Carriage expense])},
{"Orders", each List.Count([ID])}})
in
#"GroupedSource"
else
let
Source = Query_GSR,
#"GroupedSource" = Table.Group(Source, {"Month_Name", "Year_No", "Item Category", "Item Category Type", "Tariff status", "Weekday", "Transaction type", "Group number", "Customer account number"}, {{"Gross sales", each List.Sum([Gross sales])},
{"Rebates", each List.Sum([Rebates])},
{"Net sales", each List.Sum([Net sales])},
{"Direct materials", each List.Sum([Direct materials])},
{"Packaging", each List.Sum([Packaging])},
{"Lab consumables", each List.Sum([Lab consumables])},
{"QC costs", each List.Sum([QC costs])},
{"Stock adjustments", each List.Sum([Stock adjustments])},
{"Product margin", each List.Sum([Product margin])},
{"Gross profit", each List.Sum([Gross profit])},
{"Carriage income", each List.Sum([Carriage income])},
{"Carriage expense", each List.Sum([Carriage expense])},
{"Orders", each List.Count([ID])}})
in
#"GroupedSource"
我知道我的布局非常糟糕 - 可以解决的问题。我宁愿它看起来像这样:
if Wholesaler_Cmbo_Lnkd_Cell <> "All wholesalers" then
let
Source = Table.SelectRows(Query_GSR, each [Wholesaler number] = Wholesaler_Cmbo_Lnkd_Cell)
in
Source
else
let
Source = Query_GSR
in
Source
let
#"GroupedSource" = Table.Group(Source, {"Month_Name", "Year_No", "Item Category", "Item Category Type", "Tariff status", "Weekday", "Transaction type", "Group number", "Customer account number"}, {{"Gross sales", each List.Sum([Gross sales])},
{"Rebates", each List.Sum([Rebates])},
{"Net sales", each List.Sum([Net sales])},
{"Direct materials", each List.Sum([Direct materials])},
{"Packaging", each List.Sum([Packaging])},
{"Lab consumables", each List.Sum([Lab consumables])},
{"QC costs", each List.Sum([QC costs])},
{"Stock adjustments", each List.Sum([Stock adjustments])},
{"Product margin", each List.Sum([Product margin])},
{"Gross profit", each List.Sum([Gross profit])},
{"Carriage income", each List.Sum([Carriage income])},
{"Carriage expense", each List.Sum([Carriage expense])},
{"Orders", each List.Count([ID])}})
in
#"GroupedSource"
它说的是一个令牌Eof是预期的,当我试图将整个东西包装在另一个“let”和“in”中时,它表示在第一个#“GroupedSource”上有一个令牌逗号。我尝试过各种各样的“in”和“let”包含但很明显我错过了对“let”,“in”以及如何分配“变量”的答案的基本理解。
你知道我做错了吗?
答案 0 :(得分:0)
在PowerQuery中嵌套的if语句如下所示:
if [condition1]
then (if [condition2] then [true1-true2 result] else [true1-false2 result])
else [false result]
您还可以将if添加到else条件:
if [condition1]
then (if [condition2] then [true1-true2 result] else [true1-false2 result])
else (if [condition3] then [false1-true3 result] else [false1-false3 result])
答案 1 :(得分:0)
好的完全不同,这可能是我学习如何正确使用Power Query。我用以下代码实现了同样的目的:
let
Source = Query_GSR,
#"Added Custom" = Table.AddColumn(Source, "Orders", each if [Transaction type] = "Credit" then -1 else 1),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "WorkOutFilter", each if Wholesaler_Cmbo_Lnkd_Cell = "All wholesalers" then "Keep" else if [Wholesaler number] = Wholesaler_Cmbo_Lnkd_Cell then "Keep" else "Remove"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([WorkOutFilter] = "Keep")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"WorkOutFilter"})
in
#"Removed Columns"
原谅。我需要思考的不像是我编写代码而更像是这样,我在每条记录而不是代码中使用if语句!
再次感谢您的帮助 - 它确实帮助了一分钱。
答案 2 :(得分:0)
看起来你可以用Table.SelectRows(Power Query相当于Excel中的过滤器)来实现相同的功能。您的代码修改如下。
let
Source = Query_GSR,
#"Added Custom" = Table.AddColumn(Source, "Orders", each if [Transaction type] = "Credit" then -1 else 1),
Narrow = if Wholesaler_Cmbo_Lnkd_Cell = "" then #"Added Custom"
else if Wholesaler_Cmbo_Lnkd_Cell = "All wholesalers" then #"Added Custom"
else Table.SelectRows(#"Added Custom", each [Wholesaler number] = Wholesaler_Cmbo_Lnkd_Cell)
in
Narrow
&#13;
希望它有所帮助。您可能希望在Wholesaler_Cmbo_Lnkd_Cell输入上使用数据验证。
杰克