If Then using Length

bluelabel

Board Regular
Joined
Nov 27, 2008
Messages
76
Hi Power Users!

I am very new to Power Query, but an advanced Excel user. I am starting to get into Power Query and using data tables across a number of workbooks.

I have a column in my data table that should be a number however, sometimes it will be a text due to a letter being added to the end of the number. It will always be 5 numbers and then followed by a letter if that is the case.

A
B
C
1
Car
59847
10
2
Truck
59848B
20
3
Bus
59489C
30

<tbody>
</tbody>


When I bring this data into Power Query the items in Column B with the suffix letter error out.

My excel brain tells me =if(Len(B1)=6, Left(B1, 5), B1)

How do i write this into a query so it comes through as a numeric value please?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
maybe try

Column1Column2Column3Column4Column1Column2TrimColumn4
1​
Car
59847​
10​
1​
Car
59847​
10​
2​
Truck59848B
20​
2​
Truck
59848​
20​
3​
Bus59489C
30​
3​
Bus
59489​
30​

Code:
[SIZE=1]let
    C2R = List.Transform({65..90,97..122}, each Character.FromNumber(_)),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TypeText = Table.TransformColumnTypes(Source,{{"Column3", type text}}),
    TrimLetters = Table.AddColumn(TypeText, "Trim", each Text.Trim([Column3],C2R)),
    TypeNumber = Table.TransformColumnTypes(TrimLetters,{{"Trim", Int64.Type}}),
    RC = Table.RemoveColumns(TypeNumber,{"Column3"}),
    Reorder = Table.ReorderColumns(RC,{"Column1", "Column2", "Trim", "Column4"})
in
    Reorder[/SIZE]

or just

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Extract5 = Table.TransformColumns(Source, {{"Column3", each Text.Start(Text.From(_, "en-GB"), 5), type text}}),
    TypeNumber = Table.TransformColumnTypes(Extract5,{{"Column3", Int64.Type}})
in
    TypeNumber[/SIZE]
 
Last edited:
Upvote 0
A third way

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TypeText = Table.TransformColumnTypes(Source,{{"Column3", type text}}),
    KeepNumbers = Table.TransformColumns(TypeText, {{"Column3", each Text.Select(_, {"0".."9"}), type text}}),
    TypeNumber = Table.TransformColumnTypes(KeepNumbers,{{"Column3", Int64.Type}})
in
    TypeNumber
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top