Saturday 25 November 2017

Converting hexadecimal values in Tableau

While Tableau has a lot of basic maths and string functions, coping with hexadecimal numbers is not something it can do natively. Let's see how we can do this with calculated fields.

To make the solution easier we break the problem into two: Interpret each hexadecimal digit, and then put the results together to convert the whole number to decimal. We create a calculated field for the rightmost hex digit (1s)
ifnull(int(right([Hex No],1)), 
case right([Hex No],1) 
when 'a' then 10 
when 'b' then 11 
when 'c' then 12 
when 'd' then 13 
when 'e' then 14 
when 'f' then 15 
end)
We do the same for the next digit (16s) where we can use mid([Hex No],5,1) assuming our numbers are in the format 0x023c. Likewise for the next two digits, 16^2s and 16^3s. Then we bring everything together:
[16^3s]*16*16*16+[16^2s]*16*16+[16s]*16+[1s]

No comments:

Post a Comment