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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
yforti
Helper II
Helper II

Help with conditional formulas

I am having difficulty performing a classification based on a status.

Below the data model:
Capturar.JPG

 

What I need is:

Whenever DS_HISTORICO(header column) contains "Precificado" (marked in yellow), my new column must equal "YES" for ALL my keys, not only for the line that contains "Precificado".


 

 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @yforti 

 

sorry, then I got you wrong (or better... I didn't read your request well :)). Here the adapted code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpJUYrVgTBTU+HMgKLU5My0zOTElHxs0iUlYKYRQtQIVTQtDcwEsoqKYKzSUqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, DS_HISTORICO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"DS_HISTORICO", type text}}),

    final = Table.AddColumn(#"Changed Type", "Res", each if Text.Contains(Text.Lower(_[#"DS_HISTORICO"]), Text.Lower("Precificado")) then true else false, type logical),
    #"Grouped Rows" = Table.Group(final, {"Key"}, {{"AllRows", each _, type table [Key=number, DS_HISTORICO=text, Res=logical]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each if List.AnyTrue([AllRows][Res]) then "Yes" else "No"),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Added Custom", "AllRows", {"DS_HISTORICO"}, {"DS_HISTORICO"})
in
    #"Expanded AllRows"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

8 REPLIES 8
AlB
Community Champion
Community Champion

@yforti 

If you paste the sample data you are trying the code on here in text format rather than on a screen cap, i will be able to see where the problem is. Just copy the table (or fragment thereof) and paste it here. The code provided  earlier should work and does in the sample I've tried it on

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

yforti
Helper II
Helper II

@Jimmy801 hello! First of all thanks for help!

Still dont work, here's the result:

Capturar222.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What i nedd is: For ALL keys (1509991756755674244933) the result should be  "YES" (For all the lines of this key), because we have at least ONE status "Precificado" in DT_Historico (column), and "NO" for key (15099917246836) cause we dont have the status "Precificado" on the lines of this key.

Jimmy801
Community Champion
Community Champion

Hello @yforti 

 

sorry, then I got you wrong (or better... I didn't read your request well :)). Here the adapted code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpJUYrVgTBTU+HMgKLU5My0zOTElHxs0iUlYKYRQtQIVTQtDcwEsoqKYKzSUqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, DS_HISTORICO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"DS_HISTORICO", type text}}),

    final = Table.AddColumn(#"Changed Type", "Res", each if Text.Contains(Text.Lower(_[#"DS_HISTORICO"]), Text.Lower("Precificado")) then true else false, type logical),
    #"Grouped Rows" = Table.Group(final, {"Key"}, {{"AllRows", each _, type table [Key=number, DS_HISTORICO=text, Res=logical]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each if List.AnyTrue([AllRows][Res]) then "Yes" else "No"),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Added Custom", "AllRows", {"DS_HISTORICO"}, {"DS_HISTORICO"})
in
    #"Expanded AllRows"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

AlB
Community Champion
Community Champion

@yforti 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpJUYrVgTBTU+HMgKLU5My0zOTElHxs0iUlYKYRQtQIVTQtDcwEsoqKYKzSUqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, DS_HISTORICO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"DS_HISTORICO", type text}}),

    list_ = Table.SelectRows(Table.Group(#"Changed Type", {"Key"}, {{"Found", each List.Contains([DS_HISTORICO], "Precificado")}}), each [Found] = true)[Key],
    final = Table.AddColumn(#"Changed Type", "Res", each if List.Contains(list_, [Key]) then "Yes" else "No")
in
    final

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

 

@AlB 

Not yet resolved, see below:

Capturar123.JPG

For the key (15099917246836) we dont have the status "Precificado", so the reuslt of Res should be: "no"

yforti
Helper II
Helper II

@AlB this worked for some cases, in the case where I don't have the status, a column created must contain "No", for example:

this key does not have the status "Precificado"

Capturar1.JPG

 

Jimmy801
Community Champion
Community Champion

Hello @yforti 

 

add a new column with this formula

if Text.Contains(Text.Lower(_[#"DS_HISTORICO"]), Text.Lower("Precificado")) then "Yes" else "No"

 

here the complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpJUYrVgTBTU+HMgKLU5My0zOTElHxs0iUlYKYRQtQIVTQtDcwEsoqKYKzSUqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, DS_HISTORICO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"DS_HISTORICO", type text}}),

    final = Table.AddColumn(#"Changed Type", "Res", each if Text.Contains(Text.Lower(_[#"DS_HISTORICO"]), Text.Lower("Precificado")) then "Yes" else "No")
in
    final

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

AlB
Community Champion
Community Champion

Hi @yforti 

Place the following M code in a blank query to see the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUpMVIrViVYyAjJTUsBMYyAzNRXMNAEyA4pSkzPTMpMTU/LBYqYIaTMgs6REKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, DS_HISTORICO = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", Int64.Type}, {"DS_HISTORICO", type text}}),


    aux_ = if List.Contains(#"Changed Type"[DS_HISTORICO], "Precificado") then "Yes" else "No",
    #"Added Custom" = Table.AddColumn(#"Changed Type", "New col", each aux_, type text)
in
    #"Added Custom"

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.