Forum Discussion

BrandThe's avatar
BrandThe
Copper Contributor
Dec 12, 2024

JSON Conditioning with multiple IF statements

Hello I am trying to format a date column. The date in the column is a date that someone completed a certification and that certification expires after one year. I am trying to using conditional formatting comparing the date in the column against the current date to show if the certification is still valid, getting close to expiring, or expired. I can't seem to get it to work.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "attributes": {
    "class": "=if((@currentField >= @now – 31536000000 && @currentField <= @now – 5184000000), 'sp-field-severity--good', if((@currentField >= @now – 5183999999 && @currentField <= @now – 2592000000), 'sp-field-severity--warning', if(@currentField >= @now – 2591999999, 'sp-field-severity--severeWarning', 'sp-field-severity--blocked'))) + ' ms-fontColor-neutralSecondary'"
  },
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      },
      "attributes": {
        "iconName": "=if(@currentField >= @now – 31536000000 && @currentField <= @now – 5184000000), 'CheckMark', if(@currentField >= @now – 5183999999 &&  @currentField <= @now – 2592000000), 'Error', if(@currentField >= @now – 2591999999, 'Warning', 'ErrorBadge')))"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}

2 Replies

  • {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "attributes": {
        "class": "=if([$CertificationDate] >= @now - 31536000000, 'sp-field-severity--good', if([$CertificationDate] >= @now - 5184000000, 'sp-field-severity--warning', 'sp-field-severity--severeWarning'))"
      },
      "children": [
        {
          "elmType": "span",
          "style": {
            "display": "inline-block",
            "padding": "0 4px"
          },
          "attributes": {
            "iconName": "=if([$CertificationDate] >= @now - 31536000000, 'CheckMark', if([$CertificationDate] >= @now - 5184000000, 'Warning', 'ErrorBadge'))"
          }
        },
        {
          "elmType": "span",
          "txtContent": "=[$CertificationDate]"
        }
      ]
    }
    

    Fixed it for you ! :)

  • Hello BrandThe 

     

    here is a thread with more explanation about multiple IF statements in JSON

     

    https://techcommunity.microsoft.com/discussions/sharepoint_general/json-using-multiple-conditions-within-if-statement/3873451?WT.mc_id=DX-MVP-5004845

     

    Best, Dave

Resources