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
juju
Helper III
Helper III

Daypart calculations for new column

 

I am looking to create a new column in my dataset that classifies time between 9am and 5pm into one bucket ( say "business hours")  and 5pm to 8 am as "after hours". I have a [TIME] column on which this is based.

 

I am totally new to PowerBI and getting myself confused between the difference between when the DAX language is needed and when the M language is needed. I am doing this on the PowerBI desktop.  Strange - I cant find any discussion about daypart manipulations ( maybe didnt look enough).

 

My query to create the column:

 

if ( [Time] >= "9:00:00 AM" and [Time] <= "5:00:00 PM" ) 
then "Business hours" 
else "After hours"

 

Getting "error" values in the new column created. Tried many iterations without success.

2 ACCEPTED SOLUTIONS
parry2k
Super User
Super User

here is something i tried, i created hour column using DAX , hope it is helpful

hour1.PNG

 

Dax Expression

 

Hours = if(sample2[Time1] >= time(9,0,0) && sample2[Time1] <= time(17,0,0), "Business", "After")


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

juju
Helper III
Helper III

The solution by parry2k works if you are using DAX to create the formula for the new column ( directly from the Report tab). But when trying to addd a new "custom column" from the query editor, looks like the M language is used there instead ( confusing!!). Here is the M langauge that worked for me there :

 

 

if [Time] >= Time.FromText("09:00:00") and [Time] <= Time.FromText("17:00:00") 
then "Business Hours"
else "After Hours"

 

The comparable DAX formula from the Report tab is:

 

 

if ([Time] >= time(9,0,0) && [Time] <= time(17,0,0) ) 
, "Business hours" 
, "After hours")

 

View solution in original post

6 REPLIES 6
juju
Helper III
Helper III

The solution by parry2k works if you are using DAX to create the formula for the new column ( directly from the Report tab). But when trying to addd a new "custom column" from the query editor, looks like the M language is used there instead ( confusing!!). Here is the M langauge that worked for me there :

 

 

if [Time] >= Time.FromText("09:00:00") and [Time] <= Time.FromText("17:00:00") 
then "Business Hours"
else "After Hours"

 

The comparable DAX formula from the Report tab is:

 

 

if ([Time] >= time(9,0,0) && [Time] <= time(17,0,0) ) 
, "Business hours" 
, "After hours")

 

parry2k
Super User
Super User

here is something i tried, i created hour column using DAX , hope it is helpful

hour1.PNG

 

Dax Expression

 

Hours = if(sample2[Time1] >= time(9,0,0) && sample2[Time1] <= time(17,0,0), "Business", "After")


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

This worked for me only when I created the formula from the "Report " tab using the Dax editor there like you did in your exmaple. The same formula throws errors when doing so from the query editor ( via "edit queries" on ribbon)

Strange: I am using this ( modified from your code above) still not working. Getting a "token literal expected" error in the editor:

 

I have only one table so didint use the qualified table reference name. the error is on the "&&" part.

 

if([Time] >= time(9,0,0) && [Time] <= time(17,0,0), "Business", "After")

 

FYI I am doing this from the query editor screen and clicking on "custom column" under "add column" tab on the ribbon.

If i change it to below, the syntax error goes away but now new error : "Expression Error " The name 'time' wasnt recognized. Make sure its spelled correctly "!!

 

Notice "time' in lowercaps:

 

if [Time] >= time(9,0,0) and [Time] <= time(5,0,0) then "9am-5pm" else "6pm-8am"
parry2k
Super User
Super User

I think following will work, may be some syntax issue but just to give you an idea, add a column and create following DAX:

 

if (Table[Time] >= time(9,0,0) && Table[Time] <= time(17,0,0) ) 
, "Business hours" 
, "After hours")


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.