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.
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 |
Solved! Go to Solution.
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:
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.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingAdd 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:
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.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI Reporting@Anonymous
column = if ([plant a]>[plant b], [plant a]-[plant b],0)
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]
@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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
112 | |
97 | |
84 | |
67 | |
59 |
User | Count |
---|---|
150 | |
120 | |
99 | |
87 | |
68 |