Forum Discussion
Anonymous
4 years agoNot applicable
Subtract values from same column but based on value from another column in Query Editor
I want to get "Output Column" based on following condition in Power Query (M language):
if column1 value is "Peter" then "Output Column" would be value of (Peter - Fab- Kris) else value from column2
| Column1 | Column2 | Output Column |
| Peter | 1050 | 1050-95-75 =880 |
| Dave | 110 | 110 |
| Ali | 56 | 56 |
| Fab | 95 | 95 |
| Kris | 75 | 75 |
Thanks in advance!
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtSS1S0lEyNDA1UIrViVZySSxLBfENIVzHnEwgz9QMzHFLTAJyLE3BHO+izGIgzxzIiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Output", each if [Column1]="Peter" then [Column2] -Table.SelectRows(#"Changed Type",each [Column1]="Fab"){0}[Column2] -Table.SelectRows(#"Changed Type",each [Column1]="Kris"){0}[Column2] else [Column2]) in #"Added Custom"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".
1 Reply
- lbendlinSuper User
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtSS1S0lEyNDA1UIrViVZySSxLBfENIVzHnEwgz9QMzHFLTAJyLE3BHO+izGIgzxzIiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Output", each if [Column1]="Peter" then [Column2] -Table.SelectRows(#"Changed Type",each [Column1]="Fab"){0}[Column2] -Table.SelectRows(#"Changed Type",each [Column1]="Kris"){0}[Column2] else [Column2]) in #"Added Custom"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".