基于列表的条件格式
Conditional formatting based on lists
我是 M 的新手,想根据列表中的值创建一个 "if, then, else statement"。
基本上我有 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"
...
多个值
它是针对多个值我不知道如何创建逻辑
如果标签包含超过 1 个来自 FoodCompanies 而不是来自 ClosthingCompanies 或 Techcompanies 的值,我希望它是 "FoodCompanies"
如果标签包含超过 1 个来自 ClothingCompanies 而不是来自 FoodCompanies 或 Techcompanies 的值,我希望它是 "ClothingCompanies"
如果标签包含来自 AllCompanies 的 2 个值,它应该是 "MixedCompanies"
如果标签包含来自 AllCompanies 的所有值,它应该是 "AllofThem"
...
路上有人可以帮我吗?我会这样做
else if List.Count(FoodCompanies) > 1 and ( List.Count(ClothingCompanies) < 1 or List.Count(Techcompanies) < 1) then "FoodCompanies"
但我如何根据标签值对其进行评估?
这是一种方法,将您的公司列表转换为 table,匹配标签值,过滤结果,然后确定输出:
#"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
我是 M 的新手,想根据列表中的值创建一个 "if, then, else statement"。
基本上我有 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"
...
多个值
它是针对多个值我不知道如何创建逻辑
如果标签包含超过 1 个来自 FoodCompanies 而不是来自 ClosthingCompanies 或 Techcompanies 的值,我希望它是 "FoodCompanies"
如果标签包含超过 1 个来自 ClothingCompanies 而不是来自 FoodCompanies 或 Techcompanies 的值,我希望它是 "ClothingCompanies"
如果标签包含来自 AllCompanies 的 2 个值,它应该是 "MixedCompanies"
如果标签包含来自 AllCompanies 的所有值,它应该是 "AllofThem"
...
路上有人可以帮我吗?我会这样做
else if List.Count(FoodCompanies) > 1 and ( List.Count(ClothingCompanies) < 1 or List.Count(Techcompanies) < 1) then "FoodCompanies"
但我如何根据标签值对其进行评估?
这是一种方法,将您的公司列表转换为 table,匹配标签值,过滤结果,然后确定输出:
#"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