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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

Working Days Off DAX Formula

Hello everyone, I hope you're doing well.

I'm having an issue with a calculation I'm trying to make to calculate the days of leave taken by professionals.

Previously, I was helped with the following calculation that answered the question:

 

"If a person has a leave that starts on 01/25 and ends on 02/10, it only brings the 6 days of January and the 10 of February."

 

The calculation was as follows:

 

Days count = 
VAR MaxDate = MAX(Calendario[Fecha])
VAR MinDate = MIN(Calendario[Fecha])

RETURN 
    SUMX
    (
        Licencias,
        DATEDIFF
        ( 
            MAX( Licencias[INICIO], MinDate ),
            MIN( Licencias[FIN], MaxDate),
            DAY
        ) + 1
    )

But I have a problem, since my filter is related to the start of the leave, when I apply the filter to the next month, it no longer counts the January leaves. That is to say, the calculation won't bring the 10 corresponding days of February because it filters the January leaves and doesn't take them into account.

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Anonymous 

 

From what you've described, the issue appears to be that you have a relationship between columns Calendario[Fecha] (Date column of Calendar table) and Licencias[INICIO] (the start date of the leave).

 

As you've described, this means that when a filter is applied to the Calendario table, your measure will only count days of leave if the start date falls within the filtered period.

 

The general solution I would recommend is to use DAX to removethe Date filter for the purpose of the SUMX(...) calculation.

Also, you should an additional filter to ensure only rows of Licencias that intersect the filtered date range are included, otherwise you would get odd results for rows that don't intersect the filtered date range at all.

Also, I personally prefer a simple subtraction rather than DATEDIFF.

 

Something like this:

Days count =
VAR MaxDate =
    MAX ( Calendario[Fecha] )
VAR MinDate =
    MIN ( Calendario[Fecha] )
RETURN
    CALCULATE (
        SUMX (
            Licencias,
            VAR INICIO_Adjusted =
                MAX (
                    Licencias[INICIO],
                    MinDate
                )
            VAR FIN_Adjusted =
                MIN (
                    Licencias[FIN],
                    MaxDate
                )
            RETURN
                FIN_Adjusted - INICIO_Adjusted + 1
        ),
        -- Remove filter on Calendario that would filter Licencias[INICIO]
        ALL ( Calendario ), 
        -- Only rows where INICIO is on/before MaxDate
        Licencias[INICIO] <= MaxDate,
        -- Only rows where FIN is on/after MinDate
        Licencias[FIN] >= MinDate
    )

Does this work for you?

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @Anonymous 

 

From what you've described, the issue appears to be that you have a relationship between columns Calendario[Fecha] (Date column of Calendar table) and Licencias[INICIO] (the start date of the leave).

 

As you've described, this means that when a filter is applied to the Calendario table, your measure will only count days of leave if the start date falls within the filtered period.

 

The general solution I would recommend is to use DAX to removethe Date filter for the purpose of the SUMX(...) calculation.

Also, you should an additional filter to ensure only rows of Licencias that intersect the filtered date range are included, otherwise you would get odd results for rows that don't intersect the filtered date range at all.

Also, I personally prefer a simple subtraction rather than DATEDIFF.

 

Something like this:

Days count =
VAR MaxDate =
    MAX ( Calendario[Fecha] )
VAR MinDate =
    MIN ( Calendario[Fecha] )
RETURN
    CALCULATE (
        SUMX (
            Licencias,
            VAR INICIO_Adjusted =
                MAX (
                    Licencias[INICIO],
                    MinDate
                )
            VAR FIN_Adjusted =
                MIN (
                    Licencias[FIN],
                    MaxDate
                )
            RETURN
                FIN_Adjusted - INICIO_Adjusted + 1
        ),
        -- Remove filter on Calendario that would filter Licencias[INICIO]
        ALL ( Calendario ), 
        -- Only rows where INICIO is on/before MaxDate
        Licencias[INICIO] <= MaxDate,
        -- Only rows where FIN is on/after MinDate
        Licencias[FIN] >= MinDate
    )

Does this work for you?

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn
Anonymous
Not applicable

Hi @OwenAuger 

Thank you very much for helping me with this DAX measure, this correction has resolved the question and now I can make more accurate calculations.

I just had to make one final correction, as the result of the measure only provided a fixed date.

the correction was: 

Days count = 
VAR MaxDate =
    MAX ( Calendario[Fecha] )
VAR MinDate =
    MIN ( Calendario[Fecha] )
RETURN
    CALCULATE (
        SUMX (
            Licencias,
            VAR INICIO_Adjusted =
                MAX (
                    Licencias[INICIO],
                    MinDate
                )
            VAR FIN_Adjusted =
                MIN (
                    Licencias[FIN],
                    MaxDate
                )
            RETURN
                   DATEDIFF(INICIO_Adjusted, FIN_Adjusted, DAY) + 1
-- add DATEDIFF function --  
        ),
        -- Remove filter on Calendario that would filter Licencias[INICIO]
        ALL ( Calendario ), 
        -- Only rows where INICIO is on/before MaxDate
        Licencias[INICIO] <= MaxDate,
        -- Only rows where FIN is on/after MinDate
        Licencias[FIN] >= MinDate
    )

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.