0

I am trying to use JSON code on SharePoint list - works fine with 2 conditions(green,red) but I am not able to add additional condition, third with yellow...not sure how it should look.

workable:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if(@currentField >= @now - 7776000000 ,'green', (if(@currentField >= @now - 604800000, '#ffa59b','red'))"
  }
}

I am trying to add "yellow now - 4800000"

Update

We need 2 colors - green and red. Red should be for todays date and within range of 30 days from today in the past. Rest in green - both ways - ahead and in the past. Example : Today is the 10.5 that should be in red, 30 days back still in red. Above 30 days in the past Green and Tomorrow continuously green.

2
  • Please elaborate your required color conditions in question. I mean when you need green, red or yellow color. Apr 12 at 11:06
  • Ok so there is completely different condition now - We need 2 colors - green and red. Red should be for todays date and within range of 30 days from today in the past. Rest in green - both ways - ahead and in the past. Example : Today is the 10.5 that should be in red, 30 days back still in red. Above 30 days in the past Green and Tomorrow continuously green.
    – szzz
    May 10 at 9:15

2 Answers 2

0

Nearly had it! Just need to continue nesting additional if statements in the else operand spot:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if(@currentField >= @now - 7776000000 ,'green', if(@currentField >= @now - 604800000, '#ffa59b', if(@currentField >= @now - 4800000,'yellow','red')))"
  }
}
4
  • HI Chris! Thanks for the code but when I paste it to my column I receive error: Error saving column format: {"d": {"CustomFormatter": "{\"$schema\":\"https:\u002F\u002Fdeveloper.microsoft.com\u002Fjson-schemas\u002Fsp\u002Fv2\u002Fcolumn-formatting.schema.json\",\"elmType\":\"div\",\"txtContent\":\"@currentField\",\"style\":{\"background-color\":\"=if(currentField >= now - 7776000000 ,'green', if(currentField >= now - 604800000, '#ffa59b', if(currentField >= now - 4800000,'yellow','red')))\"}}", "Id": ""}} Switch to design mode This formatting JSON will be discarded
    – szzz
    Apr 11 at 7:06
  • I removed @ because it was blocking me...
    – szzz
    Apr 11 at 7:07
  • I'm not sure what the issue is. What you pasted in the comments looks to be escaped and won't work in that format. Is that just a comments issue or is that the actual format text you are trying to use? This format requires you to use the column menu > Column settings > Format this column and then choose Advanced Mode to paste the code above exactly as it's written. Then hit save. If you are getting the design mode message then you are trying to switch to the wizard which will not work with this custom format. Apr 11 at 15:44
  • Ok so there is completely different condition now - We need 2 colors - green and red. Red should be for todays date and within range of 30 days from today in the past. Rest in green - both ways - ahead and in the past. Example : Today is the 10.5 that should be in red, 30 days back still in red. Above 30 days in the past Green and Tomorrow continuously green.
    – szzz
    May 10 at 9:16
0

Try this formula:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "@currentField",
    "style": {
      "background-color": "=if(@currentField <= @now && (@currentField) >= Number(@now - 2592000000) ,'red','green')"
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.