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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
rafiql09
Frequent Visitor

How to create a dax function to create calculated column based on time range

From the below date column a new calculated column 'shift_id, is needed

rafiql09_0-1674116882625.png

if the time is 8am to 9am time shift_id is 1

if the time is 7am to 8am shift_id is 5

if the time is 4pm to5pm shift_id is 10

 

within the same date, date would be filtered based on power bi slicer though.

etc...

 

thnaks

1 ACCEPTED SOLUTION

hi @rafiql09 

try like:

shift_id = 
VAR _hour = HOUR([TimeField])
RETURN
SWITCH(
    TRUE(),
    _hour>=8&&_hour<=9, 1,
    _hour>=7&&_hour<8, 5,
    _hour>=16&&_hour<=17, 10,
    999
)

FreemanZ_0-1674283747306.png

View solution in original post

5 REPLIES 5
MFelix
Super User
Super User

Hi @rafiql09 

 

For this you need to add a new calculated column wiht a sintax similar to:

 

=if Time.Hour([Column1])>=7 and Time.Hour([Column1])<8 then 5 else if Time.Hour([Column1]) >=8 and Time.Hour([Column1]) < 9 then 1 else if Time.Hour([Column1]) >=16 and Time.Hour([Column1]) < 17 then 10 else null

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @rafiql09

My bad the code I sent you was ffor Power Query. Please forgive me the lack of information but glad you already solved it. 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks for helping me starting it .

I have made the changes and looks ok. But multiple IF giving me error!! Can you help please?

 

rafiql09_1-1674282995128.png

 

hi @rafiql09 

try like:

shift_id = 
VAR _hour = HOUR([TimeField])
RETURN
SWITCH(
    TRUE(),
    _hour>=8&&_hour<=9, 1,
    _hour>=7&&_hour<8, 5,
    _hour>=16&&_hour<=17, 10,
    999
)

FreemanZ_0-1674283747306.png

Thanks @MFelix for the help. It raises syntax error which I could not fix.

 

rafiql09_0-1674278789621.png

 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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