Changing Hexadecimal, Oct and Binary to Decimal in a Single Energy Question Perform

Date:


A Power Query Function to Convert HEX, OCT and BIN values to DEC

Some time in the past I wrote a blogpost on methods to use Unicode characters in Energy BI. In that blogpost I used a recursive Energy Question perform to transform Hex values to Dec values. A couple of weeks again one in every of my website guests kindly shared his non-recursive model of Energy Question perform which fantastically does the job. An enormous shout out to Rocco Lupoi for sharing his code. So, I made a decision to share it with everybody so extra folks can leverage his good Energy Question perform. I’ve touched his code a bit although, however it was extra of a beauty change, so all credit of this blogpost goes to Rocco. The advantages of his code isn’t restricted to being non-recursive. The code under converts numbers of any base when the bottom is smaller than 16 like Binary and Oct, so it’s not restricted to Hex values solely. The opposite good thing about the under code is that it’s not case delicate (be aware to the digits step on the code under).

Right here is the fnHex2Dec perform for Energy Question:

(enter as textual content, non-compulsory base as quantity) as quantity =>
let
        values = [
                0=0,
                1=1,
                2=2,
                3=3,
                4=4,
                5=5,
                6=6,
                7=7,
                8=8,
                9=9,
                A=10,
                B=11,
                C=12,
                D=13,
                E=14,
                F=15
        ],
        digits = Textual content.ToList(Textual content.Higher(enter)),
        dim = Checklist.Depend(digits)-1,
        exp = if base=null then 16 else base,
        Consequence = Checklist.Sum(
                        Checklist.Rework(
                                {0..dim}
                                , every Report.Subject(values, digits{_}) * Quantity.Energy(exp, dim - _)
                                )
                        )
in
        Consequence

As you see within the code above, the base parameter is non-compulsory, so if not supplied base 16 can be the default.

That is how we will invoke the above perform:

fnHex2Dec("AbCdEf", null)
Invoking fnHex2Dec function to convert numbers of any base to decimal

Right here is the outcomes of invoking the fnHex2Dec perform to transform binary to decimal:

fnHex2Dec("101010111100110111101111", 2)
Power Query binary to decimal

And this one the way it works to transform Oct to decimal:

fnHex2Dec("52746757", 8)
Power Query Oct to decimal

What do you consider the perform above? Go away your ideas within the feedback part under.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Subscribe

spot_imgspot_img

Popular

More like this
Related

TaxProf Weblog

The 2022 Congress of the Worldwide Fiscal Affiliation...

3 Methods You Can Keep away from The ‘Quiet Quitting’ Pattern in Your Enterprise

Opinions expressed by Entrepreneur contributors are their very...

Why Do Engineering Charges Change and What Does It Affect?

The software program business retains evolving, offering new...