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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

User Defined Function SyntaxError: Token Expected

I'm expanding a User Defined Function. The original function extract and returns only integers from a string, i.e. social security numbers, phone numbers.

 

I need to add logic which verifies if the length of the string being returned, matches what I want it to be. I will be passing the string, i.e. social, and the limit, in this case 9. However, I can't seem to get the function to work properly. I get a Syntax error.

 

I need the StringLimit Variable to be Optional, as I may not need it all the time.

 

Here is the original function:

 

let
    intExtraction = (AlphaNumeric as any) =>

if AlphaNumeric = null then null else

    Number.From(
        Text.Combine(
        List.RemoveNulls(
            List.Transform(
                Text.ToList(

                    // for values that come with dashes, else Excel will interpret as number
                    if Value.Is(AlphaNumeric, type text) then 
                        AlphaNumeric
                    else 
                        Number.ToText(AlphaNumeric)), 

            each if Value.Is(Value.FromText(_), type number) then _ else null
            )
        )
    ))

in
    intExtraction

And here is what I've done so far (amont other things) to try to make it work.

 

let
    intExtraction = (AlphaNumeric as any, StringLimit as number) =>
let
Integers = if AlphaNumeric = null then null else

    Number.From(
        Text.Combine(
        List.RemoveNulls(
            List.Transform(
                Text.ToList(

                    // for values that come with dashes, else Excel will interpret as number
                    if Value.Is(AlphaNumeric, type text) then 
                        AlphaNumeric
                    else 
                        Number.ToText(AlphaNumeric)), 

            each if Value.Is(Value.FromText(_), type number) then _ else null
            )
        )
    )
),

LimitTest = if StringLimit = null then AlphaNumeric else if Text.Length(AlphaNumeric) <> StringLimit then null else AlphaNumeric

in
    LimitTest

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I was able to fix the error using syntax from other functions I've used in the past:

 

let
    intExtraction = (AlphaNumeric as any, optional StringLimit as number) =>
let
    Value = if AlphaNumeric = null then null else 
            Number.From(
                Text.Combine(
                    List.RemoveNulls(
                        List.Transform(
                            Text.ToList(

                                // for values that come with dashes, else Excel will interpret as number
                                if Value.Is(AlphaNumeric, type text) then AlphaNumeric else Number.ToText(AlphaNumeric)),
                            each if Value.Is(Value.FromText(_), type number) then _ else null
            )
        )
    )
),

    CharLimit = if StringLimit = null then Value else if Text.Length(Number.ToText(Value)) <> StringLimit then null else Value

in
    CharLimit
in
    intExtraction

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

I was able to fix the error using syntax from other functions I've used in the past:

 

let
    intExtraction = (AlphaNumeric as any, optional StringLimit as number) =>
let
    Value = if AlphaNumeric = null then null else 
            Number.From(
                Text.Combine(
                    List.RemoveNulls(
                        List.Transform(
                            Text.ToList(

                                // for values that come with dashes, else Excel will interpret as number
                                if Value.Is(AlphaNumeric, type text) then AlphaNumeric else Number.ToText(AlphaNumeric)),
                            each if Value.Is(Value.FromText(_), type number) then _ else null
            )
        )
    )
),

    CharLimit = if StringLimit = null then Value else if Text.Length(Number.ToText(Value)) <> StringLimit then null else Value

in
    CharLimit
in
    intExtraction
v-cherch-msft
Employee
Employee

@Anonymous

 

It seems you need to check the if condition function.

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thanks @v-cherch-msft, I know that this is where the error is as it is the only addition made to the original, working version of the code. 

 

The problem is, I don't know how to correctly fix this issue.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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