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 September 15. Request your voucher.

Reply
Arial36
Frequent Visitor

Problems With Function in M

Hello community! It is a pleasure to be part of this group! I am going crazy with creating a function and I need a little help since
I hardly know the M language.I have two fields, one with dates that come to me from the database and another with holidays, which
I need is to create a loop with a counter initialized to zero that works on the dates field and compares the dates, if it does not
find them, it should put a 0. When it finds a match it should restart the loop and if the later date is not a holiday ( do not match),
put a one, and if it also matches, restart the loop until it exits and adds the number of consecutive holidays (Rarely more than 2).
Can this be done? I'm going to have problems with my boss but ... hahahaha Thank you very much!

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

I think your English is very good.  You can keep the column you have now and use it in a new column using this approach.  Adapt for your table/column names.  You could consolidate the two expressions, but hopefully this works for you (and you'll have both columns for different types of calculations).

 

Days Til Next Workday =
VAR vThisDate = 'Date'[Date]
VAR vNextWorkday =
    CALCULATE (
        MIN ( 'Date'[Date] ),
        'Date'[Date] > vThisDate,
        'Date'[IsWorkingDay] = 0
    )
RETURN
    DATEDIFF (
        vThisDate,
        vNextWorkday,
        DAY
    )

 

Pat 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

I think your English is very good.  You can keep the column you have now and use it in a new column using this approach.  Adapt for your table/column names.  You could consolidate the two expressions, but hopefully this works for you (and you'll have both columns for different types of calculations).

 

Days Til Next Workday =
VAR vThisDate = 'Date'[Date]
VAR vNextWorkday =
    CALCULATE (
        MIN ( 'Date'[Date] ),
        'Date'[Date] > vThisDate,
        'Date'[IsWorkingDay] = 0
    )
RETURN
    DATEDIFF (
        vThisDate,
        vNextWorkday,
        DAY
    )

 

Pat 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mahoneypat
Microsoft Employee
Microsoft Employee

I expect this can be done, but it I'm not totally clear on what you need.  Please post some sample date values (in a copy/paste format, not an image) and indicate what the correct output would be.

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thank you very much for answering! Obviously I explained badly (And my English is surely very bad too), the problem is the following: I have two tables with fields. In one I have the dates of the whole year, and in another the dates of the holidays. What I need is to identify the holidays and weekends and add them in a loop. To make it better understood, I work in a logistics company, so when a package arrives on a Friday (For example) and assuming that Monday is a holiday, I need a line that marks a 3 for me, since I would have three non-working days since the package arrived. In the photo you can see that I made a function that detects holidays and weekends, but what I really need (as the annotations in the image show) is for the function to be included in a loop, so that it can iterate and accumulate the holidays and weekends that I have each time a package arrives. Hopefully my problem has been understood a little better! From already thank you very much !WhatsApp Image 2021-02-05 at 01.59.02.jpeg

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors