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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
el_sa
New Member

YTD Measure Carries Data Over to Next Year

Hi there, 

 

I am facing an issue with my YTD calculation in Power BI Desktop. I have YTD measures for calculating sales for every year, 2022, 2023, 2024 and the current year. Currently, all previous year measures are carrying the data of the whole year when I apply them.

I have tried to apply filters for the given year inside my calculation, and outside, but it did not help. If you faced a similar issue, would love to learn how you solved it.

Thank you

3 ACCEPTED SOLUTIONS

Hi @el_sa 
In that case, this should work for you:

RollingTotal_2024 = 

VAR _CurrDate = MAX(DateDimension[Date])
VAR _Year = 2024

RETURN

/*
    Find current date and use less than or equal to to get rolling total.
    Then using DatesYTD to only keep evaluation for dates within the same year
    Finally, return only year in context
*/

CALCULATE( SUM(Sales[TypeA]), DateDimension[Date] <= _CurrDate, DATESYTD(DateDimension[Date]), DateDimension[Year] = _Year )



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

Hi,

Try this measure

Measure = CALCULATE(SUM('Sales'[TypeA]),datesytd(DateDimension[Date],"31/12"))

Just ensure that you drag Month and year to your visual from the DateDimension table.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

danextian
Super User
Super User

Hi @el_sa 

It seems you are trying to calculate the YTD value relative to the max date of the current year (shifted back to prior years). Please try this:

YTD Revenue Relative to the Month Selected = 
-- Define a variable to find the latest date in the selected context
VAR _MaxDate =
    CALCULATE (
        LASTNONBLANK ( Dates[Date], [Total Revenue] ), -- Finds the last date where revenue exists
        ALLSELECTED ( Dates ) -- Removes any filters applied to Dates to get the full selection context
    )

-- Extract the year of the latest date found
VAR _MaxYear =
    YEAR ( _MaxDate )

-- Get the year of the current row in the Dates table
VAR _CurrentRowYear =
    MAX ( Dates[Year] ) -- The year corresponding to the current row context

-- Calculate the difference in years between the latest date's year and the current row's year
VAR _Diff = _MaxYear - _CurrentRowYear

-- Adjust the latest date to align it with the current row's year
VAR _MaxYearDate =
    EDATE ( _MaxDate, - _Diff * 12 ) -- Shifts the latest date back by the year difference in months

-- Calculate the year-to-date revenue relative to the adjusted maximum date
VAR Result =
    TOTALYTD ( 
        [Total Revenue], -- Measure to calculate the total revenue
        Dates[Date], -- The date column for YTD calculation
        Dates[Date] <= _MaxYearDate -- Additional filter to include only dates up to the adjusted max date
    )

-- Return the final calculated result
RETURN
    Result

danextian_0-1737181273263.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

7 REPLIES 7
danextian
Super User
Super User

Hi @el_sa 

It seems you are trying to calculate the YTD value relative to the max date of the current year (shifted back to prior years). Please try this:

YTD Revenue Relative to the Month Selected = 
-- Define a variable to find the latest date in the selected context
VAR _MaxDate =
    CALCULATE (
        LASTNONBLANK ( Dates[Date], [Total Revenue] ), -- Finds the last date where revenue exists
        ALLSELECTED ( Dates ) -- Removes any filters applied to Dates to get the full selection context
    )

-- Extract the year of the latest date found
VAR _MaxYear =
    YEAR ( _MaxDate )

-- Get the year of the current row in the Dates table
VAR _CurrentRowYear =
    MAX ( Dates[Year] ) -- The year corresponding to the current row context

-- Calculate the difference in years between the latest date's year and the current row's year
VAR _Diff = _MaxYear - _CurrentRowYear

-- Adjust the latest date to align it with the current row's year
VAR _MaxYearDate =
    EDATE ( _MaxDate, - _Diff * 12 ) -- Shifts the latest date back by the year difference in months

-- Calculate the year-to-date revenue relative to the adjusted maximum date
VAR Result =
    TOTALYTD ( 
        [Total Revenue], -- Measure to calculate the total revenue
        Dates[Date], -- The date column for YTD calculation
        Dates[Date] <= _MaxYearDate -- Additional filter to include only dates up to the adjusted max date
    )

-- Return the final calculated result
RETURN
    Result

danextian_0-1737181273263.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
hnguy71
Super User
Super User

Hi @el_sa 
Can you share your measure and a sample with expected results?



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hello

Here is my current DAX Measure, DateDimension table is a date dimension. Expected output is the number of YTD users in a given year. 

YTD_2024 =
VAR MaxDate = MAX(DateDimension[Date])
RETURN
CALCULATE(
    SUM('Sales'[TypeA]),
    FILTER(
        ALL(DateDimension),
        DateDimension[Date] <= MaxDate &&        
        DateDimension[Date] >= DATE(2024, 1, 1) &&
        DateDimension[Date] <= DATE(2024, 12, 31))

 

Hi,

Try this measure

Measure = CALCULATE(SUM('Sales'[TypeA]),datesytd(DateDimension[Date],"31/12"))

Just ensure that you drag Month and year to your visual from the DateDimension table.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @el_sa 
are you attempting to do a rolling total for the year 2024?



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

That's right

Hi @el_sa 
In that case, this should work for you:

RollingTotal_2024 = 

VAR _CurrDate = MAX(DateDimension[Date])
VAR _Year = 2024

RETURN

/*
    Find current date and use less than or equal to to get rolling total.
    Then using DatesYTD to only keep evaluation for dates within the same year
    Finally, return only year in context
*/

CALCULATE( SUM(Sales[TypeA]), DateDimension[Date] <= _CurrDate, DATESYTD(DateDimension[Date]), DateDimension[Year] = _Year )



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors