Forum Discussion
Mahendran_C_S
4 years agoHelper I
Converting Number column to binary and map the binary 1's based on position with other table/values
I have a column with data type whole number with numbers like 39,191,68 etc... I have a requirement where I have to map these values with another table or list. The mapping table contains : Num...
Ehren
4 years agoMicrosoft Employee
Here's some M code that converts a number to a list of binary ones and zeroes, then returns the position+1 for all the ones.
let
val = 39,
div = List.Generate(() => val, (x) => x >= 1, (x) => Number.RoundDown(x / 2)),
mod = List.Transform(div, each Number.Mod(_, 2)),
pos = List.Select(List.Positions(mod), each mod{_} = 1)
in
List.Transform(pos, each _ + 1)
Hopefully you can do the rest.
mahoneypat
4 years agoMicrosoft Employee
Nice approach, Ehren . I modified your code to a function, so it can be used in a custom column with the column of numbers and return the concatenated 1s and 0s.
(num)=>
let
val = num,
div = List.Generate(() => val, (x) => x >= 1, (x) => Number.RoundDown(x / 2)),
mod = List.Transform(List.Reverse(div), each Number.ToText(Number.Mod(_, 2)))
in
Text.Combine(mod, "")
Pat