Forum Discussion
Formatting new card visual
Hey all
i m using card new visual. I m placing my "total amount collected" measure . In the same card below this visual i m using another measure value that will return the previous year, amount collected and difference in percentage. I m using the second measure value to be displayed under the Callout values-> Label, where i m giving the second measure value by using conditional formatting.
Now i want help with , I want to make the year value bold, and a conditional color for variance like green if variance > 0 and red if variance < 0.
My second measure calculates prev year value, amount collected in previous year and YoY variance. I m returning these values in my second measure
Hi Babu34 ,
You can't directly format different parts of a single measure's text output with different styles, like bolding one word and coloring another. This is because Power BI's formatting options in a standard visual apply to the entire text field, not to individual words or characters within it.
The most straightforward workaround is to use multiple, smaller Card (new) visuals and then group them. You would use your main card for the "total amount collected." Below it, place a small card with a measure that returns only the previous year; you can format this card's callout value to be bold. Next to it, add another small card with a measure for just the YoY variance percentage. You can then apply conditional formatting to this specific card's value, making it green if the variance is positive and red if it's negative. Once they are arranged, select all the visuals, right-click, and group them to make them behave as a single object.
A more advanced but powerful method is to write a DAX measure that generates an SVG image. This approach gives you precise control over the formatting of each text element. The measure dynamically creates an image containing your text with the desired bolding and conditional colors.
Formatted Previous Year Label = VAR PreviousYear = MAX('Date'[Year]) - 1 VAR PreviousYearAmount = CALCULATE( [Total Amount Collected], SAMEPERIODLASTYEAR('Date'[Date]) ) VAR Variance = ([Total Amount Collected] - PreviousYearAmount) / PreviousYearAmount -- Format the values as text VAR PreviousYearText = FORMAT(PreviousYear, "0") VAR PYAmountText = FORMAT(PreviousYearAmount, "$#,##0K") VAR VarianceText = FORMAT(Variance, "+0.0%;-0.0%;0.0%") -- Shows + for positive -- Determine the color for the variance VAR VarianceColor = IF(Variance > 0, "green", "red") -- Construct the SVG code as a text string VAR SVG_Code = " <svg xmlns='http://www.w3.org/2000/svg' width='250' height='30'> <text x='0' y='15' font-family='Segoe UI' font-size='12' font-weight='bold' fill='black'>" & PreviousYearText & "</text> <text x='50' y='15' font-family='Segoe UI' font-size='12' fill='grey'>" & " | " & PYAmountText & " | " & "</text> <text x='150' y='15' font-family='Segoe UI' font-size='12' font-weight='bold' fill='" & VarianceColor & "'>" & VarianceText & "</text> </svg> " -- Return the SVG string with the required data image header RETURN "data:image/svg+xml;utf8," & SVG_CodeAfter creating the DAX measure above, you need to set its Data category to Image URL in the Model view. Then, instead of placing it in a card's label, you add this measure to a Table visual. The table will render the SVG code as the custom-formatted image you designed. You can then resize this table and place it beneath your main card to achieve the desired look.
5 Replies
- DataNinja777Super User
Hi Babu34 ,
You can't directly format different parts of a single measure's text output with different styles, like bolding one word and coloring another. This is because Power BI's formatting options in a standard visual apply to the entire text field, not to individual words or characters within it.
The most straightforward workaround is to use multiple, smaller Card (new) visuals and then group them. You would use your main card for the "total amount collected." Below it, place a small card with a measure that returns only the previous year; you can format this card's callout value to be bold. Next to it, add another small card with a measure for just the YoY variance percentage. You can then apply conditional formatting to this specific card's value, making it green if the variance is positive and red if it's negative. Once they are arranged, select all the visuals, right-click, and group them to make them behave as a single object.
A more advanced but powerful method is to write a DAX measure that generates an SVG image. This approach gives you precise control over the formatting of each text element. The measure dynamically creates an image containing your text with the desired bolding and conditional colors.
Formatted Previous Year Label = VAR PreviousYear = MAX('Date'[Year]) - 1 VAR PreviousYearAmount = CALCULATE( [Total Amount Collected], SAMEPERIODLASTYEAR('Date'[Date]) ) VAR Variance = ([Total Amount Collected] - PreviousYearAmount) / PreviousYearAmount -- Format the values as text VAR PreviousYearText = FORMAT(PreviousYear, "0") VAR PYAmountText = FORMAT(PreviousYearAmount, "$#,##0K") VAR VarianceText = FORMAT(Variance, "+0.0%;-0.0%;0.0%") -- Shows + for positive -- Determine the color for the variance VAR VarianceColor = IF(Variance > 0, "green", "red") -- Construct the SVG code as a text string VAR SVG_Code = " <svg xmlns='http://www.w3.org/2000/svg' width='250' height='30'> <text x='0' y='15' font-family='Segoe UI' font-size='12' font-weight='bold' fill='black'>" & PreviousYearText & "</text> <text x='50' y='15' font-family='Segoe UI' font-size='12' fill='grey'>" & " | " & PYAmountText & " | " & "</text> <text x='150' y='15' font-family='Segoe UI' font-size='12' font-weight='bold' fill='" & VarianceColor & "'>" & VarianceText & "</text> </svg> " -- Return the SVG string with the required data image header RETURN "data:image/svg+xml;utf8," & SVG_CodeAfter creating the DAX measure above, you need to set its Data category to Image URL in the Model view. Then, instead of placing it in a card's label, you add this measure to a Table visual. The table will render the SVG code as the custom-formatted image you designed. You can then resize this table and place it beneath your main card to achieve the desired look.
- v-sshirivoluCommunity Support
Hi Babu34 ,
Thank you for reaching out to Microsoft Fabric community.
Here are the steps I followed:Step 1: Create the Required Measures
Total Amount Collected:
TotalAmountCollected = SUM(Sales[AmountCollected])Previous Year Amount Collected:
PreviousYearAmount =
CALCULATE(
[TotalAmountCollected],
SAMEPERIODLASTYEAR(Sales[Date])
)YoY Variance:
YoYVariance =
[TotalAmountCollected] - [PreviousYearAmount]YoY Variance Percentage:
YoYVariancePercentage =
IF(
[PreviousYearAmount] <> 0,
([YoYVariance] / [PreviousYearAmount]) * 100,
0
)Step 2: Create the Formatted Measure
FormattedMeasure =
VAR PrevYearValue = CALCULATE(SUM(Sales[AmountCollected]), SAMEPERIODLASTYEAR(Sales[Date]))
VAR CurrentYearValue = SUM(Sales[AmountCollected])
VAR VariancePercentage =
IF(
PrevYearValue <> 0,
(CurrentYearValue - PrevYearValue) / PrevYearValue * 100,
0
)
VAR YearText = YEAR(TODAY()) - 1
RETURN
YearText & " - " &
"Prev Year: " & FORMAT(PrevYearValue, "#,0") & " | " &
"Variance: " & FORMAT(VariancePercentage, "#0.0") & "%"
Step 3: Add the Card VisualSelect the Card visual and add the FormattedMeasure to the Values section.
Step 4: Apply Conditional Formatting for Variance - Green if positive, Red if negative
VarianceColor =VAR VarianceValue = [YoYVariancePercentage]RETURNIF(VarianceValue > 0, "#00FF00", "#FF0000")Apply Conditional Formatting:
Select the Card visual, go to the Format pane, and expand Callout values. Click the fx button next to Font color, choose Format by: Field value, and select VarianceColor. Click OK.
Now, the font color for the variance will be green for positive values and red for negative values.
Verify the Results
The Card visual should show the Total Amount Collected, the Previous Year Amount, and YoY variance percentage. The YoY variance will be conditionally formatted in green or red based on its value.
Output -Please find below attached .pbix file for your reference.
Regards,
Sreeteja.- v-sshirivoluCommunity Support
Hi Babu34 ,
I hope the information provided above assists you in resolving the issue. If you have any additional questions or concerns, please do not hesitate to contact us. We are here to support you and will be happy to help with any further assistance you may need.
- v-sshirivoluCommunity Support
Hi Babu34 ,
I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We’re always here to support you