Search code examples
powerbipowerquery

Changing the position of a defined string from the end to the front of the rest of the string


Compartment
#1 Z-DRIVE HYDRAULIC
#2 Z-DRIVE HYDRAULIC
SINGLE POINT DAVIT HPU
CENTRAL HYDRAULIC SYSTEM
#1 MAIN DIESEL ENGINE
#2 MAIN DIESEL ENGINE
CPP #1
CPP #2
#3 SSDG
EDG
#1 Z-DRIVE HYDRAULIC
#2 Z-DRIVE HYDRAULIC
#2 Z-DRIVE HYDRAULIC
#1 Z-DRIVE HYDRAULIC
#1 Z-DRIVE HYDRAULIC
#2 Z-DRIVE HYDRAULIC
#1 SSDG
#2 SSDG
#3 SSDG
#1 MAIN DIESEL ENGINE
#2 MAIN DIESEL ENGINE

If the #X is at the end of the string, I would like to move it to the front of the string, eg:

Before: CPP #1

After: #1 CPP


Solution

  • Just split the string into 2 parts: a part before the "#" and a part after the "#" and then concatenate the parts in reverse order, placing the part after the "#" first followed by the "#" and then the part before the "#".

    = Table.TransformColumns(#"Changed Type", {"Compartment", each 
            let
                parts = Text.Split(_, " "),
                lastPart = List.Last(parts),
                isNumbered = Text.StartsWith(lastPart, "#")
            in
                if isNumbered then
                    Text.Combine({lastPart} & List.RemoveLastN(parts, 1), " ")
                else
                    _, type text})
    

    Result :

    enter image description here