Forum Discussion
Converting Number column to binary and map the binary 1's based on position with other table/values
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.
- mahoneypat4 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
- Mahendran_C_S4 years agoHelper I
Since I am a beginner to power query M language.I was not able to achieve the requirement with the above solution. In the above solution, a value is passed and the final result is displayed in the list.
What I wanted is I have to transform the entire column with the mapping country name as string/text .For the column below :
Number 39 78 I want my resultant column to be :
Resultant column India,Canada,France,Singapore Canada,France,USA,Malaysia I don't know how to map the values with the country name and display them in the resultant column as a string . Could you please help me out in solving this? Thanks in advance.