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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
hastingse14
New Member

formatting time differences

hi,

 

I’ve got data like this with check in and check out times.  There's a column of the time difference which is decimal because it’s the fraction of 24h

 

hastingse14_0-1763436380923.png

 

 

I have a column which shows if the Time Difference is > 7 hours created by this

 

GT 7h = if(sum(Time_Log[Time Diff]) > 0.29166 , 1, 0)

 

I want to add another test to see if a time difference is more than 10 hours.  Then I want to add formatting to the Time Difference column based on if it’s > 7h or > 10h

 

thank you.

 

G

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @hastingse14 

 

Download an example PBIX file here

 

I’d rewrite the measure so that it works out if a Time Diff is >7h or >10h and then decides what colour to use for the cell background, rather than return decimal values like 1 which you then have to write more code to decide colours

 

GT 7h = 

VAR _time_diff = SELECTEDVALUE('Time_Log'[Time Diff])
VAR _7h = 420 / 1440
VAR _10h = 600 / 1440

RETURN 

SWITCH( TRUE(),

    _time_diff > _10h, "red",

    _time_diff > _7h, "orange",

    "green"

)

 

The SWITCH function requires a default value which I have set to "green" here but if you don’t want a default (all cells will get this unless they are red or orange) then make it "" instead.

 

Click on the down arrow beside the Time Diff column in the visual and then select Conditional Formatting-> Background Color

 

PhilipTreacy_0-1763443291100.png

 

Choose these settings and click OK

 

PhilipTreacy_1-1763443317706.png

 

Your table should like something like this

 

PhilipTreacy_2-1763443338376.png

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

4 REPLIES 4
v-ssriganesh
Community Support
Community Support

Hello @hastingse14,

We hope you're doing well. Could you please confirm whether your issue has been resolved or if you're still facing challenges? Your update will be valuable to the community and may assist others with similar concerns.

Thank you.

krishnakanth240
Continued Contributor
Continued Contributor

Hi @hastingse14 

 

7 hours = 7/24 = 0.29
10 hours = 10/24 = 0.42

 

Calculated columns:

TimeDiff_Hours = Time_Log[Time Diff] * 24

/*To show the value in hours (rather than fraction of 24)*/

GT_7h_Column = IF( Time_Log[Time Diff] > (7/24), 1, 0 )

GT_10h_Column = IF( Time_Log[Time Diff] > (10/24), 1, 0 )

 

Measures (for aggregated checks in visuals or cards)
GT 7h (Measure) = IF( SUM(Time_Log[Time Diff]) > (7/24), 1, 0 )

GT 10h (Measure) = IF( SUM(Time_Log[Time Diff]) > (10/24), 1, 0 )

 

Conditional formatting:

/*Create a measure that returns a hex color depending on the row’s hours*/

TimeDiff_Color =
VAR hours = SELECTEDVALUE(Time_Log[Time Diff]) * 24
RETURN
SWITCH(
TRUE(),
hours > 10, "#D32F2F", // red for > 10h
hours > 7, "#FBC02D", // amber for > 7h
"#FFFFFF" // white (default)
)

@hastingse14

v-ssriganesh
Community Support
Community Support

Hello @hastingse14

Thank you for posting your query in the Microsoft Fabric Community Forum, and thanks to @PhilipTreacy for sharing valuable insights.

 

Could you please confirm if your query has been resolved by the provided solutions? This would be helpful for other members who may encounter similar issues.

 

Thank you for being part of the Microsoft Fabric Community.

 

PhilipTreacy
Super User
Super User

Hi @hastingse14 

 

Download an example PBIX file here

 

I’d rewrite the measure so that it works out if a Time Diff is >7h or >10h and then decides what colour to use for the cell background, rather than return decimal values like 1 which you then have to write more code to decide colours

 

GT 7h = 

VAR _time_diff = SELECTEDVALUE('Time_Log'[Time Diff])
VAR _7h = 420 / 1440
VAR _10h = 600 / 1440

RETURN 

SWITCH( TRUE(),

    _time_diff > _10h, "red",

    _time_diff > _7h, "orange",

    "green"

)

 

The SWITCH function requires a default value which I have set to "green" here but if you don’t want a default (all cells will get this unless they are red or orange) then make it "" instead.

 

Click on the down arrow beside the Time Diff column in the visual and then select Conditional Formatting-> Background Color

 

PhilipTreacy_0-1763443291100.png

 

Choose these settings and click OK

 

PhilipTreacy_1-1763443317706.png

 

Your table should like something like this

 

PhilipTreacy_2-1763443338376.png

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.