number from first 10 characters in a string

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,141
Office Version
  1. 365
Platform
  1. Windows
Hi

can someone help me write a DAX formula to get the first set of numbers from within a test string
for example

123 _ a long red boat with 2 oars = 123
ABCD123 a long red boat with 2 oars = 123
ABCD 123 a long red boat with 2 oars = 123
abcd (123) a long red boat with 2 oars = 123

I'm importing an updateable excel sheet so I cant use a UDF in my original sheet
but if I import it to a template workbook in powerpivot can I have a piece of code that would work it out for me either in the table frame or within a pivot table?

Martin
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
It can probably be done, but my advice is that there is probably a better way. I would be considering using Power Query to clean your data before loading to Power Pivot. Power Pivot is a reporting tool - it expects clean data. Power Query is a data transformation tool - it loves dirty data. My advice is use Power Query to deliver cleansed data to Power Pivot, and then write Power Pivot formulas to work on clean data.

Just my opinion.
 
Upvote 0
I would be considering using Power Query to clean your data before loading to Power Pivot.

Thanks for the heads up Matt but you have left me in limbo.

Can you point me in the right direction of your suggestion.

Martin
 
Upvote 0
Unfortunately I cannot think of an easy way for a beginner using Power Query's user-interface only, as we're missing clear patterns here on which we could simply split the data. So you have to use some (already a bit advanced) code here (just one of possible other solutions of course):
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    SplitTextIntoSeparateCharacters = Table.AddColumn(Source, "Custom", each Text.ToList(Text.Range([Spalte1],0,9))),
    ShowTheSplit = Table.ExpandListColumn(SplitTextIntoSeparateCharacters, "Custom"),
    CreateTableWithNumbers = Table.FromValue({"0", "1", "2", "3","4","5","6","7","8","9"}),
    FilterUsingInnerJoin = Table.NestedJoin(ShowTheSplit,{"Custom"},CreateTableWithNumbers,{"Value"},"NewColumn",JoinKind.Inner),
    RearrangeNumbers = Table.Group(FilterUsingInnerJoin, {"Spalte1"}, {{"Count", each Text.Combine(_[Custom], "")}})
in
    RearrangeNumbers

LinkToFile
 
Upvote 0
Imke

Whoa that's so new......

Thanks for the file.

For the record I've installed power query and have a copy of M is for Data Monkey for my bed time reading.
Hopefully I can start to work how this is working.
I have a good level of experience in Excel, VBA and SQL so I'm hoping its not too much of an uphill battle.

Thanks for your assistance.

Martin
 
Upvote 0
Just working through this...
On this line
Text.ToList(Text.Range([Spalte1],0,9))

If the string is less than 9 the whole thing fails.
I guess this could be amended to be if(len([Spalte1])>=10,9,len([Spalte1]) or something like it

if the length of the string in the active cell is less than 10 characters then calculate the length of the string and use that.

Sorry guys its a learning curve...

[Spalte1 = Column1]

Martin
 
Last edited:
Upvote 0
Holy cow I thinks I've done it
Code:
if Text.Length([Column1]) >9 
then Text.ToList(Text.Range([Column1],0,9))
else Text.ToList(Text.Range([Column1],0,Text.Length([Column1]) ))
It took ages to realise if then else needs to be in lower case
and len([Column1]) is Text.Length([Column1]) in proper case

Thanks for the help guys

Martin
 
Last edited:
Upvote 0
This formula would extract the 1st set of contiguous numbers for you, maybe into a helper column?
=LOOKUP(99^99,--("0"&MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),ROW($1:$10000)))) Courtesy of:*Ron Coderre
 
Upvote 0

Forum statistics

Threads
1,224,155
Messages
6,176,736
Members
452,741
Latest member
Muhammad Nasir Mahmood

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