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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
qianrusong
Frequent Visitor

Create a column to evaluate if each row contains one value of a list

Hello, I have a table with one column called fruit - apple, orange, pear, etc. i want to create a column called Fruit in the master fact table if one column called flavour description contains one of fruits. i do not want to manually type fruits in the created column function. how to address it? thanks a lot.  

 

Fruit
apple
orange
grape
pear
watermelon

 

invoicebrandFlavour DescriptionFruit
1Ared apple1
2Bgreen apple1
3Csweet orange1
4Csour lemon0
5Ayummy watermelon1

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
  Fruits = {"apple", "orange", "grape", "pear", "watermelon"}, 
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "TYw7CoAwEAWvEra28XcA9RjBIuAjTX6sCcHbu/7AYpoZGK2ppYYmgbEpk5IDrY2mTswsWAbCz/fiFmGvQFaRTbBPGL4QCysHH8Otx/d+FO8PVU0Ge7grric=", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [invoice = _t, brand = _t, #"Flavour Description" = _t]
  ), 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Fruit", 
    each List.Min({List.Count(List.Intersect({Text.Split([Flavour Description], " "), Fruits})), 1}), 
    Int64.Type
  )
in
  #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

let
  Fruits = {"apple", "orange", "grape", "pear", "watermelon"}, 
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "TYw7CoAwEAWvEra28XcA9RjBIuAjTX6sCcHbu/7AYpoZGK2ppYYmgbEpk5IDrY2mTswsWAbCz/fiFmGvQFaRTbBPGL4QCysHH8Otx/d+FO8PVU0Ge7grric=", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [invoice = _t, brand = _t, #"Flavour Description" = _t]
  ), 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Fruit", 
    each List.Min({List.Count(List.Intersect({Text.Split([Flavour Description], " "), Fruits})), 1}), 
    Int64.Type
  )
in
  #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

thanks for your answer. I tried to replace source - table. My master table has previous ETL steps in advanced query and fruit is an excel loaded to the query. How to make changes above to fit with my real model? thanks for your suggestions. 

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

thanks @lbendlin . I found the way to adjust it. Definitively, you gave me insights to move forwards. thanks again.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.