Forum Discussion
mtrevisiol
3 years agoHelper V
IF-THEN-ELSE in Power Query with substrings
Hi everyone, I am trying to convert a calculated column into a column realised via power query in M language. The column in question extracts two letters from the 'Item' column, which contains a l...
- 3 years ago
Hi mtrevisiol ,
Try this in a new custom column:
if Text.Range([Item], 3, 1) = "K" then Text.Range([Item], 4, 2) else Text.Range([Item], 3, 2)It gives this output:
You can adjust the first numerical argument in Text.Range to change the offset (the start of when the two letters are taken from) if I've misunderstood exactly which characters you wanted.
Full example query for reference:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjQw8A4O9TMxMDJx9VaK1QGLBDlGGhkZmYC5FgYGYcHOBgZGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t]), chgTypes = Table.TransformColumnTypes(Source,{{"Item", type text}}), addSiglaCod = Table.AddColumn(chgTypes, "siglaCod", each if Text.Range([Item], 3, 1) = "K" then Text.Range([Item], 4, 2) else Text.Range([Item], 3, 2)) in addSiglaCodPete
BA_Pete
3 years agoSuper User
Hi mtrevisiol ,
Try this in a new custom column:
if Text.Range([Item], 3, 1) = "K"
then Text.Range([Item], 4, 2)
else Text.Range([Item], 3, 2)
It gives this output:
You can adjust the first numerical argument in Text.Range to change the offset (the start of when the two letters are taken from) if I've misunderstood exactly which characters you wanted.
Full example query for reference:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjQw8A4O9TMxMDJx9VaK1QGLBDlGGhkZmYC5FgYGYcHOBgZGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t]),
chgTypes = Table.TransformColumnTypes(Source,{{"Item", type text}}),
addSiglaCod = Table.AddColumn(chgTypes, "siglaCod", each if Text.Range([Item], 3, 1) = "K"
then Text.Range([Item], 4, 2)
else Text.Range([Item], 3, 2))
in
addSiglaCod
Pete