Home » excel » excel – DAX LOOKUPVALUE on text/string

excel – DAX LOOKUPVALUE on text/string

Posted by: admin May 14, 2020 Leave a comment

Questions:

I would like to create some kind of LOOKUPVALUE on text in DAX that will match a sentence with a particular keyword. For instance in the example below the second and third row have a hit because “Apple” and “Chicken” is in the string. The problem is that the text is in a string and not a standalone value.

Table 1

enter image description here

Table 2

enter image description here

Output

enter image description here

How to&Answers:

EDIT, improved answer: this new version also works when there are multiple keys in one string.

I think PowerQuery is the natural place to perform an operation like this.

The Output table would look like this:

enter image description here

A description of the applied steps:

  1. Source: a reference to Table1
  2. Added Column Key lists: adds a custom column with lists of the Table2[Key] value(s) that are in the [String] value. This is the logic for this Custom column:

enter image description here

For each row the function selects the values from the Table2[Key] column that it finds in the [String] value. It then returns a list that holds only the selected values.

  1. Expanded Key list: expands the lists in the [Key] column
  2. Join with Table2 on Key: Joins with Table2 on the Key Value
  3. Expanded Table2: Expands the table values in the [ItemTables] column and keeps the [Item] column
  4. Group and concate keys/items: Groups the Output table on String, concatenating the Keys and the Items. If you don’t want to see the [Key] column, delete {"Key", each Text.Combine([Key], " | "), type text}, from this step

The script in the Advanced Editor looks like this:

let
    Source = #"Table1",
    #"Added Column Key lists" = Table.AddColumn(Source, "Key", (r) => List.Select(Table.Column(Table2,"Key"),each Text.Contains(r[String],_,Comparer.OrdinalIgnoreCase)),type text),
    #"Expanded Key lists" = Table.ExpandListColumn(#"Added Column Key lists", "Key"),
    #"Join with Table2 on Key" = Table.NestedJoin(#"Expanded Key lists", {"Key"}, Table2, {"Key"}, "ItemTables", JoinKind.LeftOuter),
    #"Expanded ItemTables" = Table.ExpandTableColumn(#"Join with Table2 on Key", "ItemTables", {"Item"}, {"Item"}),
    #"Group and concate keys / items" = Table.Group(#"Expanded ItemTables", {"String"},{{"Key", each Text.Combine([Key], " | "), type text},{"Item", each Text.Combine([Item], " | "), type text}})
in
    #"Group and concate keys / items"

Here is a link to my .pbix file

Answer:

I created the following dummy data sets.

enter image description here

enter image description here

My interpretation of what your after is to Identify if a sentence contains a key word.

This can be done via a calculated column with the following formula –

 Lookup = LOOKUPVALUE(Table2[Result],Table2[LookUp], IF(SEARCH("Apple",Table1[Sentence],,0)> 0, "Apple",""))

You can combine the If and Search Functions with the Lookupvalue function.

The formula is searching for the word “Apple” and then returning its position within the text and if no result is found, displays 0.

The IF statement then takes any result greater then 0, as anything greater then 0 means a result has been found and that is its position within the string, and states “Apple”. This then becomes your lookup value.

This then displays as bellow

enter image description here

You can then replace the Blank (“”) that is currently the result if false, with another if statement to look for another key word such as “Orange” and then add add that to your lookup table to pull through the result your after.

Hope this makes sense and helps!

Answer:

Try this formula (see the picture which cell is where in my assumptions):

=IFERROR(INDEX($B$7:$B$9,MATCH(1,--NOT(ISERROR(FIND($A$7:$A$9,$A12))),0)),"-")

enter image description here