Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Conditional formatting when the measure used for formatting contains WEEKNUM

Hello,

 

I have an issue with conditional formatting when using two measures. I want to format the values of my table (where the coloumns are the last 10 weeks) by using the value of a measure.

 

Values of the table:

 

 

ClaimCount = Sum(Claims)

 

 

 

 

First measure that i use as a 'target' value is the average of the last 10 weeks:

 

 

Avg 10w. claims  = 
var _weeks10=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-10 )
var _weeks9=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-9 )
var _weeks8=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-8 )
var _weeks7=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-7 )
var _weeks6=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-6 )
var _weeks5=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-5 )
var _weeks4=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-4 )
var _weeks3=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-3  )
var _weeks2=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2)-2  )
var _weeks1=CALCULATE( [ClaimCount],Dim_date[WeekNo]=WEEKNUM(TODAY(),2) -1 )
return
if(
    (_weeks1+_weeks2+_weeks3+_weeks4+_weeks5+_weeks6+_weeks7+_weeks8+_weeks9+_weeks10)/10=blank(),
    " ",
    (_weeks1+_weeks2+_weeks3+_weeks4+_weeks5+_weeks6+_weeks7+_weeks8+_weeks9+_weeks10)/10
)

 

 

 

 

Then i use this conditional formatting code for the color: 

 

 

Color  = 

switch (TRUE(),
    
        [Avg 10w. claims]
        >= [ClaimCount]
        ,
    "#D64550",
    "#089000"
)

 

 

 

 The problem is that it doesnt work, it only shows the hex color for green (#089000), even though some of the values are under the target value and should show red (#D64550).

 

I have dobble checked, and the Avg. 10 weeks measure shows the correct number on a card (and it is a number). What have i done wrong?

1 ACCEPTED SOLUTION

@Anonymous 

I messed with it a bit and your format measure should work.  I did do a different Avg 10w measure:

Avg 10w. claims = 
VAR _EOPW =
    CALCULATE ( MAX ( Dim_date[Date] ), Dim_date[WeekNo] < WEEKNUM ( TODAY (), 2 ) )
RETURN
    CALCULATE (
        AVERAGEX ( VALUES ( Dim_date[WeekNo] ), [ClaimCount] ),
        DATESBETWEEN ( Dim_date[Date], _EOPW - 69, _EOPW )
    )

 Take a look at my attached sample, see if that helps.  I used your color measure as is to format my ClaimCount field:

jdbuchanan71_0-1596558753102.png

 

View solution in original post

9 REPLIES 9
v-xicai
Community Support
Community Support

Hi @Anonymous ,

 

You may change your formula like DAX below.

 

Color =
VAR ClaimCount =
    SUM ( [Claims] )
VAR _weeks10 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 10 )
VAR _weeks9 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 9 )
VAR _weeks8 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 8 )
VAR _weeks7 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 7 )
VAR _weeks6 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 6 )
VAR _weeks5 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 5 )
VAR _weeks4 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 4 )
VAR _weeks3 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 3 )
VAR _weeks2 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 2 )
VAR _weeks1 =
    CALCULATE ( SUM ( [Claims] ), Dim_date[WeekNo] = WEEKNUM ( TODAY (), 2 ) - 1 )
VAR Avg10wclaims =
    DIVIDE (
        _weeks1 + _weeks2 + _weeks3 + _weeks4 + _weeks5 + _weeks6 + _weeks7 + _weeks8 + _weeks9 + _weeks10,
        10
    )
RETURN
    IF ( Avg10wclaims >= ClaimCount, "#D64550", "#089000" )

Best Regards,

Amy 

 

Community Support Team _ Amy

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Yes, i have used the field option.

@Anonymous 

I messed with it a bit and your format measure should work.  I did do a different Avg 10w measure:

Avg 10w. claims = 
VAR _EOPW =
    CALCULATE ( MAX ( Dim_date[Date] ), Dim_date[WeekNo] < WEEKNUM ( TODAY (), 2 ) )
RETURN
    CALCULATE (
        AVERAGEX ( VALUES ( Dim_date[WeekNo] ), [ClaimCount] ),
        DATESBETWEEN ( Dim_date[Date], _EOPW - 69, _EOPW )
    )

 Take a look at my attached sample, see if that helps.  I used your color measure as is to format my ClaimCount field:

jdbuchanan71_0-1596558753102.png

 

Anonymous
Not applicable

Thank you for your answer.

 

I have tried the solution, but i do not get the exact correct answer as before, and when im calculating the claims manually. Its a small difference, but not good enough.

Rather than using the week number you can just adjust today back to the end of the prior week then calculate the avg using that date as a starting point.  That way having dates with the same week number won't matter.

Avg 10w. claims = 
VAR _EOPW =
    TODAY() - WEEKDAY(TODAY(),2)
RETURN
    CALCULATE (
        AVERAGEX ( VALUES ( Dim_date[WeekNo] ), [ClaimCount] ),
        DATESBETWEEN ( Dim_date[Date], _EOPW - 69, _EOPW )
    )
Anonymous
Not applicable

The calculated value is a little higher than the real value.

You will need to share your .pbix, it works in my testing file so not sure what the problem is.

Anonymous
Not applicable

I still dont get the exact solution... I've tried different ways now..

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.