Back to Knowledge Center

Power Platform: Conditional formatting with a date field

Conditional formatting in SharePoint lists or libraries is used to customize how fields are displayed based on item values. To do this, you can either create quick rules or use advanced mode and add your own JSON code.

List View

Problem

I created rules on a column of type Date in a SharePoint list, using the rule manager in Select Column -> Column setting -> Format this column

List menu

The rules are the following (my column name is Date Stamp):

If Date Stamp is before Today (relative), show column as Red
If Date Stamp is equal to Today (relative), show column as Yellow
If Date Stamp is after Today (relative), show column as Green

I didn’t get the expected results. The JSON code behind the conditional formatting was referencing the Date and Time from my Date column, even though I have the “Include Time” setting off for the column. In addition, the date and time wasn’t matching up with the local time zone set on the site, which messed up the formatting (not 100% sure of the reason, could be related to the tenant’s data center location).

Solution

I changed the code to reference the date “friendly format” (May 16) instead of the date and time (5/16/2022 12:00:00 AM).

  • In Select Column -> Column setting -> Format this column, I clicked on Advanced mode
  • I copied the JSON code already provided into a preferred code editor, to make it easier to read, I used Notepad++
  • I looked for the date column referenced in the code; my column name is Date Stamp. Anywhere I found “[$DateStamp]”, I replaced it with “@currentField.displayValue”
  • I copied the code back into the Advanced mode screen and clicked Save
  • My conditional formatting works as expected

Click below for the original code versus the modified code:


Original Code

Modified Code

Leverage our experience and expertise

We help your team make the most of your technology budget! Our discovery phase is performed by an experienced technologist. We deliver solutions that save you money and make your team more productive.