Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Extract numbers from a string and add a space

Hi Folks, I have a field called "Justification" in a feed that comes to Power BI and a user requested to have a column added that extracts the numbers only. So for example, the data on a row could...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous 

    (1)For your first question , you can realize it in Power Query Editor . Add a customer column with the formula below .

    Table.AddColumn(#"Changed Type", "Custom", each Text.Select([Justification],{"0".."9"," "}))

    You will get the result below :

    (2)For your second question , you can split the custom column you created before. You will get the following result.

    Then go back to Desktop view, to judge whether these two columns can returns 7 digits that begins with a 2 or 6 . If yes , return 1 , or return 0 .We create two measures to judge .

    find 1 =
    var _firstnumber=LEFT(SELECTEDVALUE('Table'[Custom.1]),1)
    var _number=LEN(SELECTEDVALUE('Table'[Custom.1]))
    return IF(_number=7 &&_firstnumber="2" || _firstnumber="6",1,0)
    find 2 =
    var _firstnumber=LEFT(SELECTEDVALUE('Table'[Custom.2]),1)
    var _number=LEN(SELECTEDVALUE('Table'[Custom.2]))
    return IF(_number=7 &&_firstnumber="2" || _firstnumber="6",1,0)

    The result is as shown :

    Then create a measure to return the value that contain 7 digits and begin with a 2 or 6 .

    final result = SWITCH(TRUE(),[find 1]=1,SELECTEDVALUE('Table'[Custom.1]),[find 2]=1,SELECTEDVALUE('Table'[Custom.2]))

    The final result is as shown :

    I have attached my pbix file , you can refer to it .

     

    Best Regard

    Community Support Team _ Ailsa Tao

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.