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
Arron_IA
Frequent Visitor

Create a Function to Enable Filtering

Hi all,

                I want to create a function which I can then use ‘Invoke Custom Function’ and apply to any column in any table. The function I wish is filtering which is outlined in the script below –

 

Here is the script applied to a particular table and column

Q8 - Looking for unusual entries in the data fields, e.g. Bank/BSoc Account Name !,",£ etc

 

let

    Source = Q4,

    // field, e.g. £, $, %

    #"Filter unusual entries Bank/BSoc Account " = Table.SelectRows(Source, each Text.Contains([#"Bank/BSoc Account Name"], "!") or Text.Contains([#"Bank/BSoc Account Name"], "?") or Text.Contains([#"Bank/BSoc Account Name"], """") or Text.Contains([#"Bank/BSoc Account Name"], "£") or Text.Contains([#"Bank/BSoc Account Name"], "$") or Text.Contains([#"Bank/BSoc Account Name"], "%") or Text.Contains([#"Bank/BSoc Account Name"], "^") or Text.Contains([#"Bank/BSoc Account Name"], "*") or Text.Contains([#"Bank/BSoc Account Name"], "(") or Text.Contains([#"Bank/BSoc Account Name"], ")") or Text.Contains([#"Bank/BSoc Account Name"], "_") or Text.Contains([#"Bank/BSoc Account Name"], "=") or Text.Contains([#"Bank/BSoc Account Name"], "+") or Text.Contains([#"Bank/BSoc Account Name"], "`") or Text.Contains([#"Bank/BSoc Account Name"], "¬") or Text.Contains([#"Bank/BSoc Account Name"], "[") or Text.Contains([#"Bank/BSoc Account Name"], "]") or Text.Contains([#"Bank/BSoc Account Name"], "{") or Text.Contains([#"Bank/BSoc Account Name"], "}") or Text.Contains([#"Bank/BSoc Account Name"], "\") or Text.Contains([#"Bank/BSoc Account Name"], "|") or Text.Contains([#"Bank/BSoc Account Name"], "<") or Text.Contains([#"Bank/BSoc Account Name"], ">") or Text.Contains([#"Bank/BSoc Account Name"], "/") or Text.Contains([#"Bank/BSoc Account Name"], "?") or Text.Contains([#"Bank/BSoc Account Name"], ":") or Text.Contains([#"Bank/BSoc Account Name"], ":") or Text.Contains([#"Bank/BSoc Account Name"], "@") or Text.Contains([#"Bank/BSoc Account Name"], "#") or Text.Contains([#"Bank/BSoc Account Name"], "~"))

in

    #"Filter unusual entries Bank/BSoc Account "

 

Thanks

2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

Hi @Arron_IA ,

 

I think the following should be an easier implementation.
Given a list of characters that you want to find (so it's easier to manage/update etc.), you can use the following code:

// SpecialCharsList
let
    Source = {"!", "?", """", "£", "$", "%", "^", "*", "(", ")", "_", "=", "+", "`", "¬", "[", "]", "{", "}", "\\", "|", "<", ">", "/", "?", ":", ":", "@", "#", "~"}
in
    Source
// FindChar
Table.SelectRows(
    PreviousStepName,
    each List.AnyTrue(
        List.Transform(
            SpecialCharsList,
            (x) => Text.Contains([TextColumn], x)
        )
    )
)

 

Filters this:

BA_Pete_0-1732638113897.png

 

...to this:

BA_Pete_1-1732638137103.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

PwerQueryKees
Post Prodigy
Post Prodigy

I am not at my laptop, so I could not test, BUT

 

Your function to invoke would be:

(S as text) => List.ContainsAny(Text.ToList(S), Text.Tolist("!?£$%^*()_=+`¬[]{}\|<>/?::@#~"""))

Create a new blank query, open advanced editor and paste the M code above.

Give the query a name. For example HasFunnyCharacters.

Invke it like

#"Filter unusual entries Bank/BSoc Account " = Table.SelectRows(Source, each not HasFunnyCharacters([#"Bank/BSoc Account Name"])

 

View solution in original post

4 REPLIES 4
PwerQueryKees
Post Prodigy
Post Prodigy

I am not at my laptop, so I could not test, BUT

 

Your function to invoke would be:

(S as text) => List.ContainsAny(Text.ToList(S), Text.Tolist("!?£$%^*()_=+`¬[]{}\|<>/?::@#~"""))

Create a new blank query, open advanced editor and paste the M code above.

Give the query a name. For example HasFunnyCharacters.

Invke it like

#"Filter unusual entries Bank/BSoc Account " = Table.SelectRows(Source, each not HasFunnyCharacters([#"Bank/BSoc Account Name"])

 

Thanks PwerQueryKees, this solution also works if I wanted to exclude the special characters I just amended the invoking script by removing the "not"  to -

 

 #"Filter unusual entries Bank/BSoc Account" = Table.SelectRows(#"Changed Type", each #"HasFunnyCharacters"([#"Bank/BSoc Account Name"])

 

kind regards 

BA_Pete
Super User
Super User

Hi @Arron_IA ,

 

I think the following should be an easier implementation.
Given a list of characters that you want to find (so it's easier to manage/update etc.), you can use the following code:

// SpecialCharsList
let
    Source = {"!", "?", """", "£", "$", "%", "^", "*", "(", ")", "_", "=", "+", "`", "¬", "[", "]", "{", "}", "\\", "|", "<", ">", "/", "?", ":", ":", "@", "#", "~"}
in
    Source
// FindChar
Table.SelectRows(
    PreviousStepName,
    each List.AnyTrue(
        List.Transform(
            SpecialCharsList,
            (x) => Text.Contains([TextColumn], x)
        )
    )
)

 

Filters this:

BA_Pete_0-1732638113897.png

 

...to this:

BA_Pete_1-1732638137103.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thanks Pete that has done the trick 😀

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.