In this blog, I'll walk you through how I created a Switch KPI measure and solved the formatting issue using Power BI's Dynamic Format String feature.
The first step is to create a table to allow users to select different KPIs dynamically.
This parameter allows users to choose which field or dimension they want to analyze (e.g., Category, City, Segment). You can create a Field Parameter called Field Selector with the following fields:
To dynamically display values based on the selected KPI, I created the following measure:
Switch KPIs = SWITCH(
SELECTEDVALUE('Switch KPIs'[Switch KPIs]),
"Sales", [Total Sales],
"Quantity", [Total Quantity],
"Profit", [Total Profit],
[Total Sales]
)
This measure dynamically switches between Total Sales, Total Quantity, and Total Profit based on the user's selection in the Switch KPIs slicer.
Now, you can use a stacked column chart to visualize the selected KPI dynamically.
Now, when you select Sales and City in the slicers, the chart will correctly display Sales by City.
While the dynamic KPI selection worked, the formatting was inconsistent:
By default, Power BI applies a single formatting style to a measure, which means you can't assign different formats to different KPIs dynamically.
To fix this, we used Power BI’s Dynamic Format String feature. Here's how:
1. Click on the Switch KPIs measure.
2. Go to the Measure Tools ribbon.
3. In the Format dropdown, select Dynamic.
I then entered the following format string:
IF(SELECTEDVALUE('Switch KPIs'[Switch KPIs])= "Sales", "$#,##0",
IF(SELECTEDVALUE('Switch KPIs'[Switch KPIs])= "Profit", "$#,##0",
"#,##0"))
This ensures:
Using Dynamic Format String, I was able to resolve the formatting inconsistency in my Switch KPIs measure. Now, users can dynamically switch between KPIs without losing appropriate formatting for each metric.This feature is particularly useful when dealing with mixed data types (currency vs. general numbers) in Power BI visualizations.