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
Applicable88
Impactful Individual
Impactful Individual

Return if its the NEXT production workday

Hello, 

I have following problem. When the 1st shift is finished after around 15:00 o'clock , we already doing the orders of next day.Before that the visualisation will not consider them, only the orders older or equal today.  So my m-query is stating with a statement in simple terms (Please disregard the DatetimeZone.Switchzone function, its just because the powerbi server uses UTC time:

 

 

if Time.Hour(DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),2,0))>=15 
and 
Time.Hour(DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),2,0))<=23 
(if [OutbounDate]<=Date.From(DateTime.LocalNow()) or [OutbounDate]=Date.AddDays(Date.From(DateTime.FixedLocalNow()), 1)) then "To Do" else "Backlog"

 

 

So from 3PM on until 11PM orders from older than today and also tomorrow are considered. 

The problem with this lies that the AddDays function is fixed. But what when tomorrow is public holidays or vacation a several day and the company is closed? Then we would consider the orders which have a outbounddate of the first working day after the vacation, public holidays or even weekend. 

I just found a Mastercalendar with "ProductiveWorkdays" as a flag. This shows me which weekdays are actually working days=1 or 

off days=0.

In this example for example you see that Monday and Tuesday with the red 0 are off days. And if today would be the Friday before the long weekend, how to tell the formula to return only the ordernumber from of the very first day with ProductiveWorkday =1? 

But only the very first one, so in this example it would be only the order with the date 19.05.2021, the Wednesday.

OutboundDateDayNameProductiveWorkdayOrdernumber
11.05.2021Tuesday11234
12.05.2021Wednesday11235
13.05.2021Thursday11236
14.05.2021Friday11237
15.05.2021Saturaday01238
16.05.2021Sunday01239
17.05.2021Monday0null
18.05.2021Tuesday0null
19.05.2021Wednesday11242
20.05.2021Thursday11243
21.05.2021Friday11244
22.05.2021Saturaday01245
23.05.2021Sunday01246
24.05.2021Monday11247
25.05.2021Tuesday11248
26.05.2021Wednesday11249

Thank you in advance.

Best.

1 ACCEPTED SOLUTION
watkinnc
Super User
Super User

The key is to obtain the date of the next legitimate workday. I would add a blank query to use as a parameter:

NextDay = List.Min(Table.SelectRows(TableName, each [ProductiveWorkday] = 1 and [OutBoundDate] > Date.From(DateTime.LocalNow()))[OutBoundDate])

This will give you the first ProductiveWorkday Date after today. 
Now you can add a step to your MainTable:

if Time.Hour(DateTime.LocalNow())>= 15 and Time.Hour(DateTime.LocalNow())<= 23 then Table.SelectRows(MainTable, each [OutBoundDate] >= Date.From(DateTime.LocalNow()) or [OutBoundDate] = NextDay) else TableSelectRows(MainTable, each OutBoundDate <= Date.From(DateTime.LocalNow()))

 

I would also make a Today query (=DateTime.LocalNow()) so that I didn't have to keep typing Date.From(DateTime.LocalNow()), just type Today instead.

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

View solution in original post

8 REPLIES 8
watkinnc
Super User
Super User

The key is to obtain the date of the next legitimate workday. I would add a blank query to use as a parameter:

NextDay = List.Min(Table.SelectRows(TableName, each [ProductiveWorkday] = 1 and [OutBoundDate] > Date.From(DateTime.LocalNow()))[OutBoundDate])

This will give you the first ProductiveWorkday Date after today. 
Now you can add a step to your MainTable:

if Time.Hour(DateTime.LocalNow())>= 15 and Time.Hour(DateTime.LocalNow())<= 23 then Table.SelectRows(MainTable, each [OutBoundDate] >= Date.From(DateTime.LocalNow()) or [OutBoundDate] = NextDay) else TableSelectRows(MainTable, each OutBoundDate <= Date.From(DateTime.LocalNow()))

 

I would also make a Today query (=DateTime.LocalNow()) so that I didn't have to keep typing Date.From(DateTime.LocalNow()), just type Today instead.

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Wow @watkinnc , I think that should be the solution :). One question: Are you referring to the same table when you wrote "TableName" on the top query and "MainTable" in the second query?

Thank you very much.

Best

Yes, sorry for the inconsistency. I'm usually on my iPhone here 🙂


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

@watkinnc 😊, thank you very much!

ImkeF
Super User
Super User

Hi @Applicable88 ,

if my understanding is correct, the following should work:
- Add a column where you check if "ProductiveWorkday" is null

- if so, return null, else return "Ordernumber"

- check new column and "Fill up"

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Applicable88
Impactful Individual
Impactful Individual

Hello @ImkeF ,

thanks for the reply. But thats not what I want to return. I might explained too complicated.Your example would return every productive working day. But I want from todays standpoint ONLY the ordernumber returned from the next outbounddate which is also a productive working day =1 . 

 

According to the table above, if today would be Tuesday May 11th, it would return me all ordernumbers with a outbound date until today AND of tomorrow. If its Wednesday May 12th, it should do the same including the next day of May 12th which is May 13th. So its always the ordernumbers older or today and of the next day. For Friday I have a exception build in for a offeset of three days because of the weekend.

As you can see my function is very rigid and only consider next day or 3 days for weekends offset.

 

So now comes the problem. What if the next day or the Monday after the weekend is not a productive working day (indicated as 0)? 

How can I utilize a dynamic if statement which is saying:

if orderoutbounddate is older or equal today and also the ordernumber of just the VERY NEXT orderoutbounddate which is a productive working day. 

 

Hi @Applicable88 ,

sorry, but this is too complicated for me to understand without a sample table with specified desired results.

It might be a good start to create some intermediate columns as well that display results of the various conditions.

That would make troubleshooting much easier and also help other helpers in this forum who might want to pick this up.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Applicable88
Impactful Individual
Impactful Individual

Hello @ImkeF,

 

I try to simplified my problem and also provided a excel with above data and the pbix file where I already did the calculated column which only lacks one function that I hope to find.

https://drive.google.com/drive/folders/1Jox5PfTJCfBP9x9hnQ6c8UBch_h7YIBq?usp=sharing

 

Given today is already May 9th, means Sunday, the next day would be a productive workday. 

According to this calculated column with this if statement:

 

 

if ([OutboundDate]=Date.From(DateTime.LocalNow()) or [OutboundDate]=Date.AddDays(Date.From (DateTime.FixedLocalNow()), 1)) and [ProductiveWorkday]=1 then "Orders" else "Fail"

 

 

I get following outcome:

Applicable88_0-1620518861287.png

Thats because tomorrow is the next day and also a productive day. But my function is rigid, since it can only lookup for the next day. But what if there are off days/weekends/ public holidays between today and the next working day?

 

If tomorrow would be a public holiday or vacation, my function wouldn't work. As you can see for example Monday the 17th and 18th of May are both public holiday indicated with 0 as not productive.  If thats the case it should return the first day after weekend/public holiday/ vacation which is again productive =1. In that case it would be wednesday 19th. 

Example is here: 

Given today is Friday May 14th return today and the next working day which is May 19th with "Orders" instead of "Fail" .Since all days in between are non productive days. 

 

Applicable88_1-1620519544246.png

 

I hope I explained better. 

 

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.

Top Solution Authors
Top Kudoed Authors