Search code examples
powerbidaxpowerbi-desktop

Extract string slice with regex


I have a column which looks like this:

ProductName
SQL Database Single Standard - S0 - DE West Central
SQL Database Single Standard - 10 DTU - DE West Central
SQL Database Single Standard - S0
SQL Database Single Premium - P4 - DE West Central
SQL Database Single General Purpose - Compute Gen5 - 1 vCore - US East

And I would like to extract part of the string to have a result like this:

ProductName
S0
10
S0
P4
1 vCore

Is there any way to do RegEx with DAX and:

  • Eliminate everything after the last -
  • Take the part before the the last two - * -

Solution

  • PowerQuery
    Create a Custom Column with:

    List.Reverse(Text.Split([ProductName], " - ")){1}
    

    Split the string on " - ", reverse it, then take the 2nd one.

    DAX
    Create a Calculated Column with:

    Column = 
      var p = SUBSTITUTE([ProductName], " - ", "|")
      return PATHITEMREVERSE(p, IF(PATHLENGTH(p) < 3, 1, 2), TEXT)