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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
javirmerino
Helper III
Helper III

Converting DAX MinX function to Power Query Language

Hi Guys, whats the best way to transform this into #M? Apologies if its been answered somewhere already;

NextWorkingDay = 
minx( 
    FILTER(
        ALL(Dim_Date), Dim_Date[IsWeekday] = TRUE  && 
        Dim_Date[IsHolidayUK] = FALSE && 
        Dim_Date[Date] > SELECTEDVALUE('SysAid Extract'[ReceivedDate]) 
    ),Dim_Date[Date])

 i've read that List.Min would be my best option but cannot figure out how to apply the filters also. Any advise would be appreciated.


Thanks in advance!

A

1 ACCEPTED SOLUTION

Hello @javirmerino 

 

I think this should be all be done in DAX, as your data is already in your datamodel and you have to handel big data volumes.

The first formula you already posted yourself by determine the next working day. There would only be the need to add a time-value. Your second request is about adding hours from another table. As they already connected this daat could be retrieved by a formula like Related(TblSLA[Hours])

 

Hope I could at least help a little bit

 

Jimmy

View solution in original post

8 REPLIES 8
Jimmy801
Community Champion
Community Champion

Hello @javirmerino 

 

a formular like yours are always triggered by a filter or row context and in your case by a specific selectedvalue. So I don't know what's your starting point to do a calculation. However, I've developed some time ago a networkingdays-function for m that accepts a starting, end date and a list with holiday. You could try to apply this 

// fnNetWorkDays2
(DateStart as date, DateEnd as date, optional Holidays as list) =>
let
    Switch = DateStart > DateEnd,
    StartDate = if Switch then DateEnd else DateStart,
    EndDate = if Switch then DateStart else DateEnd,
    HolidayIntern = if Holidays = null then List.Buffer({}) else List.Buffer(Holidays),
    CreateListDates =List.Buffer( List.Dates
    (
        StartDate,
        Duration.TotalDays
        (
            EndDate - StartDate
        )
        +1,
        #duration(1,0,0,0)
    )),

    ExcludeWeekend = List.Select
    (
        CreateListDates,
        each 
        Date.DayOfWeek
        (
            _,
            Day.Monday
        )
        <5
    ),

    ExcludeHoliday = 
    if Holidays = null then ExcludeWeekend else List.Difference
    (
        ExcludeWeekend,
        HolidayIntern
    ),
    Result = List.Count
    (
        ExcludeHoliday
    ),

    FinalResult = try (if Switch then -1 else 1) * (Result) otherwise null
    
in
    FinalResult

 

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Thanks, @Jimmy801.

Would you mind talking me through what each part of the query is doing, please? i can sort of work out some of it but i've never seen or used the SWITCH function, for example. 

 

And just for clarity, i'm trying to determine NEXT working day rather than NET working days as your formula calculates. ie. If the ReceivedDate is a Saturday then the next working day would be the following Monday (subject to Bank Holidays).

Hello @javirmerino 

 

could you then please describe how is made your database in Power Query? What databases you have and how you need to calculate it

 

Jimmy

Apologies, @Jimmy801, i thought i replied to this hours ago and had been waiting for a response. Thank you for your patience!

 

The database is constructed as below, with a direct query to a SysAid SQL Server, linked to a number of dimension tables;

 

image.png

 

i'm trying to calculate a DueDate, based on the ReceivedDate and the Urgency fields in the extract.

 

First i need to calculate the NextWorkingDay; if the request is raised outside of office hours then the calculated StartTime should be 9am the next working day. Ie. if a request is raised on Saturday 1st February then the starting time and date should be Monday 3rd February at 09:00am.

 

Secondly, i need to determine the hours from the SLA table to add to the calculated start time and date to give a DueDate. if the Urgency is "Urgent" then there is an requirement to complete within 4 working hours. Again, the SLA is based on working hours so an Urgent incident raised on Friday 31st January at 16:59pm would not be due until Monday 3rd February at 12:59pm.

 

Finally, i need a column to determine if the request was completed within the agreed SLA. if the CompletedDate is greater then the DueDate then Completed Late, else its Completed On Time.

 

 

Hello @javirmerino 

 

you have asked to a solution in Power Query, but the data is already in your datamodel and there is no way to read your datamodel from Power Query. So I don't know how i could help you with Power Query in this case.

So I would see your request in DAX with a new column for SysAid Extract, or what is the goal to do this in Power Query?

 

Jimmy

Thank @Jimmy801. I have amended my response slightly to prevent further confusion. I have instead amended my desired calculated column to DueDate to prevent us getting mixed up.

 

The RequiredCompletionDate column within my dataset is a null field. It requires the end user to populate this with a date normally so is null in 99% of records. see extract below;

 

IDTypeSub TypeStatusUrgencyRequired Completion DateReceivedReceivedDateReceivedTimeClosedClosedDateClosedTime
67674MaintenanceVisit AmendmentClosedUrgentnull09/09/2019 15:05:5209/09/201915:05:522019-09-10 10:59:4810/09/201910:59:48
67847MaintenanceField Team ChangeClosedUrgentnull11/09/2019 15:00:5611/09/201915:00:562019-09-11 15:54:2211/09/201915:54:22
68287MaintenanceSite Contract ActivateClosedVery Highnull19/09/2019 13:52:2719/09/201913:52:272019-09-20 13:18:0020/09/201913:18:00
68448MaintenanceField Team ChangeClosedUrgentnull23/09/2019 14:52:1023/09/201914:52:102019-09-24 09:34:1224/09/201909:34:12

 

My solution is to automate this DueDate based on parameters that are already provided on every incident. I'd like it calculated within Power Query (a preference not a requirement) but if the solution can be done in DAX i'd happily use that, too.

 

Thnaks for sticking with this, Jimmy. I appreciate that new users can be frustrating!

Hello @javirmerino 

 

I think this should be all be done in DAX, as your data is already in your datamodel and you have to handel big data volumes.

The first formula you already posted yourself by determine the next working day. There would only be the need to add a time-value. Your second request is about adding hours from another table. As they already connected this daat could be retrieved by a formula like Related(TblSLA[Hours])

 

Hope I could at least help a little bit

 

Jimmy

Hi @Jimmy801, thanks for your patience here. i really appreciate it.

 

I have a direct query to a SQL server named as SysAid Extract with a many to one relationship to a date dimension table and an SLA dimension table.

 

image.png

 

i'm trying to calculate a Due Date for each sysaid incident based on their urgency.

 

The first calculated column i need is to determine the [NextWorkingDay] based on the [ReceivedDate] within the extract. If a request is received out of normal office hours then the [StartDate] should be the next working day, excluding holidays and weekends. If the request is received within normal office hours then i can just use the provided [ReceivedDate]. 


The next calculation should use this calculated [StartDate], and add x working hours based on the urgency and the hours in TblKPI. For example, if an urgent request was received at 16:59 on Friday 31st January with the SLA for Urgent queries being completion within 4 hours. The [NextWorkingDay] would be Monday 3rd February, so the [DueDate] should be Monday 3rd February and the [DueTime] should be 12:59.

 

I'm assuming that this should all be calculated in Power Query (i read somewhere that "if you can do it in DAX or M, then do it in M" is a good working practice) but if its easier and quicker to calculate these as measures in DAX then i'm happy to use that process instead. You're the expert here, Jimmy!

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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