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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
olitetu
Regular Visitor

Adding custom column with If function

I'm trying to add a column returning values depending on which range another column's value fits in.

 

I tried the following:

= Table.AddColumn(#"Renamed Columns", "Recence Groupe",
each if [Recence] < "30" then "5"
else if [Recence] < "90" then "4"
else if [Recence] < "180" then "3"
else if [Recence] < "270" then "2"
else if [Recence] < "360" then "1"
else "0")

 

But the values returned are only 5 and 4. All the ones with either 3, 2 , 1 automatically fall to 0.

1 ACCEPTED SOLUTION
edhans
Community Champion
Community Champion

is the [Recence] field a string? if so, it should say 

if Number.From([Recence]) < 30, etc.

 

You cannot compare numbers to text. You will get errors. I suspect it is because before you were getting no errors, so [Recence] must be text as well.

 

Additionally, if [Recence] should be a number all the way round, just convert the field to a number. In the upper left of the column it probalby has ABC for text. Click on that and convert it to either a decimal number or whole number. Then you don't have to use Number.From() around it. Do that very early in your Power Query steps, before adding any custom columns.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

7 REPLIES 7
edhans
Community Champion
Community Champion

You should comparing values. When Power Query does a string comparison, "3" is less than "4", but "300" is also less than "4" because "300" will sort before "4" in ascending order. I believe it is looking at the ASCII values of the characters.

 

You need to either convert everything to numbers or use Number.From() around both [Recence] and your strings you are comparing.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

I changed the data type to "Whole Numbers" but now get errors everywhere (before the "4" and "5" were displaying. 

 

= Table.AddColumn(#"Renamed Columns", "Recence Groupe",
each if [Recence] < 30 then "5"
else if [Recence] < 90 then "4"
else if [Recence] < 180 then "3"
else if [Recence] < 270 then "2" 
else if [Recence] < 360 then "1" 
else "0"))

edhans
Community Champion
Community Champion

is the [Recence] field a string? if so, it should say 

if Number.From([Recence]) < 30, etc.

 

You cannot compare numbers to text. You will get errors. I suspect it is because before you were getting no errors, so [Recence] must be text as well.

 

Additionally, if [Recence] should be a number all the way round, just convert the field to a number. In the upper left of the column it probalby has ABC for text. Click on that and convert it to either a decimal number or whole number. Then you don't have to use Number.From() around it. Do that very early in your Power Query steps, before adding any custom columns.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

= Table.AddColumn(#"Replaced Value26", "_RecenceG",
each if Number.From[Recence] < 30 then 5
else if Number.From[Recence] < 90 then 4
else if Number.From[Recence] < 180 then 3
else if Number.From[Recence] < 270 then 2
else if Number.From[Recence] < 360 then 1
else 0)

 

I tried this syntax and still get errors. 

 

The column Recence was changed to values but the custom column switches it back to text..

 

Sorry I don't understand what is going on. 

edhans
Community Champion
Community Champion

You are missing the brackets

= Table.AddColumn(#"Replaced Value26", "_RecenceG",
each if Number.From([Recence]) < 30 then 5
else if Number.From([Recence]) < 90 then 4
else if Number.From([Recence]) < 180 then 3
else if Number.From([Recence]) < 270 then 2
else if Number.From([Recence]) < 360 then 1
else 0)


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
HotChilli
Community Champion
Community Champion

Putting quotes round the values will make them get treated as text. You'll get some unusual behaviour. You want to treat those values as numbers

StephenK
Resolver I
Resolver I

You cannot apply less than/greater than operators on a string/text datatype. Change the Recence column to a numeric datatype.

Helpful resources

Announcements
December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.