基于列表的条件格式

问题描述 投票:0回答:1

我是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"
...

多个值

用于多个值,我不知道如何创建逻辑

如果标签包含食品公司的多于1个值,但我不希望它是ClosthingCompanies或Tech公司的值,那么我希望它成为“食品公司”

如果标签包含服装公司的多于1个值,但食品公司或技术公司则不多,我希望它成为“服装公司”

如果标签包含AllCompanies的2个值,则应为“ MixedCompanies”

如果标签包含AllCompanies的所有值,则应为“ AllofThem”

...

有人可以在路上帮助我吗?我会喜欢

else if List.Count(FoodCompanies) > 1 and ( List.Count(ClothingCompanies) < 1 or List.Count(Techcompanies) < 1)  then "FoodCompanies"

但是如何根据标记值评估它?

excel powerquery m
1个回答
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],", ")))
© www.soinside.com 2019 - 2024. All rights reserved.