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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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