Change field BACKGROUND color based on value of ANOTHER field drop down

New Contributor

I'm using the JSON formatting field on column ESTVal, which is a number field.  I want to show a background of GREEN when the selection in STATVal is Committed.  I'm trying this JSON and see no format change:

 

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"style": {
"background-color": "=if([$STATVal] == 'Committed', '#FFA07A, '')"
}
}

6 Replies

@Czyz0  add the txtContent": "@currentField" line as shown below and make sure that your #FFA07A has a closing single quote which it doesn't at the moment. The correct JSON is:

 

 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if([$STATVal] == 'Committed','#FFA07A', '' )"
  }
}

 

 

 

Rob
Los Gallardos
Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

Hi @Czyz0,

In addition to what @RobElliott said:


You also need to make sure, that your "STATVal" field is in the view. You cannot access fields that are not being displayed.

Best Regards,
Sven

I'm still not seeing any change to my background for the field. :\
What's your current JSON code looking like now you've made the changes. And if you could give a screenshot of your view of the list...
I'm applying the JSON to the ESTVal column in the list. For my testing purposes, I do have both the ESTVal and ESTStat in the view, but the end goal is to hide the Stat from the view, as the bg color will become a visual representation of the status. So, even if I get it working, it sounds like I'll break it when I simplify the view.... :\

Back to an excel via attached data set....

@Czyz0 

Well, alternatively you could get creative:

Create a calculated column and concatenate ESTStat, a seperator sign (like "-") and ESTVal

=[ESTStat]&"-"&[ESTVal]

This concatenates the status and the value into a single field.

Then use this json formatting on that field to extract the value into txtContent (Everything after "-") and apply the styling if everything in front of "-" equals "Commited"

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
   "txtContent": "=substring(@currentField.displayValue,indexOf(@currentField.displayValue,'-')+1,indexOf(@currentField.displayValue + '^', '^'))",
  "style": {
    "background-color": "=if(substring(@currentField.displayValue,0,indexOf(@currentField.displayValue,'-')) == 'Committed','#FFA07A', '' )"
  }
}


Then you can hide both fields ESTStat and ESTVal and leave the new field in the view.

Best Regards,
Sven