Forum Discussion
group filter to sort similar value
- 6 years ago
Hi Anonymous - this is certianly possible. Here is one way. This simply goes through the data converting the employee "number" (I put that in quotes as your employee numbers are text, which is usually the best way to handle it) to a numberic value and if it works, it classifies it as "Numberic." If it fails, it removes the first character and tries again. If it works, it is 1 char, otherwise, it tries again removing the first two characters. It doesn't classifiy anything starting with A or B. Leaves those null. You can change that in the M code below.
The full M code of my example is here.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVorViVYyMTUD0+YWlmA6GShhAmEZw+SSk2Gqk5NhYknlYFYsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee Number" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee Number", type text}}), #"Added Grouping" = Table.AddColumn( #"Changed Type", "Grouping", each if List.Contains({"a".."b"}, Text.Start([Employee Number], 1)) then null else if (try Number.FromText([Employee Number]))[HasError] = false then "Numeric" else if (try Number.FromText( Text.End([Employee Number], Text.Length([Employee Number]) -1) ) )[HasError] = false then "One Letter" else if (try Number.FromText( Text.End([Employee Number], Text.Length([Employee Number]) -2) ) )[HasError] = false then "Two Letters" else null ) in #"Added Grouping"1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
Anonymousanother solution without using lists, by using Text.Length, Text.Select and the sketchy text comparisons.
#"Added Employee Group" =
Table.AddColumn(
Source,
"Employee Group",
each
let
CorrectTextFormat = Text.Upper(Text.From([Employee Number])),
Result =
Text.Length(Text.Select(CorrectTextFormat, {"A".."Z"}))
+ 1,
CheckIfInGroup = (CorrectTextFormat >= "C" or CorrectTextFormat <= "9") and Result < 4
in
if CheckIfInGroup then
"G" & Text.From(Result)
else
null
)
Best,