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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
ovetteabejuela
Impactful Individual
Impactful Individual

PowerQuery(easy): Assign Data-type in the same line as the Conditional command.

Simply.... Ho do I transform this:

 

  #"Added Custom" = Table.AddColumn(#"Filtered Rows1", "AField", each if [Field1] <> null then [Field1]*twenty_four else null),
 #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom",{{"AField", type number}})

 

Into a single-line command?

 

Or not possible?

 

Trying to do this to multiple fields that is why I'm looking to have the command shortened so I don't end up with more lines.

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

Basically:

 

 #"Added Custom" = Table.AddColumn(#"Filtered Rows1", "AField", each if [Field1] <> null then [Field1]*twenty_four else null, type number),

 

There is a subtle difference though: with Table.TransformColumnTypes, the data is acually converted to the new type, and when you supply the type with Table,AddColumn, then you must ensure that the data matches the data type supplied.

 

As an example, the following code will run fine in the Query Editor, but you will get errors when loading the data as not all numbers are whole numbers.

 

let
    Source = {250..260},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each [Column1]/2, Int64.Type)
in
    #"Added Custom"

 

With the 2 step approach in this case, you will get errors with Table.TransformColumnTypes in the Query Editor.

 

But usually it won't harm supplying the type with Table.AddColumn.

Specializing in Power Query Formula Language (M)

View solution in original post

2 REPLIES 2
MarcelBeug
Community Champion
Community Champion

Basically:

 

 #"Added Custom" = Table.AddColumn(#"Filtered Rows1", "AField", each if [Field1] <> null then [Field1]*twenty_four else null, type number),

 

There is a subtle difference though: with Table.TransformColumnTypes, the data is acually converted to the new type, and when you supply the type with Table,AddColumn, then you must ensure that the data matches the data type supplied.

 

As an example, the following code will run fine in the Query Editor, but you will get errors when loading the data as not all numbers are whole numbers.

 

let
    Source = {250..260},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each [Column1]/2, Int64.Type)
in
    #"Added Custom"

 

With the 2 step approach in this case, you will get errors with Table.TransformColumnTypes in the Query Editor.

 

But usually it won't harm supplying the type with Table.AddColumn.

Specializing in Power Query Formula Language (M)

I knew it's an easy one, I've seen this example once but I can't google my way to that example.

 

And thanks for showing me the difference between the two approach this can help me when debugging.

 

As always @MarcelBeug, thank you!

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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