Beast Mode

Beast Mode

Domo IDEAs Conference - Beast Modes - Number Formatting

Coach
edited March 2023 in Beast Mode

Greetings! This is another post highlighting a beast mode from my Domo IDEAs conference session. This one covers how to format numbers a number when concatenating with a string.

Problem:

When concatenating a string with a number the resulting value is a string and unable to utilize Domo's built in formatting.

Solution:

Check the length of the number using the LENGTH function and then get the appropriate segments of the number using SUBSTRING and then combining them all together using CONCAT

  1. -- Author:
  2. -- Created:
  3. -- Last Modified:
  4. -- Description:
  5. -- Checks the length of a number to determine how to insert thousands separators (,) using SUBSTRING to get 3 digit pieces of the number.
  6.  
  7. CONCAT('Total Amount: ',
  8.   CASE WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 12 THEN
  9.   CONCAT(
  10.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 3), ',',
  11.   SUBSTRING(ROUND(SUM(`random_number`), 0), 4, 3), ',',
  12.   SUBSTRING(ROUND(SUM(`random_number`), 0), 7, 3), ',',
  13.   SUBSTRING(ROUND(SUM(`random_number`), 0), 10, 3)
  14.   )
  15. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 11 THEN
  16.   CONCAT(
  17.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 2), ',',
  18.   SUBSTRING(ROUND(SUM(`random_number`), 0), 3, 3), ',',
  19.   SUBSTRING(ROUND(SUM(`random_number`), 0), 6, 3), ',',
  20.   SUBSTRING(ROUND(SUM(`random_number`), 0), 9, 3)
  21.   )
  22. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 10 THEN
  23.   CONCAT(
  24.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 1), ',',
  25.   SUBSTRING(ROUND(SUM(`random_number`), 0), 2, 3), ',',
  26.   SUBSTRING(ROUND(SUM(`random_number`), 0), 5, 3), ',',
  27.   SUBSTRING(ROUND(SUM(`random_number`), 0), 7, 3)
  28.   )
  29. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 9 THEN
  30.   CONCAT(
  31.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 3), ',',
  32.   SUBSTRING(ROUND(SUM(`random_number`), 0), 4, 3), ',',
  33.   SUBSTRING(ROUND(SUM(`random_number`), 0), 7, 3)
  34.   )
  35. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 8 THEN
  36.   CONCAT(
  37.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 2), ',',
  38.   SUBSTRING(ROUND(SUM(`random_number`), 0), 3, 3), ',',
  39.   SUBSTRING(ROUND(SUM(`random_number`), 0), 6, 3)
  40.   )
  41. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 7 THEN
  42.   CONCAT(
  43.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 1), ',',
  44.   SUBSTRING(ROUND(SUM(`random_number`), 0), 2, 3), ',',
  45.   SUBSTRING(ROUND(SUM(`random_number`), 0), 5, 3)
  46.   )
  47. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 6 THEN
  48.   CONCAT(
  49.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 3), ',',
  50.   SUBSTRING(ROUND(SUM(`random_number`), 0), 4, 3)
  51.   )
  52. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 5 THEN
  53.   CONCAT(
  54.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 2), ',',
  55.   SUBSTRING(ROUND(SUM(`random_number`), 0), 3, 3)
  56.   )
  57. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) = 4 THEN
  58.   CONCAT(
  59.   SUBSTRING(ROUND(SUM(`random_number`), 0), 1, 1), ',',
  60.   SUBSTRING(ROUND(SUM(`random_number`), 0), 2, 3)
  61.   )
  62. WHEN LENGTH(ROUND(SUM(`random_number`), 0)) <= 3 THEN
  63.   ROUND(SUM(`random_number`), 0)
  64. WHEN SUM(`random_number`) IS NULL THEN 0
  65. ELSE 'ERROR'
  66. END
  67. )


Note:

These beast modes only go to 100 Billion (12 digits). Any number higher would need to expand using the same logic for higher lengths.

**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In

Comments

  • Here's a link to the video of my session outlining this beast mode: https://www.youtube.com/watch?v=gO8OLpsAk4M&amp;index=6

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Here's a link to the video of my session outlining this beast mode: https://www.youtube.com/watch?v=gO8OLpsAk4M&amp;index=6

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Domo Employee

    Hey Grant, this was super helpful. We were able to add commas as per the INR formatting as well. event the total is working in this case.


  • Member

    Is there any hope of Domo implementing the SQL FORMAT() AS function?

    This kind of workaround should not be necessary.

  • Thanks, extremely useful for when you want to display multiple summary numbers!

  • I was surprised when I tried to use FORMAT in a beast mode and got an error "This calculation is using a nonexistent function". Then I was shocked to find threads going back to 2018 asking for a way to format numbers when converting them to text. I believe that FORMAT is even available in the old version of MySQL that Domo is using. It's very disheartening that this has been left unaddressed for at least 5 years.

  • Contributor

    @Marc Aec, @DHo to my surprise this does work even though the validator says non existent function.

    concat('£', format(sum(somenumbercolumn), 2))

    credit goes to Sam Chalvet for making me aware of this.

  • If it works even though it fails validation, then it is at best unsupported and could stop working in the future. If it fails validation, I can't confidently include it in the best mode.

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In