Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Charlz_1
Contributor II
Contributor II

Background color expression

Hi,

I'm new on Qlik Sense and I want to change background color in time column.

before 12h00 it should be green

from 12h00 to 2h00 it should be yellow then red

please help with appropriate expression for this.

Thank you.....

Labels (1)
7 Replies
Kaloyan
Partner - Contributor II
Partner - Contributor II

Hi @Charlz_1 ,

When you add your time field to a table, use the Background color expression setting in the dimension (or measure) properties.

Here is an expression based on a very simple dataset:
If(Time<'12:00:00', Green(), If(Time>='12:00:00' and Time<'14:00:00', Yellow(),  Red()))

Of course you can use the RGB function if you want to adjust the green/yellow/red nuances or add the field as Master Dimension / Master Measure and adjust the color from Master Items settings.

The dataset:
Load 
  Timestamp(Timestamp#(Time, 'hh:mm:ss'), 'hh:mm:ss') as Time
Inline [
Time
10:30:00
12:21:00
15:30:00
];

I hope this helps.

Welcome to QS world - have a great journey !!

Charlz_1
Contributor II
Contributor II
Author

Thank you Kaloyan, let me try it and see how it works..
Thanks a lot.
Charlz_1
Contributor II
Contributor II
Author

Hi Kaloyan,

I tried the solution you provided, it only returns red color nothing else.

I used the first solution you provided, the expression.

Kaloyan
Partner - Contributor II
Partner - Contributor II

Hi Charlz,

If it is only showing red, then something in the conditions before that could be wrong, therefore the expression returns the result associated with condition not fulfilled (red).
Could you please share a bit more information about your dataset and task, so that me (and others here) get better idea of the use case? Also, could you please share the expression you have written?

Thank you

Charlz_1
Contributor II
Contributor II
Author

I think the problem is because it is datetime format, how do I go about it if it's like this?

Kaloyan
Partner - Contributor II
Partner - Contributor II

Hi @Charlz_1 ,

You need to first properly interpret your field containing days, hours and seconds as a timestamp (using Timestamp# function).
Then you need to create a second field that contains only the  hours:minutes:seconds part (using the Frac function) and use that field for the conditional coloring.
I used your dates and loaded them Inline, then performed date and time transformations and here is the result.

Example dataset:
Time:
Load
   Time(Frac(Timestamp#(TimeStamp, 'YYYY-MM-DD hh:mm:ss.ffffff')))     as TimePart,
   TimeStamp(Timestamp#(TimeStamp, 'YYYY-MM-DD hh:mm:ss.ffffff'), 'YYYY-MM-DD hh:mm:ss')    as TimeStamp
Inline [
TimeStamp
2021-11-03 09:48:00.000000
2021-12-29 16:03:00.000000
2021-11-22 12:24:00.000000
];

Expression:
If(TimePart< '12:00:00', Green(),
  If(TimePart>='12:00:00' and TimePart<'14:00:00', Yellow()
    , Red()
   )
)


Result

Kaloyan_0-1641933558847.png

 

Charlz_1
Contributor II
Contributor II
Author

Hi @Kaloyan 

Thanks a lot, it's working now.

I appreciate your help 😊