Forum Discussion
Help with formatting measure(Swicth) with conditional based on selected parameter in power bi
- Anonymous1 year ago
Hi YagoXavier
Thank you for using Microsoft Community Forum.
The issue you're experiencing is due to how the FORMAT() function works in DAX. While FORMAT() can apply specific numeric formats like currency or decimal places, it converts the result into a text data type. This means visuals like bar and column charts, which require numeric values for rendering and aggregations (e.g., tooltips, data labels), cannot interpret the formatted output correctly, leading to visuals disappearing or behaving unexpectedly.
To dynamically apply formatting based on the selected comparison parameter without converting your measure to text, we recommend using Calculation Groups via Tabular Editor. Calculation Groups allow you to define both the expression and the numeric format string separately, preserving the numeric data type while enabling the desired formatting (such as currency, whole number, or decimal) depending on the user’s slicer selection. This approach resolves the formatting limitation cleanly and is the officially supported method for dynamic formatting in Power BI.
If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
YagoXavier Create measures for each parameter with the appropriate formatting.
DAX
NetRevenueA = FORMAT([Net Revenue A], "Currency")
DiscountA = FORMAT([Discount A], "Currency")
GrossRevenueA = FORMAT([Gross Revenue A], "Currency")
DailyAverageA = FORMAT([Daily Average A], "0.00")
PAA = FORMAT([P.A. A], "0.00")
MPA = FORMAT([M.P. A], "0.00")
NumberOfCouponsA = FORMAT([Number of Coupons A], "0")
NumberOfDaysWorkedA = FORMAT([Number of Days Worked A], "0")
NumberOfItemsA = FORMAT([Number of Items A], "0")
AverageTicketA = FORMAT([Average Ticket A], "Currency")
Use a SWITCH statement to dynamically select the appropriate formatted measure based on the selected parameter.
DAX
% vG_Period_A_Formatted =
SWITCH(
TRUE(),
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Net Revenue", [NetRevenueA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Discount", [DiscountA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Gross Revenue", [GrossRevenueA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Daily Average", [DailyAverageA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare P.A.", [PAA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare M.P.", [MPA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Qty. Coupons", [NumberOfCouponsA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Days Worked", [NumberOfDaysWorkedA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Number of Items", [NumberOfItemsA],
SELECTEDVALUE('Comparison Pair'[Comparison Pair (groups)]) = "Compare Average Ticket", [AverageTicketA]
)
Replace the original measure in your visuals with the new dynamic measure % vG_Period_A_Formatted.
- YagoXavier1 year ago
Helper I
Hello bhanu_guatam
First of all, thank you for answering and trying to solve my problem, but the same thing happened when I tried to use the format in the switch or in the measurement itself.
It gives an error and the bar doesn't appear, I believe the format is only for using text.I'm going to try something related to the design groups to test or else put the "measurement types" (currency, decimal number with 1 place and decimal number with 2 places) in the measurement and try another look. This one has worked best so far, the only problem is that it greatly limited the formatting of my measurement. If you succeed, come back here to comment, it might help someone.
Thanks bhanu_guatam.
- Anonymous1 year agoNot applicable
Hi YagoXavier
Thank you for using Microsoft Community Forum.
The issue you're experiencing is due to how the FORMAT() function works in DAX. While FORMAT() can apply specific numeric formats like currency or decimal places, it converts the result into a text data type. This means visuals like bar and column charts, which require numeric values for rendering and aggregations (e.g., tooltips, data labels), cannot interpret the formatted output correctly, leading to visuals disappearing or behaving unexpectedly.
To dynamically apply formatting based on the selected comparison parameter without converting your measure to text, we recommend using Calculation Groups via Tabular Editor. Calculation Groups allow you to define both the expression and the numeric format string separately, preserving the numeric data type while enabling the desired formatting (such as currency, whole number, or decimal) depending on the user’s slicer selection. This approach resolves the formatting limitation cleanly and is the officially supported method for dynamic formatting in Power BI.
If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.- Anonymous1 year agoNot applicable
Hi YagoXavier
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.