Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Migasuke

Dynamic text search in Power Query

Use case description

Imagine we own several stores in one big shopping mall. We noticed that people write their reviews about all stores on the website of the shopping mall. What does it mean? It means we have hundreds or even thousands of reviews and comments in one single page but majority of those comments are irrelevant to us since we own only few shops in the mall and not every single one of them.

Main idea of our solution has following steps:

     1. We provide list of our stores.

     2. We search if those stores are mentioned in any of the comment.
     3.
We keep only those comments which are related to our stores.

Of course, everything should be automatic and possible to run in PBI Service.

 

Initial Set up

To keep the example simple I will work with only two queries:

1. First query is called “Comments” – this is the query which contains all the comments from the Shopping mall. Example is following:

Migasuke_0-1682769889508.png

 

2. The next query is related to our companies called Companies. It’s basically a list of the text strings we want to find:

Migasuke_1-1682769963559.png

So basically we search for three companies called Super Buyyy, Order Anything 27/7 and Massive Discounts ABC.


Solution

1. Let’s start with a very first step. If you have a table similar to my Companies table, be sure you change the table into the list. In case you don’t know how to do it, we can simply do right click on the column header and select Drill Down:

Migasuke_2-1682770099750.png

You should notice that the icon of the query has also changed:

 

Migasuke_3-1682770123612.png

Changing the table to a list is a crucial step because then we can take advantage of List functions in Power Query.


2. In the next step we will go to the Comments table and add a new custom column. Syntax for the query is very simple and we basically refer to our created Companies list:

Migasuke_4-1682770192080.png
Result should look like this:

Migasuke_5-1682770215021.png

 


3. The same way as we did before we will create a custom column, which contains find logic. This time the code is a bit more tricky:

Migasuke_6-1682770256322.png

Now let’s look more closely into the formula.
List.Contains is a function which validates if some of our values from Companies List can be found within Comments column and returns TRUE or FALSE. Basic formula would work only if we had 100% match of the value from List with our Comments. Example can be seen below:

Migasuke_7-1682770310875.png

Since we always compare Comments, which are different from our initial list values, we need to adjust the code by the equation criteria which would be:

(x as text, y as text)=>Text.Contains(Text.Lower(y),Text.Lower(x))

What does the formula above mean? Basically:

  • We define variables x and y as a text.
  • x is represented as our Companies List.
  • y is represented as our Comments.
  • Text.Lower ensures that we work only with lower case text (Super Buyyy vs super buyyy is not the same in terms of comparison)
  • Text.Contains compares our Comments with the List values.

 

Result of our new column should look like this:

Migasuke_8-1682770389878.png
In the final step we can just remove column with Lists and apply filter on values equal to TRUE so we exclude companies we are not interested in:

Migasuke_9-1682770414913.png

 




 




 



 






 

 

 

 

 

 

Comments