Forum Discussion
change column value based on condition
Dear All,
I have a table that looks like this:
| Code | Value1 | Value2 |
| 0001 | 100 | 200 |
| 0002 | 50 | 500 |
0003 | 10 | 20 |
| 0004 | 150 | 300 |
I'd like a condition in Power Query for which:
- if row meets Code = 0003, then Value1 and Value2 must be divided for 12.
I do not want to generate other columns, just want to substitute values in Value1 and Value2.
Desired result:
| Code | Value1 | Value2 |
| 0001 | 100 | 200 |
| 0002 | 50 | 500 |
0003 | 0,833333 | 1,66666 |
| 0004 | 150 | 300 |
Thank you a lot
powerbricco add a replace value step and change it as shown below, do it for both value 1 and value 2
= Table.ReplaceValue(#"Changed Type",each [Value1],each if [Code] = 3 then [Value1] / 12 else [Value1], Replacer.ReplaceValue,{"Value1"})
5 Replies
- parry2kSuper User
powerbricco add a replace value step and change it as shown below, do it for both value 1 and value 2
= Table.ReplaceValue(#"Changed Type",each [Value1],each if [Code] = 3 then [Value1] / 12 else [Value1], Replacer.ReplaceValue,{"Value1"})- powerbriccoAdvocate I
Dear parry2k , it does work perfectly!
May I ask you another advice?
Let's say I have a date column. Every time rows meet codes 001, 002, 003 and 0004, each Value1 should be replaced with month in date column. How can I write that?Thank you a lot..
- parry2kSuper User
- parry2kSuper User
powerbricco try this, change column name as per your table:
Table.ReplaceValue(#"Changed Type",each [Value],each if [Code] = 1 or [Code] = 2 or [Code] = 3 then Date.Month([Date]) else [Value],Replacer.ReplaceValue,{"Value"}) - AnonymousNot applicable
Hi powerbricco ,
Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.
If these also don't help, please share more detailed information and description to help us clarify your scenario to test.
How to Get Your Question Answered Quickly
Regards,
Xiaoxin Sheng