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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

M Query Nested IF

Hi Team,

Good Morning!

 

I need M query function for Status column output based on Col 1 to col 3.

 

Column --> Status is expected output .

 

screen.PNG

 

Thanks,

KVs

 

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

Thanks to ImkeF, with a little change, it seems to work on my side.

Add custom columns

Custom.1=Record.FieldValues(Record.SelectFields(_, {"Col1", "Col2", "Col3"}))
Custom.2= if Text.Contains([Custom.1], "Yes") then "Yes" else if Text.Contains([Custom.1], "No") then "No" else if Text.Contains([Custom.1],"Pending") then "Pending" else null

Capture9.JPG

After creating "Custom.1", then expand it by "Extract values" with comma.

Capture10.JPG

Whole queries, you could paste it in Advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikwtVtJR8ssHEgGpeSmZeelKsTowYayCGGphYvjUYDUPlwiqDqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2", type text}, {"Col3", type text}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom.1", each Record.FieldValues(Record.SelectFields(_, {"Col1", "Col2", "Col3"}))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"Custom.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Added Custom2" = Table.AddColumn(#"Extracted Values", "Custom.2", each if Text.Contains([Custom.1], "Yes") then "Yes" else if Text.Contains([Custom.1], "No") then "No" else if Text.Contains([Custom.1],"Pending") then "Pending" else null)
in
    #"Added Custom2"

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

Thanks to ImkeF, with a little change, it seems to work on my side.

Add custom columns

Custom.1=Record.FieldValues(Record.SelectFields(_, {"Col1", "Col2", "Col3"}))
Custom.2= if Text.Contains([Custom.1], "Yes") then "Yes" else if Text.Contains([Custom.1], "No") then "No" else if Text.Contains([Custom.1],"Pending") then "Pending" else null

Capture9.JPG

After creating "Custom.1", then expand it by "Extract values" with comma.

Capture10.JPG

Whole queries, you could paste it in Advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikwtVtJR8ssHEgGpeSmZeelKsTowYayCGGphYvjUYDUPlwiqDqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2", type text}, {"Col3", type text}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Custom.1", each Record.FieldValues(Record.SelectFields(_, {"Col1", "Col2", "Col3"}))),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"Custom.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Added Custom2" = Table.AddColumn(#"Extracted Values", "Custom.2", each if Text.Contains([Custom.1], "Yes") then "Yes" else if Text.Contains([Custom.1], "No") then "No" else if Text.Contains([Custom.1],"Pending") then "Pending" else null)
in
    #"Added Custom2"

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ImkeF
Community Champion
Community Champion

Hi @Anonymous ,

if you're looking for the first non-empty-item, then adding a column with the following formula should deliver:

 

List.First(Record.FieldValues(Record.SelectFields(_, {"Col1", "Col2", "Col3"})))

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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