Forum Discussion
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 A | Plant B | Savings |
| 1 | 0.50 | 0 |
| 3.75 | 1.90 | 1.85 |
| 0.70 | 1.15 | 0 |
| 2 | 1.25 | 0.75 |
Add this custom column in Power Query Anonymous
if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0It will return this data for you:
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.
5 Replies
- parry2kSuper 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.⚡
- smpa01Community Champion
Anonymous
column = if ([plant a]>[plant b], [plant a]-[plant b],0)- AnonymousNot applicable
I get an error when this try this is there a differnent method.
- smpa01Community Champion
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]
- edhansCommunity Champion
Add this custom column in Power Query Anonymous
if [Plant A] > [Plant B] then [Plant A] - [Plant B] else 0It will return this data for you:
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.