Forum Discussion

Amp13's avatar
Amp13
Copper Contributor
Mar 19, 2024

JSON - Conditional Formatting

Hello smart people!

I am struggling to get my json correct for a sharepoint list column.  Can someone please help me.  This is what I would like to see happen in my date column. 

Date of today =  green  >#D0FA8E

Date yesterday = yellow >#F9F9A3

Date older than 2 days  = red > #F95C08

 

Please and thank you!!!

 

  • Amp13 Try using JSON column formatting like: 

     

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "background-color": "=if(Number(@currentField)==0,'',if(@currentField < addDays(@now, -2),'#F95C08',if(@currentField < addDays(@now, -1),'#F9F9A3','#D0FA8E')))",
        "color": "black",
        "padding-left": "10px"
      }
    }

     

    You can adjust the conditions as per your requirements.

     

    Here I have used Number(@currentField) == 0 to check if SharePoint date column is blank or not. You can find other ways to check if SharePoint date & time column is empty or not at: SharePoint JSON formatting: Check if date & time column is blank/empty.

     

    You can also use internal name of date column in format like [$MyDateColumn] in place of @currentField. You can get the internal name of your SharePoint list columns by following this article: How to find the Internal name of columns in SharePoint Online?

     

    Output


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

  • Amp13 Try using JSON column formatting like: 

     

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "background-color": "=if(Number(@currentField)==0,'',if(@currentField < addDays(@now, -2),'#F95C08',if(@currentField < addDays(@now, -1),'#F9F9A3','#D0FA8E')))",
        "color": "black",
        "padding-left": "10px"
      }
    }

     

    You can adjust the conditions as per your requirements.

     

    Here I have used Number(@currentField) == 0 to check if SharePoint date column is blank or not. You can find other ways to check if SharePoint date & time column is empty or not at: SharePoint JSON formatting: Check if date & time column is blank/empty.

     

    You can also use internal name of date column in format like [$MyDateColumn] in place of @currentField. You can get the internal name of your SharePoint list columns by following this article: How to find the Internal name of columns in SharePoint Online?

     

    Output


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

    • Amp13's avatar
      Amp13
      Copper Contributor
      Thank you very much! Greatly appreciated! Have a great day! Works like a charm!

Resources