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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
GeekAlfPro
Helper V
Helper V

Time comparison duration/difference

Hi,

 

I have a table with columns of hours

i have a theorical hour and a real hour, and I want to have the advances and delays

In Dax, how can I get the differences lower than 0?

i tried this code, but without success

 

heure colonne = switch(TRUE(),
	and(
		and(HOUR('Table Tests Horaires'[Heure Théorique])>=21, HOUR('Table Tests Horaires'[Heure Théorique])<=23),
    HOUR('Table Tests Horaires'[Heure réelle])>=0),
    'Table Tests Horaires'[Heure réelle]-'Table Tests Horaires'[Heure Théorique]+24,
    
	and(
	and(
		HOUR('Table Tests Horaires'[Heure réelle])<=23, HOUR('Table Tests Horaires'[Heure réelle])>=21),
    HOUR('Table Tests Horaires'[Heure Théorique]>=0)),
    'Table Tests Horaires'[Heure réelle]-('Table Tests Horaires'[Heure Théorique]-24),	
	
	'Table Tests Horaires'[Heure réelle]-'Table Tests Horaires'[Heure Théorique]-24
    )

 

 

The column hour column gives me positive time but not differences

 

GeekAlfPro_0-1662481940909.png

 

Thanks for your help

 

1 ACCEPTED SOLUTION

Hi,

 

i finally manage to get the result i want.

i set 2 more columns, based on the decimal result.

if it's <0 it's an advance

if it's >0 it's delayed

 

the final code is : 

Colonne Ecarts = IF(
    HOUR(Journal[Heure théo])>= 21 && HOUR(Journal[Heure théo])<= 23 && HOUR(Journal[Heure Départ Réelle])>=0 && HOUR(Journal[Heure Départ Réelle])<=3,
     (Journal[Heure Départ Réelle]+1-Journal[Heure théo]), 
     IF(HOUR(Journal[Heure Départ Réelle])<=23 && HOUR(Journal[Heure Départ Réelle])>=21 && HOUR(Journal[Heure théo])>=0 && HOUR(Journal[Heure théo])<=3,
      (Journal[Heure Départ Réelle]-1-Journal[Heure théo]),
      (Journal[Heure Départ Réelle]-Journal[Heure théo])
    ))

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Hi  @GeekAlfPro ,

Here are the steps you can follow:

1. Create calculated column.

Column = IF( HOUR('Test'[actual]) >=21 && HOUR('Test'[real]) <=3 , [real]+24-[actual] ,  IF( HOUR('Test'[actual])<=3 && HOUR('Test'[real]) >=21 , [real]-24-[actual] , [real]-[actual]))

2. Result:

vyangliumsft_1-1662536092997.png

If you need pbix, please click here.

Time comparison durationdifference.pbix

 

Best Regards,

Liu Yang

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

Hi,

 

i finally manage to get the result i want.

i set 2 more columns, based on the decimal result.

if it's <0 it's an advance

if it's >0 it's delayed

 

the final code is : 

Colonne Ecarts = IF(
    HOUR(Journal[Heure théo])>= 21 && HOUR(Journal[Heure théo])<= 23 && HOUR(Journal[Heure Départ Réelle])>=0 && HOUR(Journal[Heure Départ Réelle])<=3,
     (Journal[Heure Départ Réelle]+1-Journal[Heure théo]), 
     IF(HOUR(Journal[Heure Départ Réelle])<=23 && HOUR(Journal[Heure Départ Réelle])>=21 && HOUR(Journal[Heure théo])>=0 && HOUR(Journal[Heure théo])<=3,
      (Journal[Heure Départ Réelle]-1-Journal[Heure théo]),
      (Journal[Heure Départ Réelle]-Journal[Heure théo])
    ))

Hi @Anonymous 

 

Thanks for your response, your code is better than mine !

Nevertheless, in the second row, i expect to have a negative différence because the real is before the actual.

in your suggestion, i see only a positive difference, how can i have the negative difference ?

 

Another Question : what is the unit of the result of the calculated column ?

 

Thanks again !

Greg_Deckler
Community Champion
Community Champion

@GeekAlfPro Just subtract them and then multiply by 24*60*60



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler 

 

Thanks for your proposal, but when i test, if the difference is negative, i have no data once i formated the column in hour but maybe as it's an hour format, it can't be negative...

@GeekAlfPro Right, I would use a custom format string instead.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.