1

Apologies, I am relatively new to SharePoint/JSON, So forgive me if I am not seeing the obvious. I would be INCREDIBLY grateful if someone would help.

I would like to apply conditional formatting to a date field using a nested IF statement.

IF the current field is blank, then set the CSS class to 'blocked'. However, if the current field is populated, run a second IF: IF the current field is more than 1 year before today, then set the CSS class to 'blocked', however if the current field is within the last year, set the CSS class to 'good'.

I have ran the following, without success. No formatting is applied whatsoever.


{

  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",

"elmType": "div",

"attributes": {

"class": "=if(toString(@currentField)=='', 'sp-field-severity--blocked', if([@currentField]+31104000000 < @now,'sp-field-severity--blocked', 'sp-field-severity--good'))"
  },

"txtContent": "@currentField"

}

HOWEVER, when I split the conditions into two, they work independently.

  1. =IF empty, blocked, good.
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "attributes": {
    "class": "=if(toString(@currentField)=='','sp-field-severity--blocked', 'sp-field-severity--good')"
  },
  "txtContent": "@currentField"
}
  1. =IF exceeds 1 year, blocked, good.
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "attributes": {
    "class": "=if(@currentField+31104000000 < @now, 'sp-field-severity--blocked', 'sp-field-severity--good')"
  },
  "txtContent": "@currentField"
}

My next step would be to add the corresponding CSS class icon into the field.

  • Are you trying to format the row or just the date field? – Ganesh Sanap Aug 22 at 10:40
  • @GaneshSanap Hi, I am only trying to format the date field, not the row. Thanks. – taylort139 Aug 22 at 11:07
  • I would massively appreciate some direction on this, @GaneshSanap or others. I have been stuck for 3 days.. I'm certain that the solution is something very straightforward, which I have missed. – taylort139 Aug 23 at 10:51
  • I have tried the same you are trying to achieve and the result is same. I don't understand why it is not working after merging both conditions. – Ganesh Sanap Aug 23 at 10:54
  • It is working fine for second condition. But, it is not working for blank/empty fields after merging both codes. – Ganesh Sanap Aug 23 at 10:56

Your Answer

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

Browse other questions tagged or ask your own question.