Forum Discussion

carlblunck's avatar
carlblunck
Copper Contributor
Aug 19, 2022
Solved

Conditional formatting help

Hi community,

 

Just need some help with some JSON conditional formatting code.  Snip of my list, note the current result values are blank because of the JSON code issue.

Current JSON script.  What have I got wrong?

Essentially want the current result value to display red if:

Goal = under and current result is greater than benchmark

Goal = over and current result is less than benchmark

 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "color": "=if((@currentField > [$Benchmark] && [$Goal]) = 'Under', 'red', if((@currentField < [$Benchmark] && [$Goal]) = 'Over', 'red', 'green')",
    "font-weight": "=if((@currentField > [$Benchmark] && [$Goal]) = 'Under', 'bold', if((@currentField < [$Benchmark] && [$Goal]) = 'Over', 'bold', 'normal')"
  }
}

 

 

Thanks in advance

Carl

  • carlblunck you've missed out the second = 

     

    It should be =='Under' etc

     

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "color": "=if((@currentField > [$Benchmark] && [$Goal]) == 'Under', 'red', if((@currentField < [$Benchmark] && [$Goal]) == 'Over', 'red', 'green')",
    	"font-weight": "=if((@currentField > [$Benchmark] && [$Goal]) == 'Under', 'bold', if((@currentField < [$Benchmark] && [$Goal]) == 'Over', 'bold', 'normal')"
      }
    }

     

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

6 Replies

  • carlblunck There are multiple problems with your JSON like you have few unnecessary opening brackets, few closing brackets are missing and in expression you need to use double equal to (==) for comparison.

     

    Try this JSON & it should work for you:

     

    {
        "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
        "elmType": "div",
        "txtContent": "@currentField",
        "style": {
            "color": "=if(@currentField > [$Benchmark] && [$Goal] == 'Under', 'red', if(@currentField < [$Benchmark] && [$Goal] == 'Over', 'red', 'green'))",
            "font-weight": "=if(@currentField > [$Benchmark] && [$Goal] == 'Under', 'bold', if(@currentField < [$Benchmark] && [$Goal] == 'Over', 'bold', 'normal'))"
        }
    }

     

    Note: You have to use the internal names of columns in JSON in format: [$InternalNameOfColumn].

     

    You can get internal name of columns by following: Find the internal name of SharePoint column 


    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.

    For SharePoint/Power Platform blogs, visit: Ganesh Sanap Blogs

    • RobElliott's avatar
      RobElliott
      Silver Contributor

      ganeshsanap I'd already given the JSON code that works fine, even with the extra brackets. Be careful not to step on someone else's solution.

       

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

      • ganeshsanap's avatar
        ganeshsanap
        MVP

        RobElliott Thank you for your response. Did you actually tried using the JSON you provided?

         

        Try using the JSON you provided & let us know the results. Thank you.

  • RobElliott's avatar
    RobElliott
    Silver Contributor

    carlblunck you've missed out the second = 

     

    It should be =='Under' etc

     

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "color": "=if((@currentField > [$Benchmark] && [$Goal]) == 'Under', 'red', if((@currentField < [$Benchmark] && [$Goal]) == 'Over', 'red', 'green')",
    	"font-weight": "=if((@currentField > [$Benchmark] && [$Goal]) == 'Under', 'bold', if((@currentField < [$Benchmark] && [$Goal]) == 'Over', 'bold', 'normal')"
      }
    }

     

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

Resources