如何从另一张表中搜索一行子字符串?

时间:2019-09-11 02:03:04

标签: dax powerquery powerbi-desktop m

说我有3列的table1:

Source  Category   Type
DOG     Carnivore  mammal
CAT     Carnivore  mammal
GOAT    Herbivore  mammal
LIZARD  Carnivore  Reptile

我有table2,其中包含很多我不关心的文本/字符,但确实在表1的源列中找到了一个子字符串:

Description                                             Type (New Column)
nonsensetext345 ka dfne DOG ke 344ab 09/06
unneededtextandnumbers GOAT MoreIrrelavantTextBlahBLah

如何在table2中添加另一列,该列在Description中搜索table1的源列中的匹配子字符串并返回关联的类型?

2 个答案:

答案 0 :(得分:1)

//Table1
let
    Source = #table(
    {"Source", "Category", "Type"}, 
    {{"DOG", "Carnivore", "mammal"},
     {"CAT", "Carnivore", "mammal"},
     {"GOAT", "Herbivore", "mammal"},
     {"LIZARD", "Carnivore", "Reptile"}})
in
    Source
//Table2
let
    Source = #table(
        {"Description"},
        {{"nonsensetext345 ka dfne DOG ke 344ab 09/06"},
         {"unneededtextandnumbers GOAT MoreIrrelavantTextBlahBLah"}}),
    Type =
        Table.AddColumn(
            Source,
            "Type",
            each 
                // first we grab the description into a variable
                let currentRowDescription = [Description]
                in
                    // Get only rows from Table1 that match a condition
                    Table.SelectRows(
                        Table1,
                        // The condition is Text.Contains(currentRowDescription, [Source])
                        // This means that we are taking only the rows from Table1
                        // whose value in Table1[Source] is found in the string
                        // currentRowDescription.
                        // Based on that table, we take the values in the field, [Type].
                        each Text.Contains(currentRowDescription, [Source]))[Type]),
    // we can't guarantee only one match, so we're expanding all matches here.
    #"Expanded Type" = Table.ExpandListColumn(Type, "Type")
in
    #"Expanded Type"

以上为M,受到严重评论。我们可以在DAX中采用非常相似的方法:

Type (dax) = 
// again, we grab the value of the current row's description
VAR CurrentRowDescription = 'Table2'[Description]
// Again, we filter 'TAble1' based on searching for the values in 'Table1'[Source]
// in the current row's description.
VAR MatchingRowsFromTable1 =
  FILTER (
    'Table1',
    // FIND searches a string (arg1) for a substring (arg2) from an optional starting
    // position (arg3), returning arg4 when the substring isn't found. It returns the
    // starting index of the substring. Thus if it's > 0 there's a match.
    FIND ( 'Table1'[Source], CurrentRowDescription,, 0 ) > 0
  )
// DAX needs exactly one scalar value to add as a column. Here we are defensive with
// SAMPLE which will return exactly 1 row
VAR Only1MatchingRow = SAMPLE ( 1, MatchingRowsFromTable1, 'Table1'[Type], ASC )
RETURN
  // Select just the type column.
  SELECTCOLUMNS ( Only1MatchingRow, "Type", 'Table1'[Type] )

答案 1 :(得分:0)

从table1 t1中选择*加入t2上的table2 t2.description类似于'%'+ t1.source +'%'