Hi all,
I am new to Power Query, I needed to convert Decimal to Hex, here is my solution using recursive approach.
It is working like a charm, however, I have no idea if it is an optimal solution. So, comments are appreciated:
I am new to Power Query, I needed to convert Decimal to Hex, here is my solution using recursive approach.
It is working like a charm, however, I have no idea if it is an optimal solution. So, comments are appreciated:
Code:
(input as number, optional string as nullable text) =>
let
inputString = if string = null then "" else string,
reminder = Number.Mod(input, 16),
helperString = if reminder > 9 then
List.First(List.Select({ {10, "A"}, {11, "B"}, {12, "C"}, {13, "D"}, {14, "E"}, {15, "F"} }, each _{0} = reminder)){1} & "," & inputString
else
Text.From(reminder) & "," & inputString,
quotient = Number.IntegerDivide(input, 16),
finalResult = if quotient > 0 then
@fxNumberToHex(quotient, helperString)
else List.Accumulate(Text.Split(helperString, ","), "", (state, current) => Text.From(state) & Text.From(current) & "")
in
finalResult