Search code examples
powerbiswitch-statementreporting

PowerBi new column with switch statement and dates


I am trying to update background color of a column in PowerBi report. For that I am trying to create a column in power BI report and adding a switch statement to display background as
Red if dates greater than 2 weeks Green if dates less than 2 weeks etc.,

The switch statement is working fine for strings but not with dates. Any suggestion or pointer would help to make dates switch work.

Working Switch for string conditions by creating a new column

columnNameColorNumber = SWITCH(MyData[name],"abc",1,"def",2,3)

New column shows numbers as expected

Failing Switch when trying to create a column in power bi with date conditions

columnNameColorNumber = SWITCH(MyData[reqDate],"(MyData[reqDate]>(Today()-14))",1,2)

New column shows error


Solution

  • Try:

    columnNameColorNumber = SWITCH(TRUE(),MyData[reqDate]>Today()-14,1,2)
    

    to add more switch checks it would look like this:

    columnNameColorNumber = SWITCH(TRUE(),
       MyData[reqDate]>Today()-14,1,
       MyData[reqDate]>Today()-30,2,3)