Forum Discussion
Change column data type depending the value
Good Morning,
I have this table. As you can see in Result column sometimes I have text sometimes but also I have numbers, either Whole Numbers or Decimal Numbers. I'm wondering How can I change the data type of the column depending the value.
In fact, I'm trying to change in the Main Menu
But I'm getting this 2 messages
I tried many different things but it doesn't work.
This Result column I'm using sometimes for trending chart so I need to take the numbers as Data type Number or Decimal.
I hope someone can help me.
Thanks a lot in advance
Hi RWRW
I noticed the changes tharunkumarRTK and ryan_mayu suggested but noticed that for both lines you are using "Changed Type" as the first argument.
The first argument for the second column added should refer to your previous step. If you refer to an earlier step, the steps in between will be missed.
Change the first argument of your second column (your text column) to refer to the step name for the first column (your numeric column).
In my example when creating the numeric column, my step name ended up as "Custom1" and the code is as follows:
= Table.AddColumn( #"Changed Type", "Numeric Result", each try Number.From([Result]) otherwise null )For my text column, i used the following:
= Table.AddColumn( Custom1, "Text Result", each if [Numeric Result] = null then [Result] else null )Notiice the change in the first arguments?
I hope this helps.
19 Replies
- tharunkumarRTKSuper User
RWRW
I would suggest you to split your result column into two different columns based on their type and proceed accordingly. You can split it by following this syntax:= Table.AddColumn(#"Added Custom", "Number", each if Value.Type([Result]) = Number.Type then [Result] else null, type number)If the post helps please give a thumbs up
If it solves your issue, please accept it as the solution to help the other members find it more quickly.
Tharun
- ryan_mayuSuper User
we can only set up one data type for one column. if the column contains both text and number, the data type can only be TEXT.
you can try to create a new column in PQ to retrieve numbers.
=try Number.FromText([Result]) otherwise null
- RWRWHelper III
Sorry this screenshot