Forum Discussion
Split a Text column by delimiter multiple combinations - mQuery
Hi Friends,
I have a table that is loaded into Power BI and has a column as "Cycles" and has text data as below.
And I wanted to add another column "FY Quarter" during the data load steps using mQuery. Can you please advise the best way for this?
| Cycles | FY Quarter |
| Project\FY23\FY23-Q4\Cycle-1 | FY23-Q4 |
| Project\FY23\FY23-Q4\Cycle-2 | FY23-Q4 |
| Project\FY24 | FY24 |
| Project\FY24\FY24-Q1 | FY24-Q1 |
| Project\FY24\FY24-Q1\Cycle-3 | FY24-Q1 |
| Project\FY24\FY24-Q1\Cycle-4 | FY24-Q1 |
| Project\FY24\FY24-Q1\Cycle-5 | FY24-Q1 |
| Project\FY24\FY24-Q2 | FY24-Q2 |
| Project\FY24\FY24-Q2\Cycle-6 | FY24-Q2 |
| Project\FY24\FY24-Q2\Cycle-7 | FY24-Q2 |
Thanks,
Prabhat
Hi prabhatnath ,
Try the following custom column in the query editor:
if Text.Contains([Cycles], "\Cycle-") then Text.BetweenDelimiters([Cycles], "\", "\Cycle-", {1, RelativePosition.FromEnd}, 0) else Text.AfterDelimiter([Cycles], "\", {0, RelativePosition.FromEnd})Here's the complete M Code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLomJcYs0MoaQuoEmMTHOlck5qbqGSjpKUCGlWB1Cqo3wqDaByGGRgJC6gVC7wCzcimB2GZOk2oQk1abEqDaCKzLCowhmpBlJqs2RVccCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cycles = _t, #"FY Quarter" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cycles", type text}, {"FY Quarter", type text}}), Custom = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Cycles], "\Cycle-") then Text.BetweenDelimiters([Cycles], "\", "\Cycle-", {1, RelativePosition.FromEnd}, 0) else Text.AfterDelimiter([Cycles], "\", {0, RelativePosition.FromEnd}), type text) in Customplse try this
try List.Select( Text.Split([Cycles],"\"),(x)=> Text.Contains(x,"-Q", Comparer.OrdinalIgnoreCase)){0} otherwise List.Select( Text.Split([Cycles],"\"),(x)=> Text.Contains(x,"FY", Comparer.OrdinalIgnoreCase)){0}
4 Replies
- danextianSuper User
Hi prabhatnath ,
Try the following custom column in the query editor:
if Text.Contains([Cycles], "\Cycle-") then Text.BetweenDelimiters([Cycles], "\", "\Cycle-", {1, RelativePosition.FromEnd}, 0) else Text.AfterDelimiter([Cycles], "\", {0, RelativePosition.FromEnd})Here's the complete M Code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLomJcYs0MoaQuoEmMTHOlck5qbqGSjpKUCGlWB1Cqo3wqDaByGGRgJC6gVC7wCzcimB2GZOk2oQk1abEqDaCKzLCowhmpBlJqs2RVccCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cycles = _t, #"FY Quarter" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cycles", type text}, {"FY Quarter", type text}}), Custom = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Cycles], "\Cycle-") then Text.BetweenDelimiters([Cycles], "\", "\Cycle-", {1, RelativePosition.FromEnd}, 0) else Text.AfterDelimiter([Cycles], "\", {0, RelativePosition.FromEnd}), type text) in Custom - AhmedxSuper User
plse try this
try List.Select( Text.Split([Cycles],"\"),(x)=> Text.Contains(x,"-Q", Comparer.OrdinalIgnoreCase)){0} otherwise List.Select( Text.Split([Cycles],"\"),(x)=> Text.Contains(x,"FY", Comparer.OrdinalIgnoreCase)){0} - AnonymousNot applicable
Hi prabhatnath ,
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
- prabhatnathAdvocate III