Search code examples
powerbidata-transform

Create new Column in Power Bi with RegEx


I'm relatively new to Power BI and want to generate a new one based on a column. The contents of the new column should be based on the first value of another column. For example:

ColumnA NewColumn
1123 Argentinia
5644 Brazil
5555 Brazil
3334 Denmark
1124 Argentinia

As you can see, the first value of the number decides which country will be added to the new column. In SQL I know that I can use something like this:

`select * from table where column LIKE '%[2]`%' 

and so on but is this possible with Power BI? Thanks a lot.

Edit:

My additional list looks like this:

ID Country
1  Argentina
2  Swiss
3  Denmark
4  Norway
5  Brazil

and so on...

I thougt I could use somethin like this:

NewColumn = IF('table'[ColumnA] = "%[1]`%" 
THEN "Argentinia"
ELSE if IF('table'[ColumnA] = "%[2]`%
THEN Swiss
ELSE "No Country")

Solution

  • Okay, I've now also found a solution:

    NewColumn = SWITCH(TRUE();
    
                     LEFT(table[ColumnA]; 1) in {"1"}; "Argentina";
                     LEFT(table[ColumnA]; 1) in {"2"}; "Swiss";
                     LEFT(table[ColumnA]; 1) in {"3"}; "Denmark";
                     LEFT(table[ColumnA]; 1) in {"4"}; "Norway";
                     LEFT(table[ColumnA]; 1) in {"5"}; "Brazil"
                )
    

    Works very well :)