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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
Nie
Helper I
Helper I

Errors from Table.Transform using Custom Function

Hi. I need help trying to resolve "Error" values that my code is generating.

 

I have 2 columns of values, "ABC" and "DEF", of type Text. The dataset uses "NaN" to represent null and some values might be "NaN". I use the following code to recode their values.

 

 

= Table.TransformColumns(
   #"Removed Other Columns",
   List.Transform({"ABC", "DEF"}, each {_, MyCustomFunction, Text.Type, MissingField.Ignore})
   )

 

 

 

MyCustomFunction is as follows:

 

 

(currentGrade as text) as text =>
let
    output = 
         if Text.Contains(currentGrade, "1") then "Upper"
    else if Text.Contains(currentGrade, "2") then "Mid"
    else if Text.Contains(currentGrade, "3") then "Low"
    else if Text.Contains(currentGrade, "NaN") then null
    else null
in
    output

 

 

 

I want to recode "1, 2, 3" accordingly to "Upper, Mid, Low". If something is "NaN" or not an expected value, I want this recoded to null. But instead of null. I am seeing "Error".

 

.Screenshot 2021-06-24 103128.png

 

How can I fix this without introducing another step to replace error values?

1 ACCEPTED SOLUTION
Nie
Helper I
Helper I

I found the solution after various trial and errors. I found it works when I replace 

(currentGrade as text) as text =>

with

(currentGrade as text) =>

 

in the code below.

(currentGrade as text) as text =>
let
    output = 
         if Text.Contains(currentGrade, "1") then "Upper"
    else if Text.Contains(currentGrade, "2") then "Mid"
    else if Text.Contains(currentGrade, "3") then "Low"
    else if Text.Contains(currentGrade, "NaN") then null
    else null
in
    output

 

View solution in original post

2 REPLIES 2
Nie
Helper I
Helper I

I found the solution after various trial and errors. I found it works when I replace 

(currentGrade as text) as text =>

with

(currentGrade as text) =>

 

in the code below.

(currentGrade as text) as text =>
let
    output = 
         if Text.Contains(currentGrade, "1") then "Upper"
    else if Text.Contains(currentGrade, "2") then "Mid"
    else if Text.Contains(currentGrade, "3") then "Low"
    else if Text.Contains(currentGrade, "NaN") then null
    else null
in
    output

 

amitchandak
Super User
Super User

@Nie , Is power bi Taking Nan as Nan or null,

 

in replace (even from the menu ) you can use null

 

try a replace first with Nan  to null

and null to some value say X

 

and see if one of them works.

 

 

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.