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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
DAE
Frequent Visitor

Calcular número de días de diferentes años - HELP!

Hola, tengo una tabla con la siguiente información y he calculado los días totales entre la fecha de inicio y la fecha final.

DAE_1-1714734280213.png

Ahora, necesito contar los días para cada año y que pueda filtrar por el año de los días algo así:

 

DAE_2-1714734357083.png

He intentado hacerlo con TOTALYTD, pero al no tener una sola columna con las fechas, no funciona.

HELP!

 

2 REPLIES 2
v-jialongy-msft
Community Support
Community Support

Hi @DAE 

 

First, make sure you have a date table that contains enough date ranges to cover all your possible start and end dates.

Date Table = CALENDAR (MIN('Data'[StartDate]), MAX('Data'[EndDate]))

 

To make slicing and calculating easier, you need to add the year column to the date table:

Year = YEAR('Date Table'[Date])

 

Next, you need to create a metric that calculates the number of days per year. This metric will calculate how many days there are in each year between the start date and the end date.

Days in Each Year = 
SUMX(
    VALUES('Date Table'[Year]),
    VAR CurrentYearStart = DATE('Date Table'[Year], 1, 1)
    VAR CurrentYearEnd = DATE('Date Table'[Year], 12, 31)
    VAR StartDate = MAX('Data'[StartDate])
    VAR EndDate = MIN('Data'[EndDate])
    VAR OverlapStart = MAX(CurrentYearStart, StartDate)
    VAR OverlapEnd = MIN(CurrentYearEnd, EndDate)
    RETURN
        IF(
            OverlapStart <= OverlapEnd,
            DATEDIFF(OverlapStart, OverlapEnd, DAY) ,
            0
        )
)

 

vjialongymsft_0-1714977964758.png

 

 

Best Regards,

Jayleny

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

Hola @v-jialongy-msft , sigo los pasos indicados, pero no funciona. Aparece el resultado en todos los días, y en la sumatoria total "Days each year" aparecen más días de los correctos. El cálculo debería ser este:

 

DAE_4-1715016537206.png

El problema es que tengo la fecha de inicio y la fecha final en la misma fila, son muchos registros, con diferentes fechas y no puedo hacer una fila por cada tramo de fechas y cada registro, ya que las combinaciones son muchisimas.

 

Envío todos los screenshots, por si me puedes dar cualquier indicación

DAE_2-1715015540770.png

¡Gracias!

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!