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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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