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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Ricardo76
Frequent Visitor

Count calendar days per natural year

Hello,
I have a table with many records, including a start date field and an end date field:

IDSTART DATEEND DATE...more fields
15682023-01-012023-01-15 
656052023-01-012023-01-08 

354863

2023-02-012023-02-06 

 

I want to calculate how many days per year are included between the start date and the end date of all records. in this case they would be: 21. 15 days of first row, 0 day of second row (It is already counting in the first row) and 6 days of third row.

 

I hope I explained myself well, thank you very much

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Ricardo76 
This is a crazy solution. In fact I don't expect best performance but it works. Please refer to attached sample file with the proposed solution. 

This solution provides you expected total result (21) as in a card visual but shall not provide correct result at row level.

1.png

Calendar Days = 
VAR String = 
    CONCATENATEX ( 
        'Table',
        CONCATENATEX ( CALENDAR ( 'Table'[START DATE], 'Table'[END DATE] ), [Date], "," ),
        ","
    )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@Date", PATHITEM ( Items, [Value] ) )
VAR T3 = DISTINCT ( T2 )
RETURN
    COUNTROWS ( T3 )

View solution in original post

5 REPLIES 5
tamerj1
Super User
Super User

@Ricardo76 
The following solution is simpler and more efficient.

1.png

Calendar Days 2 = 
COUNTROWS (
    DISTINCT (
        SELECTCOLUMNS (
            GENERATE ( 
                'Table',
                CALENDAR ( 'Table'[START DATE], 'Table'[END DATE] )
            ),
            "@Date",
            [Date]
        )
    )
)
tamerj1
Super User
Super User

Hi @Ricardo76 
This is a crazy solution. In fact I don't expect best performance but it works. Please refer to attached sample file with the proposed solution. 

This solution provides you expected total result (21) as in a card visual but shall not provide correct result at row level.

1.png

Calendar Days = 
VAR String = 
    CONCATENATEX ( 
        'Table',
        CONCATENATEX ( CALENDAR ( 'Table'[START DATE], 'Table'[END DATE] ), [Date], "," ),
        ","
    )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@Date", PATHITEM ( Items, [Value] ) )
VAR T3 = DISTINCT ( T2 )
RETURN
    COUNTROWS ( T3 )

Crazy solution but works!!!
Thank you very much

 

jaweher899
Impactful Individual
Impactful Individual

please try 

Total Days =
SUMX(
'Table',
IF(YEAR( 'Table'[END DATE] ) = YEAR( 'Table'[START DATE] ),
DATEDIFF( 'Table'[START DATE], 'Table'[END DATE], DAY ),
DATEDIFF( 'Table'[START DATE], DATE(YEAR( 'Table'[END DATE] ), 12, 31), DAY ) +
DATEDIFF( DATE(YEAR( 'Table'[END DATE] ), 1, 1), 'Table'[END DATE], DAY )
)
)

Thanks, but the solution doesn't work on my model. Because he is adding the days and not counting. Each year cannot add more than 365 days.

Ricardo76_0-1675953146827.png

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.