DAX function for displaying decimals as fractions

  • DAX function for displaying decimals as fractions

    Posted by DSC Communities on July 6, 2021 at 10:28 am
    • Sarah Fox

      Member

      July 6, 2021 at 10:28 AM

      Hi there,

      I am wanting to display decimal values as fractions, e.g 3/5 as 3/5 and not 0.6 etc.Ā 

      I have tried using the following DAX function from https://community.powerbi.com/t5/Desktop/show-numbers-as-fraction/m-p/125253 :

      Fraction =
      var given=2.5
      var precision=100000
      return (given*precision/GCD(given*precision,precision))&”/”&(precision/GCD(given*precision,precision))

      It is working well for the most part but I am getting a number like this for a value that should be 15/26.Ā 

      guruguruguru_0-1625580668266.png

      Also, I would like it to say, for example 2/2 or 5/5 instead of 1/1 in the case of a whole number. How do I do this?Ā 

      Ā 

      Thanks so much in advance.Ā Ā 

      ——————————
      Sarah Fox
      ——————————

    • Samyr AbrahĆ£o Moises

      Member

      July 6, 2021 at 1:36 PM

      Check if this post helps you: Solved: Convert Decimals to Custom Fractions – Microsoft Power BI Community

      Cheers!

      ——————————
      Samyr Abrahão Moises
      ——————————
      ——————————————-

    • James Watts

      Member

      July 8, 2021 at 4:31 AM

      I think you are expecting too much.Ā  The GCD function can only return factors of the precision – so for this value of 10,000, it will be multiples of 2 and 5.Ā  It is never going to reduce the divisor to 26.Ā  The GCD of 5,769 and 10,000 is 1.

      Also, you should put a ROUND(,0) on the RETURN value to get rid of the decimal places in the numerator:

      RETURN ROUND((given*precision/GCD(given*precision,precision)) ,0) &”/”&(precision/GCD(given*precision,precision))

      ——————————
      James Watts
      Business Intelligence Analyst
      ——————————
      ——————————————-

    DSC Communities replied 4 years, 1 month ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

The discussion ‘DAX function for displaying decimals as fractions’ is closed to new replies.

Start of Discussion
0 of 0 replies June 2018
Now

Welcome to our new site!

Here you will find a wealth of information created for peopleĀ  that are on a mission to redefine business models with cloud techinologies, AI, automation, low code / no code applications, data, security & more to compete in the Acceleration Economy!