我是M的新手,并且想根据列表内的值创建一个“ if,then,else语句”。
基本上我有4个列表:
let
FoodCompanies = {"Nestlé", "Pepsico", "Unilever"},
ClothingCompanies = {"Nike", "Ralph Lauren", "Old Navy"},
TechCompanies = {"Apple", "Samsung", "IBM"},
AllCompanies = {FoodCompanies,ClothingCompanies,TechCompanies}
现在,我想创建一个条件列,以检查是否存在另一个值(并根据该值进行计算)来检查另一列(标签)。
| ItemId| DateOfSale | tag |
| 001 | 01/01/1980 | Nestlé |
| 002 | 01/01/1980 | Nike, Apple |
| 003 | 01/01/1980 | Unilever, Old Navy, IBM |
| 004 | 01/01/1980 | Samsung |
所以...我是这样开始的:
#"Added Conditional Column" = Table.AddColumn(#"Renamed Columns3", "type", each
单个值
if [tag] = "" then "Empty tag"
else if [tag] = "Nestlé" then "Nestlé"
else if [tag] = "Nike" then "Nike"
...
多个值
是针对多个我不知道如何创建逻辑的值
如果标签包含来自FoodCompany的值大于1,但不包含ClosthingCompanies或Techcompanies的值,我希望它成为“ FoodCompanies”
如果标签包含的服装公司价值大于1,而食品公司或科技公司的价值不超过1,我希望它成为“服装公司”
如果代码包含AllCompanies的2个值,则应为“ MixedCompanies”
如果标记包含AllCompanies的所有值,则应为“ AllofThem”
...
有人可以帮助我吗?我会喜欢
else if List.Count(FoodCompanies) > 1 and ( List.Count(ClothingCompanies) < 1 or List.Count(Techcompanies) < 1) then "FoodCompanies"
但是如何根据标签值对其进行评估?
答案 0 :(得分:0)
这是将公司列表转换为表格,匹配标记值,过滤结果并确定输出的一种方法:
#"Renamed Columns3" = //your previous step here
fnMatchingList = (MyList) =>
let
AllLists = #table(type table [#"ListName"=text, #"ListValues"=list],
{{"FoodCompanies",{"Nestlé", "Pepsico", "Unilever"}},
{"ClothingCompanies", {"Nike", "Ralph Lauren", "Old Navy"}},
{"TechCompanies",{"Apple", "Samsung", "IBM"}}}),
FullList = Table.ExpandListColumn(AllLists, "ListValues"),
Match = Table.AddColumn(FullList, "Match", each List.Contains(MyList,[ListValues])),
Filtered = Table.SelectRows(Match, each ([Match] = true)),
Output = if Table.RowCount(Filtered) = 1 then Filtered{0}[ListValues] else
if List.Distinct(Filtered[ListName]) = List.Distinct(FullList[ListName]) then "AllCompanies" else
Text.Combine(List.Distinct(Filtered[ListName]),", ")
in
Output,
#"Added Matching List" = Table.AddColumn(#"Previous Step", "taglist", each if [tag] = null or [tag] = "" then "(Empty Tag)" else fnMatchingList(Text.Split([tag],", ")))
编辑:为帮助理解,以下是一个独立的查询,您可以逐步执行该查询,以查看该函数的实际作用:
let
MyList = {"Pepsico", "Nike"},
AllLists = #table(type table [#"ListName"=text, #"ListValues"=list],
{{"FoodCompanies",{"Nestlé", "Pepsico", "Unilever"}},
{"ClothingCompanies", {"Nike", "Ralph Lauren", "Old Navy"}},
{"TechCompanies",{"Apple", "Samsung", "IBM"}}}),
FullList = Table.ExpandListColumn(AllLists, "ListValues"),
Match = Table.AddColumn(FullList, "Match", each List.Contains(MyList,[ListValues])),
Filtered = Table.SelectRows(Match, each ([Match] = true)),
Output = if Table.RowCount(Filtered) = 1 then Filtered{0}[ListValues] else
if List.Distinct(Filtered[ListName]) = List.Distinct(FullList[ListName]) then "AllCompanies" else
Text.Combine(List.Distinct(Filtered[ListName]),", ")
in
Output