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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
JohannesBlank
New Member

Odd behaviour when setting datatype of custom column

I'm seeing some odd and inconsistent behaviour when it comes to setting a datatype while creating a custom column vs. creating the column, then changing the datatype.

 

Specifically, in the example below 

 

#"Added Custom" = Table.AddColumn(#"Changed Type1", "Rating_Num", each Text.Remove([#"Rating"],{"A",}), Number.Type),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Rating_Num, type number}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Rating Cluster", each if [#"Rating_Num"] = 1 then "1" else if [#"Rating_Num"] <= 5 then "2 to 5" else "larger than 5")

 

I would expect the second step to be redundant, as the data type is already set in the first step. However, if I delete the second step, even after refreshing, the third step produces an error. And oddly, the second step is visually different

 

JohannesBlank_1-1732797688710.pngJohannesBlank_2-1732797711270.png

 

(after first / after second step, with a different column name than in the example above).

 

Why would that be? Does setting data type when creating a column even work?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @JohannesBlank ,

You can try this M code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMgSTRhC2I4QDoUwhlBmYNAdyYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rating = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Rating", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Rating_Num", each Number.From(Text.Remove([#"Rating"],{"A"}))),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom", "Rating Cluster", each if [#"Rating_Num"] = 1 then "1" else if [#"Rating_Num"] <= 5 then "2 to 5" else "larger than 5")
in
    #"Added Conditional Column"

 

vjunyantmsft_0-1732846990782.png

vjunyantmsft_1-1732847000077.png

But I can't explain why specifying the column type as number in your code doesn't work, because in my test, the data type of the column is indeed displayed as number at the end of the addcolumn step, but Power Query still treats it as text.
This may help you:

vjunyantmsft_0-1732849233478.png


Table.AddColumn - Table Function | Power Query M


It is recommended that if you want to change directly to a numeric type next time, use Number.From or Number.FromText.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @JohannesBlank ,

You can try this M code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMgSTRhC2I4QDoUwhlBmYNAdyYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rating = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Rating", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Rating_Num", each Number.From(Text.Remove([#"Rating"],{"A"}))),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom", "Rating Cluster", each if [#"Rating_Num"] = 1 then "1" else if [#"Rating_Num"] <= 5 then "2 to 5" else "larger than 5")
in
    #"Added Conditional Column"

 

vjunyantmsft_0-1732846990782.png

vjunyantmsft_1-1732847000077.png

But I can't explain why specifying the column type as number in your code doesn't work, because in my test, the data type of the column is indeed displayed as number at the end of the addcolumn step, but Power Query still treats it as text.
This may help you:

vjunyantmsft_0-1732849233478.png


Table.AddColumn - Table Function | Power Query M


It is recommended that if you want to change directly to a numeric type next time, use Number.From or Number.FromText.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

bhanu_gautam
Super User
Super User

@JohannesBlank , Try using below m code

 

m
#"Added Custom" = Table.AddColumn(#"Changed Type1", "Rating_Num", each Text.Remove([#"Rating"],{"A",}), Number.Type),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Rating_Num", type number}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Rating Cluster", each if [#"Rating_Num"] = 1 then "1" else if [#"Rating_Num"] <= 5 then "2 to 5" else "larger than 5")




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi bhanu_gautam,

 

Isn't that the same code as I posted? I cannot see any difference. My goal would be to remove the second step, without producing errors in the third. The data looks like 1, 2, 1A, 2A etc., so the first step should both remove the letters as well as convert to number type.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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