Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Mch201
Contributor III
Contributor III

Using If statements to set colors

I am trying to set colors of the bars based on the difference between target and actual sales. but the problem I am having is that if the difference is exactly 5, the color will be blue otherwise it will show gray. even though the code says  if (Avg(Targets)- (Avg([Sales])) >= 5, then set blue as the color. it only show blue when the difference is EXACLTY 5. how do i fix this?

 

 

=If(Avg(Targets)- (Avg([Sales])) >= 10, Yellow(),

If(Avg(Targets)- (Avg([Sales])) >= 15, Red(),

If(Avg(Targets)- (Avg([Sales])) >= 5, LightBlue(),

)))

Labels (5)
3 Replies
edwin
Master II
Master II

average will result in a real number and not an integer and depending on how you display your data (Qlik Sense rounds it off) so depending on your precision, add fraction to force it to round up in your if statement.  you will need to test this fraction (format your expression to show more decimal points so you know what you are dealing with)

also test for >=15 before testing for >=10 as it is Red will never show

Ksrinivasan
Specialist
Specialist

hi,

you have to set between ranges as below

If(Avg(Targets)- Avg([Sales])) >= 10 and (Avg(Targets)- Avg([Sales])) <= 15, Yellow(),

 

ksrinivasan

PradeepK
Creator II
Creator II

Use Below Code for reference.. 

 

if( $(vActual) >=15 ,Red()
    ,if( $(vActual) >=10 ,Yellow()
        ,if( $(vActual) >= 5 ,blue()
            ,Black()	// for all values less than 5
        )
    )
)

 

Where vActual is variable holding calculation condition 

 

Avg(Targets)- Avg(Sales)

 

 

Also @edwin  is right! Be aware of the code execution flow.