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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Janosa
New Member

Double if statement in custom column

Hello all,

 

This custom column is already working except i want to add another if statement.

 

#"Added Custom" = Table.AddColumn(#"Expanded Average weight", "Correct weight", each if( [Weight kg] = 0 and Text.Contains([Product], "something") )then [Average] else [Weight kg])

 

I want to add if([transactioncode] ="86") then [weight] ....i want the weight to be negative but i'm clueless on how to add this to the query. Can you please help me with this?

Thanks and regards

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Janosa ,

 

The basic Power Query (M) structure for an IF statement is as follows:

if SomethingIsTrue then DoThisThing
else if ThisOtherThingIsTrue then DoThisIntead
else EscapeActionIfAllConditionsFalse

// i.e. if..then..else if..then..else
// No brackets, no capitals

 

The conditions get evaluated in order, top to bottom, so you need to be clear about what you want to be checked and when.

Based on the above, your new query step might look something like this:

#"Added Custom" =
Table.AddColumn(
    #"Expanded Average weight",
    "Correct weight",
    each if [transactioncode] = "86" then -[weight]
        else if [Weight kg] = 0 and Text.Contains([Product], "something") then [Average]
    else [Weight kg]
)

 

Also make sure that your [transactioncode] column is actually Text data type, otherwise you need to remove the quotes from "86".

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
BA_Pete
Super User
Super User

Hi @Janosa ,

 

The basic Power Query (M) structure for an IF statement is as follows:

if SomethingIsTrue then DoThisThing
else if ThisOtherThingIsTrue then DoThisIntead
else EscapeActionIfAllConditionsFalse

// i.e. if..then..else if..then..else
// No brackets, no capitals

 

The conditions get evaluated in order, top to bottom, so you need to be clear about what you want to be checked and when.

Based on the above, your new query step might look something like this:

#"Added Custom" =
Table.AddColumn(
    #"Expanded Average weight",
    "Correct weight",
    each if [transactioncode] = "86" then -[weight]
        else if [Weight kg] = 0 and Text.Contains([Product], "something") then [Average]
    else [Weight kg]
)

 

Also make sure that your [transactioncode] column is actually Text data type, otherwise you need to remove the quotes from "86".

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hello Pete,

 

thanks for the info, worked like a charm and I learned something new today.

 

regards

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors