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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
evel83
Frequent Visitor

YTD for previous year

I need to make a year to date for the same period last year calculation. I want to present this year and previous year next to each other in a bar chart. The periods are business defined. I cannot use standard time-intelligence functions. I have created the next test measure because my original measure in the bar chart returns the same result for each period in the previous year. 

 

YTD Last Date Last Year =
VAR RemoveFilter =
    FILTER (
        ALL ( Date);
        Date[Year] <= YEAR ( VALUES ( Last Refresh'[Last Refresh] ) ) -1
            && Date[Date] <= MAX ( Date[Date] )
    )
VAR Last Date =
    CALCULATE ( LASTDATE ( Date[Date] ); RemoveFilter )
RETURN
    Last Date Last Year

 

My intention is that the test measure returns the last date of each period for the previous year. It didn't work that way though. It returns for each period the last day of the year (screenshot below). I've also made a measure in which I removed the '-1' in the formula. This measure does return the last date of the period for this year. How can I create the measure in such a way that it returns the last day of the period in the previous year?

 

test.png

 

 

2 REPLIES 2
Greg_Deckler
Super User
Super User

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

I tried several different calculations, but nothing works. I 've given it a lot of time but i do not have the feeling that I really understand how the syntax works. I've applied the syntax in Time Intelligence "The Hard Way" in the next measure and tried to compute a first date, last date and row count on the date table in order to check if the calculation is correct.

 

VAR MAX_Year=

YEAR ( VALUES ( 'Last refresh'[Last refresh] ) )
VAR MAX_Date=
MAX ( Datum[Datum] )
VAR TempTable =
CALCULATETABLE ( Date; ALL ( Date[Date] ); ALL ( Date[Date] ) )
VAR RemoveFilter =
FILTER ( TempTable; Datum[Year] <= MAX_Year && Datum[Date] <= MAX_Date)
RETURN
CALCULATE ( FIRSTDATE ( Datum[Datum] ); RemoveFilter )
 
Results into:
test.png

 

The rowcount suggests that not all rows are counted in the datetable that are before the last date of each period in this or previous year (not really a ytd, but a count of all mutations over all years). What I want to achieve is to:

  1. Filter the date table with dates <= this year (and in the other measure <= previous year)
  2. Apply visual level filter (periods, weeks) (So the max date will only return the max date of a period,week in this year)
  3. Filter the date table to return all dates <= max date for each period,week 
  4. Calculate on facttable filtered on dates based on previous steps 

Perhaps I need to find another approach, or is this possible? Because periods can vary in start- and enddate I cannot use standard time based calculations I guess.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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