Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Pandadev
Post Prodigy
Post Prodigy

Is there a Bin to Hex option in PowerBI

Hi I am trying to convert 24 bit binary code to hex

 

A formula like this worked in excel using the Bin2Hex

=RIGHT("0"&BIN2HEX(LEFT(A1,LEN(A1)/3)),2)&RIGHT("0"&BIN2HEX(MID(A1,LEN(A1)/3+1,LEN(A1)/3)),2)&RIGHT("0"&BIN2HEX(MID(A1,2*LEN(A1)/3+1,LEN(A1)/3)),2)

 

An example 110000000101100111011110  should be C059DE

 

 

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

Hi, @Pandadev 

I have a complicated method, if you don't think it’s clumsy  , you can try it:

It needs to use conditional column to convert "4 bit binary " to "1bit hex",then merge them.

97.png M query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0NIAAQwMQ09AQRAMJpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"24bit binary" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"24bit binary", type text}, {"Index", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "24bit binary", "24bit binary - Copy"),
    #"Split Column by Position" = Table.SplitColumn(#"Duplicated Column", "24bit binary - Copy", Splitter.SplitTextByRepeatedLengths(4), {"4bit binary - 1", "4bit binary - 2", "4bit binary - 3", "4bit binary - 4", "4bit binary - 5", "4bit binary - 6"}),
    #"Unpivoted Columns2" = Table.UnpivotOtherColumns(#"Split Column by Position", {"Index", "24bit binary"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns2", "1 bit HEX ", each if [Value] = "0000" then 0 else if [Value] = "0001" then 1 else if [Value] = "0010" then 2 else if [Value] = "0011" then 3 else if [Value] = "0100" then 4 else if [Value] = "0101" then 5 else if [Value] = "0110" then 6 else if [Value] = "0111" then 7 else if [Value] = "1000" then 8 else if [Value] = "1001" then 9 else if [Value] = "1010" then "A" else if [Value] = "1011" then "B" else if [Value] = "1100" then "C" else if [Value] = "1101" then "D" else if [Value] = "1110" then "E" else if [Value] = "1111" then "F" else null),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Conditional Column",{{"1 bit HEX ", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "1 bit HEX "),
    #"Merged Columns" = Table.CombineColumns(#"Pivoted Column",{"4bit binary - 1", "4bit binary - 2", "4bit binary - 3", "4bit binary - 4", "4bit binary - 5", "4bit binary - 6"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

 

For more details,please chech the sample file.

Best Regards,
Community Support Team _ Eason

View solution in original post

6 REPLIES 6
amitchandak
Super User
Super User

I tried this dax solution , but it comes up with an error . I converted the binary long number from text to decimal , tried converting it to binary , where it just goes to orange binary , but keep getting a MOD has wrong data type or result is too large

DEC2HEX =
VAR ConvertMe = 'Reg'[Mode S Binary]
VAR Base = 24
VAR BitTable =
GENERATESERIES ( 1, 6 )
VAR DEC2HEX =
CONCATENATEX(
BitTable,
VAR c = MOD ( TRUNC ( ConvertMe / POWER ( base, [value] - 1 ) ),base )
RETURN SWITCH(c,10,"A",11,"B",12,"C",13,"D",14,"E",15,"F",c),
,
[Value],Desc)
       
RETURN
{ DEC2HEX } // Returns 'FF'

@Pandadev ,

the GENERATESERIES ( 1, 6 ) should be till 8 or 16

 

My advice would be to use the power query way. All DAx ways are a bit complex.

I tried the M code for MODE S , which is the text binary converted to bin

= Table.AddColumn(#"Changed Type2", "Mode S Bin", each Binary.ToText([Mode S Binary])) converts back to text correctly

= Table.AddColumn(#"Changed Type2", "Mode S Hex", each BinaryEncoding.Hex([Mode S Binary])) when I tried this it just brings back error

Hi, @Pandadev 

I have a complicated method, if you don't think it’s clumsy  , you can try it:

It needs to use conditional column to convert "4 bit binary " to "1bit hex",then merge them.

97.png M query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0NIAAQwMQ09AQRAMJpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"24bit binary" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"24bit binary", type text}, {"Index", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "24bit binary", "24bit binary - Copy"),
    #"Split Column by Position" = Table.SplitColumn(#"Duplicated Column", "24bit binary - Copy", Splitter.SplitTextByRepeatedLengths(4), {"4bit binary - 1", "4bit binary - 2", "4bit binary - 3", "4bit binary - 4", "4bit binary - 5", "4bit binary - 6"}),
    #"Unpivoted Columns2" = Table.UnpivotOtherColumns(#"Split Column by Position", {"Index", "24bit binary"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns2", "1 bit HEX ", each if [Value] = "0000" then 0 else if [Value] = "0001" then 1 else if [Value] = "0010" then 2 else if [Value] = "0011" then 3 else if [Value] = "0100" then 4 else if [Value] = "0101" then 5 else if [Value] = "0110" then 6 else if [Value] = "0111" then 7 else if [Value] = "1000" then 8 else if [Value] = "1001" then 9 else if [Value] = "1010" then "A" else if [Value] = "1011" then "B" else if [Value] = "1100" then "C" else if [Value] = "1101" then "D" else if [Value] = "1110" then "E" else if [Value] = "1111" then "F" else null),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Conditional Column",{{"1 bit HEX ", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "1 bit HEX "),
    #"Merged Columns" = Table.CombineColumns(#"Pivoted Column",{"4bit binary - 1", "4bit binary - 2", "4bit binary - 3", "4bit binary - 4", "4bit binary - 5", "4bit binary - 6"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

 

For more details,please chech the sample file.

Best Regards,
Community Support Team _ Eason

Thanks , that solution worked great

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.