let KoelnerPhonetik = (input as text) as text => let _upper = Text.Upper(Text.Trim(input)), _replaced = Text.Replace(Text.Replace(Text.Replace(Text.Replace(_upper, "Ä", "A"), "Ö", "O"), "Ü", "U"), "ß", "SS"), _lettersOnly = Text.Select(_replaced, {"A".."Z"}), chars = Text.ToList(_lettersOnly), len = List.Count(chars), isVowel = (c as text) as logical => List.Contains({"A","E","I","J","O","U","Y"}, c), prevChar = (i as number) as text => if i > 0 then chars{i - 1} else "", nextChar = (i as number) as text => if i < len - 1 then chars{i + 1} else "", encodeChar = (i as number) as list => let c = chars{i}, p = prevChar(i), n = nextChar(i), code = if c = "H" then null else if isVowel(c) then "0" else if c = "B" then "1" else if c = "P" and n = "H" then "3" else if c = "P" then "1" else if List.Contains({"D","T"}, c) and List.Contains({"C","S","Z"}, n) then "8" else if List.Contains({"D","T"}, c) then "2" else if List.Contains({"F","V","W"}, c) then "3" else if List.Contains({"G","K","Q"}, c) then "4" else if c = "C" then let nIn = List.Contains({"A","H","K","L","O","Q","R","U","X"}, n), pSZ = List.Contains({"S","Z"}, p), ccode = if nIn and (not pSZ) then "4" else "8" in ccode else if c = "X" then if List.Contains({"C","K","Q"}, p) then "8" else "48" else if c = "L" then "5" else if List.Contains({"M","N"}, c) then "6" else if c = "R" then "7" else if List.Contains({"S","Z"}, c) then "8" else null, asList = if code = null then {} else if code = "48" then {"4","8"} else {code} in asList, rawDigits = List.Combine(List.Transform({0..len-1}, each encodeChar(_))), dedup = List.Transform( {0..List.Count(rawDigits)-1}, (i) => if i = 0 then rawDigits{i} else if rawDigits{i} = rawDigits{i-1} then null else rawDigits{i} ), dedupClean = List.RemoveNulls(dedup), finalDigits = if List.Count(dedupClean) = 0 then {} else let head = dedupClean{0}, tail = List.Skip(dedupClean, 1), tailNoZeros = List.RemoveItems(tail, {"0"}), rebuilt = {head} & tailNoZeros in rebuilt, result = if List.Count(finalDigits) = 0 then "" else Text.Combine(finalDigits, "") in result in KoelnerPhonetik