Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created November 30, 2022 10:07
Show Gist options
  • Save bjulius/f9b139310d606daad51da8728d66ecd9 to your computer and use it in GitHub Desktop.
Save bjulius/f9b139310d606daad51da8728d66ecd9 to your computer and use it in GitHub Desktop.
Excel BI Challenge 76 - Brian Julius Solution
let
Source = NewcomersRaw,
ReplaceAnd = Table.ReplaceValue(Source,"and",",",Replacer.ReplaceText,{"The Newcomers"}),
Split = Table.SplitColumn(ReplaceAnd, "The Newcomers", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv)),
Unpivot = Table.UnpivotOtherColumns(Split, {"Year"}, "Attribute", "Value"),
Trim = Table.TransformColumns(Unpivot,{{"Attribute", Text.Trim, type text}, {"Value", Text.Trim, type text}}),
Filter = Table.SelectRows(Trim, each ([Value] <> "" and [Value] <> "s" and [Value] <> "Tobago" and [Value] <> "Herzogovenia" and [Value] <> "the Grenadines" and [Value] <> "Principe" and [Value] <> "Nevis")),
FirstLetter = Table.SplitColumn(Filter, "Value", Splitter.SplitTextByPositions({0, 1}, false), {"Alphabet", "Value.2"}),
Group = Table.Group(FirstLetter, {"Alphabet"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
Rank = Table.AddRankColumn( Group, "Rank", {"Count", Order.Descending}, [RankKind = RankKind.Dense] ),
FilterNClean = Table.RemoveColumns( Table.SelectRows(Rank, each [Rank] <= 3), "Rank")
in
FilterNClean
@bjulius
Copy link
Author

bjulius commented Nov 30, 2022

Looks simple, but absolutely wicked curveball in this one - filter step needs to include all countries with " and " in the name to avoid counting as two countries:

Filter = Table.SelectRows(Trim, each ([Value] <> "" and [Value] <> "s" and [Value] <> "Tobago" and [Value] <> "Herzogovenia" and [Value] <> "the Grenadines" and [Value] <> "Principe" and [Value] <> "Nevis"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment