Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
robinshalabh
New Member

Text Contains Query in power query

Dear all,

I am new to M code. I am trying to create a column A based on data in Column B. Column A objective is to get column B data if Column B contains text. For e.g. if column B contains BNP, then, Column A should have full text of cells containing BNP in column B.

 

can anybody help me.

 

 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @robinshalabh 

 

the problem I see with the solution from @Payeras_BI  is if you have a column that is formated as text not as any. This would mean, everey value is text and so you will get not the desired result. When you use this formula, it should always work

let 
Check = try Number.From([ColumnB])
in 
if Check[HasError] = true then [ColumnB] else null

Here the complete code example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvILUIrViVYyNDIG045gEi4KJk3BpJlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ColumnB = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ColumnB", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ColumnA", each let 
Check = try Number.From([ColumnB])
in 
if Check[HasError] = true then [ColumnB] else null)
in
    #"Added Custom"

transform this

Jimmy801_0-1615543370842.png

 

into this

Jimmy801_1-1615543384708.png

 

View solution in original post

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @robinshalabh 

 

the problem I see with the solution from @Payeras_BI  is if you have a column that is formated as text not as any. This would mean, everey value is text and so you will get not the desired result. When you use this formula, it should always work

let 
Check = try Number.From([ColumnB])
in 
if Check[HasError] = true then [ColumnB] else null

Here the complete code example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvILUIrViVYyNDIG045gEi4KJk3BpJlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ColumnB = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ColumnB", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "ColumnA", each let 
Check = try Number.From([ColumnB])
in 
if Check[HasError] = true then [ColumnB] else null)
in
    #"Added Custom"

transform this

Jimmy801_0-1615543370842.png

 

into this

Jimmy801_1-1615543384708.png

 

Payeras_BI
Super User
Super User

Hi @robinshalabh ,

Add a Custom Column and type in the following code:

 

if Value.Is([Column B],type number) then null else [Column B])

 

Payeras_BI_0-1615541626005.png

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.