Excel Power Query m Code to Extract Call Number

sanket_sk

Board Regular
Joined
Dec 27, 2016
Messages
140
Office Version
  1. 365
Platform
  1. Windows
Hi There,

I am looking for Excel Power Query M Code which will Segregate Call numbers in two different columns as per their type.
Also there might be some free text available in few case numbers, need to remove those unwanted characters and extract correct call number

Sample Input Table
Number
183461-1000754
183461-1001042
183461-1001069
183461-1001152
183490-1000647 Complaint
60-0032745101
60-0032819228
60-0032827919
60-0032829170 Demo


Expected Output
CaseNo
183490-1000647
183461-1000754
183461-1001042
183461-1001069
183461-1001152
60-0032829170
60-0032745101
60-0032819228
60-0032827919
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here's the result:

Book1
ABCDE
1CallsCallReport
2NumberNumberCaseNo
3183461-1000754183461-1000754183461-1000754
4183461-1001042183461-1001042183461-1001042
5183461-1001069183461-1001069183461-1001069
6183461-1001152183461-1001152183461-1001152
7183490-1000647 Complaint183490-1000647 Complaint183490-1000647
860-003274510160-003274510160-0032745101
960-003281922860-003281922860-0032819228
1060-003282791960-003282791960-0032827919
1160-0032829170 Demo60-0032829170 Demo60-0032829170
Sheet1


And here's the M Code:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Calls"]}[Content],
    AddedCaseColumn = Table.AddColumn(Source, "Case", each if Text.StartsWith([Number], "1") then [Number] else null),
    SplitColumnBySpace1 = Table.SplitColumn(AddedCaseColumn, "Case", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Case", "Case.2"}),
    RemovedCase2 = Table.RemoveColumns(SplitColumnBySpace1,{"Case.2"}),
    AddedNoColumn = Table.AddColumn(RemovedCase2, "No", each if Text.StartsWith([Number], "60-") then [Number] else null),
    SplitColumnBySpace2 = Table.SplitColumn(AddedNoColumn, "No", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"No", "No.2"}),
    RemovedNo2 = Table.RemoveColumns(SplitColumnBySpace2,{"No.2"}),
    ReplacedNulls = Table.ReplaceValue(RemovedNo2,null,"",Replacer.ReplaceValue,{"Case", "No"}),
    ChangedType = Table.TransformColumnTypes(ReplacedNulls,{{"Number", type text}, {"Case", type text}, {"No", type text}}),
    CleanedText = Table.TransformColumns(ChangedType,{{"Case", Text.Clean, type text}, {"No", Text.Clean, type text}}),
    TrimmedText = Table.TransformColumns(CleanedText,{{"Case", Text.Trim, type text}, {"No", Text.Trim, type text}})
in
    TrimmedText

Note that I manually remove spaces from step names, and the last 2 steps (CleanedText and TrimmedText) are probably not needed, but don't hurt.

This was all done right through the Interface and very simple and basic. I suggest watching either Mr. Excel's Power Query playlist, or Mike's playlist Full Advanced Data Analysis & BI Class (MSPTDA). Power Query, Power Pivot, DAX, M Code, Power BI & Excel on his ExcelIsFun channel. I prefer Mike's because he provides before and after files. Learning just the basics (which would have taken little time for this) can save you hours of time.
 
Upvote 0
Solution
Here's the result:

Book1
ABCDE
1CallsCallReport
2NumberNumberCaseNo
3183461-1000754183461-1000754183461-1000754
4183461-1001042183461-1001042183461-1001042
5183461-1001069183461-1001069183461-1001069
6183461-1001152183461-1001152183461-1001152
7183490-1000647 Complaint183490-1000647 Complaint183490-1000647
860-003274510160-003274510160-0032745101
960-003281922860-003281922860-0032819228
1060-003282791960-003282791960-0032827919
1160-0032829170 Demo60-0032829170 Demo60-0032829170
Sheet1


And here's the M Code:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Calls"]}[Content],
    AddedCaseColumn = Table.AddColumn(Source, "Case", each if Text.StartsWith([Number], "1") then [Number] else null),
    SplitColumnBySpace1 = Table.SplitColumn(AddedCaseColumn, "Case", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Case", "Case.2"}),
    RemovedCase2 = Table.RemoveColumns(SplitColumnBySpace1,{"Case.2"}),
    AddedNoColumn = Table.AddColumn(RemovedCase2, "No", each if Text.StartsWith([Number], "60-") then [Number] else null),
    SplitColumnBySpace2 = Table.SplitColumn(AddedNoColumn, "No", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"No", "No.2"}),
    RemovedNo2 = Table.RemoveColumns(SplitColumnBySpace2,{"No.2"}),
    ReplacedNulls = Table.ReplaceValue(RemovedNo2,null,"",Replacer.ReplaceValue,{"Case", "No"}),
    ChangedType = Table.TransformColumnTypes(ReplacedNulls,{{"Number", type text}, {"Case", type text}, {"No", type text}}),
    CleanedText = Table.TransformColumns(ChangedType,{{"Case", Text.Clean, type text}, {"No", Text.Clean, type text}}),
    TrimmedText = Table.TransformColumns(CleanedText,{{"Case", Text.Trim, type text}, {"No", Text.Trim, type text}})
in
    TrimmedText

Note that I manually remove spaces from step names, and the last 2 steps (CleanedText and TrimmedText) are probably not needed, but don't hurt.

This was all done right through the Interface and very simple and basic. I suggest watching either Mr. Excel's Power Query playlist, or Mike's playlist Full Advanced Data Analysis & BI Class (MSPTDA). Power Query, Power Pivot, DAX, M Code, Power BI & Excel on his ExcelIsFun channel. I prefer Mike's because he provides before and after files. Learning just the basics (which would have taken little time for this) can save you hours of time.
Thanks A Lot!!!
This is working exactly as expected

Thanks & regards,
Sanket
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Power Query m Code to Extract Call Number
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Just for fun, here is another way.

PQX
ABCDE
1NumberNumberCaseNo
2183461-1000754183461-1000754183461-1000754
3183461-1001042183461-1001042183461-1001042
4183461-1001069183461-1001069183461-1001069
5183461-1001152183461-1001152183461-1001152
6183490-1000647 Complaint183490-1000647 Complaint183490-1000647
760-003274510160-003274510160-0032745101
860-003281922860-003281922860-0032819228
960-003282791960-003282791960-0032827919
1060-0032829170 Demo60-0032829170 Demo60-0032829170
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    UNQ = List.Zip({List.Distinct(List.Transform(Source[Number],each Text.Middle(_,0,1))),{"Case","No"}}),
    AddColumns = List.Accumulate(UNQ,Source,(s,c)=> Table.AddColumn(s,c{1},each if Text.Middle([Number],0,1)=c{0} then Text.Split([Number]," "){0} else null))
in
    AddColumns
 
Upvote 0

Forum statistics

Threads
1,223,990
Messages
6,175,817
Members
452,672
Latest member
missbanana

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