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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

If staments

Hi

I am trying to create a custom column that calculates the unit savings if plant A were to use plant B pricing. How would I write an if statement that would only return the savings and return a value of 0 if it would cost more money. I want it to look something like the table below.

Plant APlant BSavings
10.500
3.751.901.85
0.701.150
21.250.75
1 ACCEPTED SOLUTION
edhans
Super User
Super User

Add this custom column in Power Query @Anonymous 

if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0

It will return this data for you:

edhans_0-1612311866115.png

Then simply change the data type to a number. Here is the full M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLQMzVQitWJVjLWMzcF8g31LCF8Az1zAzDf0BTMNwJzjICcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Plant A" = _t, #"Plant B" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Plant A", type number}, {"Plant B", type number}}),
    #"Added Savings" = Table.AddColumn(#"Changed Type", "Savings", each if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Savings",{{"Savings", type number}})
in
    #"Changed Type1"

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

5 REPLIES 5
edhans
Super User
Super User

Add this custom column in Power Query @Anonymous 

if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0

It will return this data for you:

edhans_0-1612311866115.png

Then simply change the data type to a number. Here is the full M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLQMzVQitWJVjLWMzcF8g31LCF8Az1zAzDf0BTMNwJzjICcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Plant A" = _t, #"Plant B" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Plant A", type number}, {"Plant B", type number}}),
    #"Added Savings" = Table.AddColumn(#"Changed Type", "Savings", each if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Savings",{{"Savings", type number}})
in
    #"Changed Type1"

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
smpa01
Super User
Super User

@Anonymous 

 

column = if ([plant a]>[plant b], [plant a]-[plant b],0)

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

jcarnovale_0-1612308539467.png

I get an error when this try this is there a differnent method.

@Anonymouslooks like you are trying this pqwry while I gave you the dax soluion.

in pqwry do the following

if [plant a]>[plant b] then [plant a] else [plant a]-[plant b]
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
parry2k
Super User
Super User

@Anonymous try following columns

 

New Col = 
MAX ( Table[Plant A] - Table[Plant B], 0 )

 

Check my latest blog post Compare Budgeted Scenarios vs. Actuals I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors