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 August 31st. Request your voucher.

Reply
Margreet
Helper II
Helper II

Count days of a contract in a period

Hello,

 

Hope someone knows the answer... My formula to conunt the numer of days in a year a contract is active don't work. I use the following formule: 

_TotalDays of a contract in period =

VAR StartPeriode = MIN(Datumtabel[Date])

VAR EindPeriode  = MAX(Datumtabel[Date])

 

RETURN

SUMX(

    FILTER(

        F_Contracten,

        -- Alleen toewijzingen die overlappen met de periode

        NOT(ISBLANK(related(D_contracten[Ingangsdatum]))) &&

        NOT(ISBLANK(related(D_contracten[einddatum]))) &&

        related(D_contracten[Ingangsdatum]) <= EindPeriode &&

        RELATED(D_contracten[einddatum]) >= StartPeriode

    ),

    DATEDIFF(

        MAX(RELATED(D_contracten[Ingangsdatum]), StartPeriode),

        MIN(related(D_contracten[einddatum]), EindPeriode),

        DAY

    ) + 1


with this as result:

Margreet_1-1752600735113.png

This is not what i want to see i want in each year the contract is active see the the days belong to dat year: 

for example: key1:
in 2022: 1-8-2022 - 31-12-2022: 153 days
in 2023: 1-1-2023 - 31-12-2023: 365 days
in 2024:  1-1-2024 - 1-10-2024: 274 days

 

Margreet_0-1752600709216.png

 

Thanks a lot for reaction! 

 

Margreet_2-1752601018917.png

 

1 ACCEPTED SOLUTION

Hi @Margreet as per the table that you have shared, create a disconnected year table like this 

 

YearTable = DISTINCT(SELECTCOLUMNS(Datumtabel, "Year", YEAR([Date])))
 
and then create the total active days measure like this
 
Total Days =
VAR SelectedYear = SELECTEDVALUE(YearTable[Year])

RETURN
IF (
    NOT ISBLANK(SelectedYear),
    VAR YearStart = DATE(SelectedYear, 1, 1)
    VAR YearEnd = DATE(SelectedYear, 12, 31)

    RETURN
        SUMX (
            D_contracten,
            VAR ContractStart = D_contracten[Ingangsdatum]
            VAR ContractEnd = D_contracten[einddatum]

            VAR OverlapStart = MAX ( ContractStart, YearStart )
            VAR OverlapEnd = MIN ( ContractEnd, YearEnd )

            RETURN
                IF (
                    OverlapStart <= OverlapEnd,
                    DATEDIFF ( OverlapStart, OverlapEnd, DAY ) + 1,
                    0
                )
        ),
   
   
    SUMX (
        D_contracten,
        VAR ContractStart = D_contracten[Ingangsdatum]
        VAR ContractEnd = D_contracten[einddatum]
        RETURN
            IF (
                NOT ISBLANK(ContractStart) && NOT ISBLANK(ContractEnd),
                DATEDIFF(ContractStart, ContractEnd, DAY) + 1,
                0
            )
    )
)
 
 
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

View solution in original post

6 REPLIES 6
Margreet
Helper II
Helper II

hi @techies super! Thanks a lot, it worked in my pbix! 

Margreet
Helper II
Helper II

hi @techies , Yeah i am looking for the formula for that outcome... How did you do that?

 

Hi @Margreet as per the table that you have shared, create a disconnected year table like this 

 

YearTable = DISTINCT(SELECTCOLUMNS(Datumtabel, "Year", YEAR([Date])))
 
and then create the total active days measure like this
 
Total Days =
VAR SelectedYear = SELECTEDVALUE(YearTable[Year])

RETURN
IF (
    NOT ISBLANK(SelectedYear),
    VAR YearStart = DATE(SelectedYear, 1, 1)
    VAR YearEnd = DATE(SelectedYear, 12, 31)

    RETURN
        SUMX (
            D_contracten,
            VAR ContractStart = D_contracten[Ingangsdatum]
            VAR ContractEnd = D_contracten[einddatum]

            VAR OverlapStart = MAX ( ContractStart, YearStart )
            VAR OverlapEnd = MIN ( ContractEnd, YearEnd )

            RETURN
                IF (
                    OverlapStart <= OverlapEnd,
                    DATEDIFF ( OverlapStart, OverlapEnd, DAY ) + 1,
                    0
                )
        ),
   
   
    SUMX (
        D_contracten,
        VAR ContractStart = D_contracten[Ingangsdatum]
        VAR ContractEnd = D_contracten[einddatum]
        RETURN
            IF (
                NOT ISBLANK(ContractStart) && NOT ISBLANK(ContractEnd),
                DATEDIFF(ContractStart, ContractEnd, DAY) + 1,
                0
            )
    )
)
 
 
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
techies
Super User
Super User

Hi @Margreet is this the year-wise breakdown you are looking for?

 

techies_0-1752604977946.png

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
Margreet
Helper II
Helper II

Thank! 

KeyProductkeyIngangsdatumEinddatum
1101-8-20221-10-2024
2111-12-202231-12-2022
381-12-20221-6-2023
4101-3-20231-8-2023
5111-3-20231-3-2024
6221-3-20231-3-2025
FBergamaschi
Solution Sage
Solution Sage

Please include, in a usable format, not an image, a small set of rows for each of the tables involved in your request and show the data model in a picture, so that we can import the tables in Power BI and reproduce the data model. The subset of rows you provide, even is just a subset of the original tables, must cover your issue or question completely. Do not include sensitive information and do not include anything that is unrelated to the issue or question. Please show the expected outcome based on the sample data you provided and make sure, in case you show a Power BI visual, to clarify the columns used in the grouping sections of the visual.

 

Need help uploading data? click here

 

Want faster answers? click here

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.