Tip #1321: Formatting currency values in Power Apps portals

Dollars are dollars are dollars. Until they’re not. I’ve been working on the portal implementation where pricing can look like this:

(Those are UAE Dirhams, if you must know)

Forms seem to be fine but any custom layout, uhm, is suddenly very challenging. I can get values without any issues but can’t format them for display. Before long, an SOS is sent:

Folks, can’t figure out how to get formatted values from fetchxml in liquid. Is it possible at all? 

— some random dude who knows bugger all about portals, let’s call him “George”, for example.

Mighty portallers like Nick Doelman, Colin Vermander, and Andrew Butenko chimed in. Redacted list of their suggestions:

  • Do a join to currency and bring currency sign (g.d. – that would only work half way as the currency sign doesn’t give me a proper localized currency formatting. Where do I put the sign, front or back? Do I use dot or comma as a decimal separator? )
  • Maybe it’s a limitation in the Liquid implementation (g.d. – duh!)
  • Write a plugin to output to another field (g.d. – ugh, did someone said #lowcode?)

It’s been a while since I wrote a half-decent plugin so I decided to dig into the portal source code instead (and you can too). Lo and behold, there are undocumented filters that do exactly what’s needed.

format: format, [culture]

Takes a format string and an optional culture, making it possible to write something like:

<blockquote>
My income is {{ 12345.005 | format: "C", "en-US" }} 
before tax. When I grow up and move to south of France, 
I will earn {{ 67890.049 | format: "C", "fr-FR" }} 
per week.
</blockquote>

Which produces a nice but totally fictitious statement:

My income is $12,345.00 before tax. When I grow up and move to south of France, I will earn 67 890,05 € per week.

currency: entity, attribute, [format]

Format is great but still leaves us with the task of figuring out the currency of the record, etc. Enter currency filter that “formats a given numeric value according to the CRM currency settings for a given record and entity attribute”. Easier to demonstrate:

{% assign q = entities.quote[params['id']] %}
<blockquote>The quote total value is 
{{ q['totalamount'] | currency: q, "totalamount" }}
</blockquote>

That produces something like

The quote total value is £1,234,567.89

The other useful currency and decimal filters include:

basecurrency: [format]

Formats a given numeric value as representing a value in the organization base currency.

invariantculturedecimalvalue: decimals

Returns the unformatted/culture invariant value for a decimal number up to a specified number of decimal places

maxdecimals: decimals, [culture]

Formats a decimal number using specified number of decimals and culture. Strips out trailing zeros and the decimal separator if possible.

decimals: decimals, [culture]

Formats a decimal number using specified number of decimals and culture.

Cover photo by Disha Sheta from Pexels

One thought on “Tip #1321: Formatting currency values in Power Apps portals

  1. […] you read my posts primarily for content around Power Apps Portals, then I suggest you check out CRM Tip of the Day, where George Doubinski dug into the portals source code and discovered […]

Leave a Reply

Your email address will not be published. Required fields are marked *