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
ggzmorsh
Helper II
Helper II

IF statement for time

I need an IF statement where if a specific column has a Date and Hour it would get me the Final Date by checking a separate hour table or a condition that if the Hour is between 00:00:00 and 08:00:00 it would return the previous date.

An example table below shows that ID A to D has an hour between 00:00:00 and 08:00:00 and it must return Date 1 which is reduced by 1 and if the condition is false simply return Date1.

 

IDDate 1HourFinal Date
a01/05/202001:00:0001/04/2020
b01/05/202002:00:0001/04/2020
c01/05/202003:00:0001/04/2020
d01/05/202004:00:0001/04/2020
e01/05/202011:00:0001/05/2020

 

The formula i tried to use is 

Final Time = IF(TIME(Hour,00,00) < TIME(8,0,0),Date1 - 1,Date1)
 
but returned an error. TIME has the wrong data type or result is too large or too small.
1 ACCEPTED SOLUTION
az38
Community Champion
Community Champion

Hi @ggzmorsh 

try 

Final Time = IF([Hour] < TIME(8,0,0),Date1 - 1,Date1)

or

Final Time = IF(TIME(VALUE(LEFT([Hour],2)),00,00) < TIME(8,0,0),Date1 - 1,Date1)

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

6 REPLIES 6
ggzmorsh
Helper II
Helper II

I do not know why my data is always returning the statement true. So the workaround i did is i extracted just the HOUR from the Hour column and used the IF statement on that and it worked. 

Anonymous
Not applicable

@ggzmorsh 

The issue is possibly cause by unmatched format, please check the format for the date(Date/ Date time) and hour(Time) columns.

 

Best,
Paul

az38
Community Champion
Community Champion

Hi @ggzmorsh 

try 

Final Time = IF([Hour] < TIME(8,0,0),Date1 - 1,Date1)

or

Final Time = IF(TIME(VALUE(LEFT([Hour],2)),00,00) < TIME(8,0,0),Date1 - 1,Date1)

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

I forgot to mention this:

Final Time = IF([Hour] < TIME(8,0,0),Date1 - 1,Date1)

When i used this formula earlier it would always return as true. 

az38
Community Champion
Community Champion

@ggzmorsh mayb e your Hour field is set as Text. So, try my second statement or change Data type in power query editor mode

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

I used this:

 

FinalTime = IF('Table'[HOUR] < TIME(8;0;0);'Table'[DATE1]-1;'Table'[DATE1])
 
It works:
Screenshot_13.png
 
Please note that Month and Day are switched in date fields, because i use a different Locale. Thus this is calculated 1 day back from the 1st of may.

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.