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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
IDBdesign
Frequent Visitor

Unable to add new queries after creating custom function

I am trying to add a custom function to a report I am working on, however it seems that after adding the function I can no longer add new blank queries.  I recieve the error message "Something went wrong: an item with the same key has already been added."

 

Function "FlattenText":

let 
    fn_Type = type function (
        txt as (type text meta [
            Documentation.FieldCaption = "Text",
            Documentation.FieldDescription = "<code>Text</code> to sanitize",
            Documentation.SampleValues = {"Hello    World!─â”"}
        ]),
        optional allowchars as (type text meta [
            Documentation.FieldCaption = "Allow Characters",
            Documentation.FieldDescription = "Characters to be ignored by the sanitizer",
            Documentation.SampleValues = {".?!$#"}
        ])
    ) as text meta [
        Documentation.Name = "FlattenText",
        Documentation.DisplayName = "FlattenText",
        Documentation.Description = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.LongDescription = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.Category = "New Vue Custom Functions | Text",
        Documentation.Author = "Isaac Bickel (isaac.bickel@new-vue.com)"
    ],
    fn_Body = (txt as text, optional allowchars as text) as text =>
        let
            char_list = Text.ToList(allowchars),
            clean_txt = 
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " ", char_list})
                else
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "}),
            list_txt = Text.Split(clean_txt, " "),
            filtered_txt = List.Select(list_txt, each _ <> ""),
            result_txt = Text.Combine(filtered_txt, " ")
        in
            result_txt
in
    Value.ReplaceType(fn_Body, fn_Type)

 

Appreciate any help I can get on this.  Thanks!

1 ACCEPTED SOLUTION

Hi @IDBdesign 

I think this is a bug that you should report.

Problem with your function is an error in the handling of the optional parameter. If you write

            clean_txt =
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "} & char_list)

instead of 

            clean_txt =
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " ", char_list})

 

the function will work with optional parameters as well and you can add more queries.

But this shouldn't cause inability to add more queries. Therefore I'd recommend to report this as a bug.

 

Full function code:

 

let 
    fn_Type = type function (
        txt as (type text meta [
            Documentation.FieldCaption = "Text",
            Documentation.FieldDescription = "<code>Text</code> to sanitize",
            Documentation.SampleValues = {"Hello    World!─â”"}
        ]),
        optional allowchars as (type text meta [
            Documentation.FieldCaption = "Allow Characters",
            Documentation.FieldDescription = "Characters to be ignored by the sanitizer",
            Documentation.SampleValues = {".?!$#"}
        ])
    ) as text meta [
        Documentation.Name = "FlattenText",
        Documentation.DisplayName = "FlattenText",
        Documentation.Description = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.LongDescription = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.Category = "New Vue Custom Functions | Text",
        Documentation.Author = "Isaac Bickel (isaac.bickel@new-vue.com)"
    ],
    fn_Body = (txt as text, optional allowchars as text) as text =>
        let
            char_list = Text.ToList(allowchars),
            clean_txt = 
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "} & char_list)
                else
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "}),
            list_txt = Text.Split(clean_txt, " "),
            filtered_txt = List.Select(list_txt, each _ <> ""),
            result_txt = Text.Combine(filtered_txt, " ")
        in
            result_txt
in
    Value.ReplaceType(fn_Body, fn_Type)

 

 

BTW: Happy to see that my introduction of new record fields into the function metadata is spreading 😉

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

Hmm, something looks odd to me about your code. I generally keep my function definitions separate or I would think there would have to be multiple let statements. However, I am not the Power Query expert, that's @ImkeF 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi @IDBdesign 

I think this is a bug that you should report.

Problem with your function is an error in the handling of the optional parameter. If you write

            clean_txt =
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "} & char_list)

instead of 

            clean_txt =
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " ", char_list})

 

the function will work with optional parameters as well and you can add more queries.

But this shouldn't cause inability to add more queries. Therefore I'd recommend to report this as a bug.

 

Full function code:

 

let 
    fn_Type = type function (
        txt as (type text meta [
            Documentation.FieldCaption = "Text",
            Documentation.FieldDescription = "<code>Text</code> to sanitize",
            Documentation.SampleValues = {"Hello    World!─â”"}
        ]),
        optional allowchars as (type text meta [
            Documentation.FieldCaption = "Allow Characters",
            Documentation.FieldDescription = "Characters to be ignored by the sanitizer",
            Documentation.SampleValues = {".?!$#"}
        ])
    ) as text meta [
        Documentation.Name = "FlattenText",
        Documentation.DisplayName = "FlattenText",
        Documentation.Description = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.LongDescription = "Removes non-printable charactes and extra whitespace from <code>text</code>",
        Documentation.Category = "New Vue Custom Functions | Text",
        Documentation.Author = "Isaac Bickel (isaac.bickel@new-vue.com)"
    ],
    fn_Body = (txt as text, optional allowchars as text) as text =>
        let
            char_list = Text.ToList(allowchars),
            clean_txt = 
                if allowchars <> null then
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "} & char_list)
                else
                    Text.Select(txt, {"a".."z", "A".."Z", "0".."9", " "}),
            list_txt = Text.Split(clean_txt, " "),
            filtered_txt = List.Select(list_txt, each _ <> ""),
            result_txt = Text.Combine(filtered_txt, " ")
        in
            result_txt
in
    Value.ReplaceType(fn_Body, fn_Type)

 

 

BTW: Happy to see that my introduction of new record fields into the function metadata is spreading 😉

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi@ImkeF 

 

Thank you for your help.  I've gone ahead and reported it as a bug as you've suggested.  You've also solved an issue with my custom functions optional parameter which I didn't even bring up in the initial post haha.  Thanks! 😁

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.