Forum Discussion
Transform a specific value in a column
- 2 years ago
I think you will need to use the Table.TransformRows function here. See this code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkvMKU01VNJRMtQzNlGK1YGKGAFFjPRMzBEixmA1ZqYIEROwGlMjhIgpWI0JkogZWMTUAiFiDtZljmSXBVjEGGhyLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Topic = _t, Values = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Values", type number}}), ConditionalTransform = Table.FromRecords( Table.TransformRows( #"Changed Type", (x) => Record.TransformFields( x, {"Values", each if x[Topic] = "Value4" then x[Values] /100 else x[Values]} ) ) ) in ConditionalTransformI am just dividing any record with Value4 in the Topic column by 100, otherwise I am not touching it.
becomes this:
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.
I think you will need to use the Table.TransformRows function here. See this code:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkvMKU01VNJRMtQzNlGK1YGKGAFFjPRMzBEixmA1ZqYIEROwGlMjhIgpWI0JkogZWMTUAiFiDtZljmSXBVjEGGhyLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Topic = _t, Values = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Values", type number}}),
ConditionalTransform =
Table.FromRecords(
Table.TransformRows(
#"Changed Type",
(x) => Record.TransformFields(
x, {"Values", each if x[Topic] = "Value4" then x[Values] /100 else x[Values]}
)
)
)
in
ConditionalTransform
I am just dividing any record with Value4 in the Topic column by 100, otherwise I am not touching it.
becomes this:
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.