Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
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
(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?
Solved! Go to Solution.
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"
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:
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.
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"
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:
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.
@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")
Proud to be a Super User! |
|
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.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
76 | |
74 | |
57 | |
38 | |
33 |
User | Count |
---|---|
70 | |
63 | |
55 | |
48 | |
46 |