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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
MAAbdullah47
Helper V
Helper V

Creating conditional column based on time for another column

Dear All

 

  I created 3 global variables with name: Morning, Noon and evening and I set default values '11:00' ,'16:00' and '23:59' for each variable respectively. Now I need to add new condition column based on column name called time_open_at check if the time <= Morning then it's breakfast, and BTW Morning And Noon then 'Lunch' otherwise if > Noon and < Evening then 'Dinner' how shall I do this?

21 REPLIES 21
Greg_Deckler
Super User
Super User

Are you doing this in Power Query (M) code or in the data model and want to use DAX?


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

The User group moved and I couldn't found the answer.

Here is one way and you do not need any kind of global variable or anything, just a calculated column like this:

 

Column = IF([Time]<TIME(11,0,0),"Morning",IF([Time]<TIME(16,0,0),"Noon","Evening"))

You could modify this to just do less than 16:00:00 be Lunch, otherwise "Dinner".


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

I tried to work around:

 

Meal = IF(TIMEVALUE('order product'[orders.opened_at])<TIME(11,0,0),"Breakfast",IF(TIMEVALUE('order product'[orders.opened_at])<TIME(18,0,0),"Lunch","Dinner"))

 

But still not working

The text is here:

 

Meal = IF('order product'[orders.opened_at]<TIME(11,0,0),"Breakfast",IF('order product'[orders.opened_at]<TIME(18,0,0),"Lunch","Dinner"))

Ok I got it, but There is an error said: 

 

"DAX comparison operations do not support comparing values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values."


@MAAbdullah47 wrote:

Ok I got it, but There is an error said: 

 

"DAX comparison operations do not support comparing values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values."


What is the type of "orders.opened_at", I think it is a TEXT type, that's why you get such error.

Try to convert it to a Date/Time type and apply

Meal = IF(HOUR('order product'[orders.opened_at])<11,"Breakfast",IF(HOUR('order product'[orders.opened_at])<18,"Lunch","Dinner"))

You'll do more conversion work if "orders.opened_at" is not a valid date format text.

Capture.PNG

I got now how to change the text into (Date/Time) but when I try to change the type it gives the following message error:

 

"We can't Automatically convert the column to Date/Time Type", any advice?

 


@MAAbdullah47 wrote:

I got now how to change the text into (Date/Time) but when I try to change the type it gives the following message error:

 

"We can't Automatically convert the column to Date/Time Type", any advice?

 


The error shows there're some text in that column not in a valid date format in your data, would you mind sharing any sample data in your case?

Hold On I'll try to send you the column information only, but here I cannot send it as an attachment, so what I should do?

 


@MAAbdullah47 wrote:

Hold On I'll try to send you the column information only, but here I cannot send it as an attachment, so what I should do?

 


@MAAbdullah47

You can post it as plain text or upload the attachment to a network driver(Onedrive, Google Drive or any) and share the download link.

Can You send me your email to share? 


@MAAbdullah47 wrote:

Can You send me your email to share? 


zhll2046@gmail.com

Did you get my email?

Is there any updates?

Let me know how to convert the column into date time, I found many difficulties without clear instructions for altering the data type, I'll be appreciated if you can help me. 

Please note the column contents in text all of them are correct 100% , I did this checking through R in the following command:

 

if( class( order_product$orders.opened_at ) == "try-error" || is.na( order_product$orders.opened_at ) ) print( "That wasn't correct!" )

 

It didn't give me any warning or errors.

 

So the assumption of text error is not possible, the problem from power bi editor.

Still, It said, "Cannot convert value '' of type Text to type Date."

Thank you but I still don't know where I put this command when I create a new field (conditional column)? 

Greg_Deckler
Super User
Super User

Are you doing this in Power Query (M) code or in the data model and want to use DAX?


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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