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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Syndicate_Admin
Administrator
Administrator

I need help

Hello

I need to make this new column ProductType for what it asks me and I do not know what function / functions to use, thanks in advance:

Product Type. It will store the type of product being marketed. Be Meat for those products whose Variety Code start with C, Fish for those products whose Variety Code start with P y Fruits and Vegetables for those products whose Code of Variage start with F.

2323232323.JPG

1 ACCEPTED SOLUTION
sevenhills
Super User
Super User

You can do either in DAX or power query. I prefer Power Query.

 

DAX, you can do with "Switch" and "Contains", "ContainsString"

DAX - Add new Column:

 

Product Type Column = 

var _pt_1stChar = LEFT('Table'[Variety Code], 1)

RETURN SWITCH(
  TRUE(),
  _pt_1stChar = "C", "Meat",
  _pt_1stChar = "P", "Fish",
  _pt_1stChar = "F", "Fruits & Vegetables"
)

 

 

DAX - Add new measure:

Product Type Measure = 
var _pt_1stChar = LEFT(SELECTEDVALUE('Table'[Variety Code]), 1)

RETURN SWITCH(
  TRUE(),
  _pt_1stChar = "C", "Meat",
  _pt_1stChar = "P", "Fish",
  _pt_1stChar = "F", "Fruits & Vegetables"
)

 

Power query, you can do with "Text.StartsWith" or "Text.Contains" per your needs and Conditional column:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck4EA6VYnWilZAQzIB9JvCAfwXYrggEwN62oCEkgFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Variety Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Variety Code", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Product Type", 
    
    each if Text.StartsWith([Variety Code], "C", Comparer.OrdinalIgnoreCase) then "Meat" 
    else if Text.StartsWith([Variety Code], "P", Comparer.OrdinalIgnoreCase) then "Fish" 
    else if Text.StartsWith([Variety Code], "F", Comparer.OrdinalIgnoreCase) then "Fruits & Vegetables" else null)
 
in
    #"Added Conditional Column"

 

 

sevenhills_0-1651522840235.png

 

 

I dont know spanish, sorry, I used some text for Variety code to get the code for you.

View solution in original post

2 REPLIES 2
sevenhills
Super User
Super User

You can do either in DAX or power query. I prefer Power Query.

 

DAX, you can do with "Switch" and "Contains", "ContainsString"

DAX - Add new Column:

 

Product Type Column = 

var _pt_1stChar = LEFT('Table'[Variety Code], 1)

RETURN SWITCH(
  TRUE(),
  _pt_1stChar = "C", "Meat",
  _pt_1stChar = "P", "Fish",
  _pt_1stChar = "F", "Fruits & Vegetables"
)

 

 

DAX - Add new measure:

Product Type Measure = 
var _pt_1stChar = LEFT(SELECTEDVALUE('Table'[Variety Code]), 1)

RETURN SWITCH(
  TRUE(),
  _pt_1stChar = "C", "Meat",
  _pt_1stChar = "P", "Fish",
  _pt_1stChar = "F", "Fruits & Vegetables"
)

 

Power query, you can do with "Text.StartsWith" or "Text.Contains" per your needs and Conditional column:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck4EA6VYnWilZAQzIB9JvCAfwXYrggEwN62oCEkgFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Variety Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Variety Code", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Product Type", 
    
    each if Text.StartsWith([Variety Code], "C", Comparer.OrdinalIgnoreCase) then "Meat" 
    else if Text.StartsWith([Variety Code], "P", Comparer.OrdinalIgnoreCase) then "Fish" 
    else if Text.StartsWith([Variety Code], "F", Comparer.OrdinalIgnoreCase) then "Fruits & Vegetables" else null)
 
in
    #"Added Conditional Column"

 

 

sevenhills_0-1651522840235.png

 

 

I dont know spanish, sorry, I used some text for Variety code to get the code for you.

AUaero
Responsive Resident
Responsive Resident

A switch statement ought to do it.

ProductType = 
SWITCH(
   TRUE(),
   VarietyCode = "C", "Meat",
   VarietyCode = "P", "Fish",
   VarietyCode = "F", "Fruits & Vegetables"
)

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors