Peter Hui
Peter Hui

Reputation: 119

Power Query - Table.TranformColumns

I'm trying to add leading zeros into a column in power query call JobCodes. I know I can do this by adding a new column using Text.Start([JobCodes],5,"0"), but I don't want to add a new column and go back to remove the column I don't need. I want to be able to do this in one step using Table.TransformColumns function. Is this possible?

Code:

Table.TransformColumns(#"Changed Type", each Text.PadStart([JobCodes],5,"0"))

Error:

Expression.Error: We cannot convert a value of type Function to type List. Details: Value=Function Type=Type

Upvotes: 3

Views: 11399

Answers (3)

user15330783
user15330783

Reputation: 1

The correct expresion is:

= Table.TransformColumns(#"Changed Type",{{"JobCodes", each Text.PadStart(Text.From( _), 6, "0"), type text}})

Upvotes: 0

horseyride
horseyride

Reputation: 21298

Or

= Table.ReplaceValue( Source, each  [JobCodes] ,each Text.PadStart(Number.ToText([JobCodes]),5,"0") ,Replacer.ReplaceValue,{"JobCodes"})
in Replace

Upvotes: 0

Alexis Olson
Alexis Olson

Reputation: 40204

Your syntax is just a bit off.

I think this is what you want:

= Table.TransformColumns(#"Changed Type",{{"JobCodes", each Text.PadStart(_, 5,"0")}})

The error is because it was expecting a list of columns that you want to transform (notice the {{...}} above.


The easiest wat to get the syntax right is to use the GUI to do a transformation and then just edit the function a bit. For example, you could use Format > Add Prefix which would give you the following step (assuming you choose to prefix 000).

= Table.TransformColumns(#"Changed Type", {{"JobCodes", each "000" & _, type text}})

Just take out the "000" & _ and put in the transformation you actually want.

Upvotes: 7

Related Questions