This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreDid you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now
Want to format a measure based on a slicer selection, the measure value, or another conditional way? Now you can! With dynamic format strings for measures a DAX expression can now be used to determine what format string a measure will use.
These dynamic format strings for measures are the same dynamic format strings already available in calculation groups! If you are familiar with these in calculation items, the DAX patterns you used there are applicable here to individual measures. Learn more about calculation groups at https://aka.ms/calculationgroups.
The FORMAT function can also be used in a measure DAX expression to conditionally apply a format string, but the drawback is if the measure was a numeric data type, the use of FORMAT changes the measure to a text data type. As a text data type the measure is then no longer usable as values in visuals. To maintain the measure as a numeric data type and conditionally apply a format string, you can now use dynamic format strings for measures to get around this drawback!
Dynamic format strings for measures is in public preview. To use this feature first go to File > Options and settings > Options > Preview features and check the box next to Dynamic format strings for measures.
Graphical_user_interface_Description_automatically_generated_with_medium_confide
To add a dynamic format string to a measure,
1) Currency conversion and showing the results with the correct currency format string – A common scenario is in a report converting from one currency to another. When the value is converted, the report should show the converted currency in the appropriate format.
There are step by step instructions available at https://learn.microsoft.com/power-bi/create-reports/desktop-dynamic-format-strings#example to set up the Adventure Works 2020 PBIX file with the needed tables for this currency conversion example.
Here in my Adventure Works 2020 data model, I have the yearly conversion rates for some countries in the table ‘Yearly Average Exchange Rates’.
Table_Description_automatically_generated_with_medium_confidence
I also have a table with the countries I want to convert to, and their currency format strings as the 'Country Currency Format Strings' table.
Table_Description_automatically_generated
To join these to my existing tables, I add relationships to the new tables.
First, I create a relationship between the ‘Country Currency Format Strings’ table and ‘Yearly Average Exchange Rates’ on the Country column. Make the relationship one to many and so that ‘Country Currency Format Strings’ filters ‘Yearly Average Exchange Rates’.
Second, I create a relationship between the ‘Date’ table and the ‘Yearly Average Exchange Rates’ table on the Year column. Make the relationship many to many and so that ‘Date’ table filters” the ‘Yearly Average Exchange Rates’ table.
A_screenshot_of_a_computer_Description_automatically_generated
With all this set up, I then create a measure to compute the exchange rate with this DAX expression:
And then I create the measure [Converted Sales Amount] to convert my existing [Sales Amount] measure to other currencies with this DAX expression:
Converted Sales Amount =
SUMX (
'Date',
CALCULATE (
[Sales Amount] * [Exchange Rate (Yearly Avg)]
)
)
Now when a country is selected in the slicer, the [Converted Sales Amount] shows not only the converted [Sales Amount] but also shows the value in the specified format. And because this is done with the dynamic format strings for measures, the underlying data type of the measure remains numeric and is usable in any visual like before.
Graphical_user_interface_application_Description_automatically_generated
2) User driven format strings – Different teams may want to see the report formatted in different ways for their reporting needs. Dynamic format strings for measures can be report user driven to indicate how they want to see the number formatted. And in some formatting cases, such as when abbreviating 1,000s, the dynamic format strings for measures can also conditionally format based on the measure value.
A_screenshot_of_a_computer_Description_automatically_generated
For the top slicer I create a calculated table to define the format strings in my model. In Modeling ribbon I click on New table and enter the following DAX expression:
Which looks like this in the Data view:
Graphical_user_interface_text_application_table_Description_automatically_genera
Now I create this measure:
Then I go to the Measure tools ribbon and choose Dynamic from the Format list box. In the DAX formula bar, I enter the following dynamic format string DAX expression:
I could have alternatively also written it without using SELECTEDMEASURE() using the measure name itself, [Sales Amount (Pick)], like this:
With this now in place, the visuals with [Sales Amount (Pick)] will show the value in the specified format. In the cases where abbreviating to 1000s such as when using K to abbreviate, any number under 1000 will show the full value and not be abbreviated.
A_screenshot_of_a_product_list_Description_automatically_generated_with_low_conf
3) Measure driven format strings – In the previous example the measure itself was used to determine how the value would be formatted when abbreviated by 1000s. I can take this further and have the measure value fully determine the abbreviation limits and formatting. This is like the “Auto” option in display units on visuals, but now I get to define exactly how it works with my measure using dynamic format strings.
I create a new measure called [Sales Amount (Auto)] defined as:
And I add this dynamic format string expression to [Sales Amount (Auto)]:
If I had negative values, I could include those limits, and if I had very large numbers, I could also abbreviate and format them appropriately too. Now the visuals will show this measure abbreviated and in format I have defined:
A_screenshot_of_a_computer_Description_automatically_generated_with_medium_confi
4) Locale driven currency conversion – I may know the locale of the country I am converting to, but not the exact currency format rules, or noticed it is tricky to get that format string correct for currencies that flip the . and , in their format strings. Here we can leverage the updated FORMAT function that can also take a locale argument! When used with the dynamic format strings for measures we can still keep the measure as a numeric data type and use FORMAT. The drawback to this approach is you cannot customize the currency format string for that locale further.
I create the Locale table using the Modeling ribbon’s New table and enter the following DAX expression:
I then create a relationship from the ‘Locale’ table to the ‘Country Currency Format Strings’ table on the Country column. This should be many to one, and cross filtering in both directions for this example. As these are small tables and not part of a complex model, I am ok with the using cross filtering in both directions here.
A_screenshot_of_a_computer_Description_automatically_generated
I create a new measure [Converted Sales Amount (Locale)] with this DAX expression:
And give [Converted Sales Amount (Locale)] measure the following dynamic format string DAX expression:
The FORMAT function itself will output a string that already formatted the value of the measure into the appropriate currency format for the given locale. Because I want this string to be used literally, that is, I don’t want any part of it to be used like a format string, I am wrapping it in single quotes.
Now I can use this locale driven currency formatting in the visuals!
A_screenshot_of_a_computer_Description_automatically_generated_with_medium_confi
I can now compare the locale driven currency format strings with the first example where I defined the format string manually.
A_picture_containing_text_screenshot_font_number_Description_automatically_gener
These four examples are just the beginning. I'm excited to see all the other creative ways you'll use dynamic format strings for measures in your reports! There are some things to keep in mind using dynamic format strings for measures.
Try dynamic format strings for measures today and learn more at https://learn.microsoft.com/power-bi/create-reports/desktop-dynamic-format-strings.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.