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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

using "if" function extract time

Hello, I am trying to extract hours from my time column which contains hours in a  hh:mm: SS format. I want to extract hours when the time is between 9:30 to 10:00 I want to get the value "9:30 - 10:00" otherwise I want to get the hours.

I used the following DAX formula, but it didn't work. 

 

New column = IF([time]>=09:30:00; IF([time]<=10:00:00; "9:30 -10:00" ;HOUR([Time]))

I get the error,  the syntax for ":" is incorrect

 

when I change the formula to

New Column = IF([Time]>=("09:30:00")&&[Time]<=("10:00:00");"9:30 -10:00";HOUR([Time]))

I get the error Dax comparision do not support comparing values of type date with text, but my time column has a datatype time.

1 ACCEPTED SOLUTION
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

 

For your scenario, I create a data sample as example. Assuming that you have Time column like this.

Time
8/2/2018 9:28:20
8/1/2018 9:30:00
8/1/2018 9:30:05
8/1/2018 9:45:20
8/1/2018 10:00:00
8/1/2018 10:01:00
8/2/2018 10:00:05

You could create a calculated column with the formula below.

 

Column =
VAR h =
    HOUR ( 'Table'[Time] )
VAR m =
    MINUTE ( 'Table'[Time] )
VAR s =
    SECOND ( 'Table'[Time] )
RETURN
    IF (
        h >= 9
            && m >= 30,
        'Table'[Time],
        IF ( h <= 10 && m = 0 && s = 0, 'Table'[Time] )
    )

Then you will get the output like this:

if time.PNG

 

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

 

For your scenario, I create a data sample as example. Assuming that you have Time column like this.

Time
8/2/2018 9:28:20
8/1/2018 9:30:00
8/1/2018 9:30:05
8/1/2018 9:45:20
8/1/2018 10:00:00
8/1/2018 10:01:00
8/2/2018 10:00:05

You could create a calculated column with the formula below.

 

Column =
VAR h =
    HOUR ( 'Table'[Time] )
VAR m =
    MINUTE ( 'Table'[Time] )
VAR s =
    SECOND ( 'Table'[Time] )
RETURN
    IF (
        h >= 9
            && m >= 30,
        'Table'[Time],
        IF ( h <= 10 && m = 0 && s = 0, 'Table'[Time] )
    )

Then you will get the output like this:

if time.PNG

 

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.