cancel
Showing results for
Did you mean:

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Anonymous
Not applicable

## YTD Comparisons of Current, Last Year and 2 Years Prior with Irregular Calendar Years

I've been trying to get YTD measures to compare the current YTD against last year and 2 years ago YTDs.  I was able to get the current YTD, but have been having trouble with the other two.  2016 is made up of 14 months and goes from Nov 2015 through Dec 2016 and 2015 starts on Nov 2014 through Oct 2015.

I have a table with account data that has dates and sales.  I have also created a calendar table with dates, months, years etc. that's linked to the sales table. I have the following measures:

Total Sales = SUM(Sales[Sales])

CY YTD = TOTALYYTD([Total Sales], Sales[Date])

When I try to get LY YTD, I can't seem to get the amounts from Nov and Dec 2015 to be included.  How can I get the time intelligence functions to adjust to these particular years?  Thanks for your time and help.

1 ACCEPTED SOLUTION
Community Champion

Hi @Anonymous,

The approach I had taken is as under.

1.  I changed your Dates table formation as  -  Dates = CALENDAR(MIN(Sales[Date]), MAX(Sales[Date]))

This is the best way to create a calendar table based on the minimum and maximum date of the fact tables.

2.  Created a column called MonthNumber = Month(Dates[Date])

3.  Set the MonthName to sorted by column MonthNumber.

4.  Created a MonthSequentialNumber column

MonthSequentialNumber = ('Dates'[Year] - MIN( 'Dates'[Year] )) * 12 + 'Dates'[MonthNumber]

This generates a sequential number for each month and incremented by 1 for every month in the Dates table.

In the sample file this number ranges from 11 to 45.

To go back 1 year just need to subtract 12 from this. Similarly to go back 2 years subtract by 24 and so on.

5.  Created a measure called

YTD1YearB4 = Calculate([Total Sales],Filter(ALL(Dates), Dates[Year] = Max(Dates[Year]) - 1  && Dates

[MonthSequentialNumber] <= Max(Dates[MonthSequentialNumber]) - 12) )

6. Created a measure called

YTD2YearB4 = Calculate([Total Sales],Filter(ALL(Dates), Dates[Year] = Max(Dates[Year]) - 2  && Dates

[MonthSequentialNumber] <= Max(Dates[MonthSequentialNumber]) - 24) )

Depending on the Year selected it will report the 1year before and 2 year before total sales.

I have  uploaded the file in one drive. and the link is

https://1drv.ms/u/s!ApP3mBZyGaHfzyeQc9QxgrROjsFD

If this works for you please accept this as a solution and also give KUDOS.

Cheers

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
14 REPLIES 14
Frequent Visitor

Hi CheenuSing,

Thank you.  It worked except I keep running into the same issue I have when I am using the formula to get last year numbers using the sameperiodlastyear dax formula.  I think it ihas something to do with 53wks.  Not sure how to work around that issue in your formula.  This is how I have to work around the sameperiodlast year issue:    Year behind Sales = CALCULATE(SUM([Total Sales Amount]),dateadd('Dates'[Date],-364,Day))

In your the formula YTD 3/12/22:  last year is using 12/26/21-3/5/2022 vs 1/2/2022-3/12/2022.

Any help is appreciated!!

Helper V

Hi @Anonymous,

did you get any solution to this??? If yes then kindly share.

Regards,

Hemant

Super User

Hi @Anonymous,

Try to do something like this for previous year:

```LY YTD =
TOTALYTD (
[Total Sales],
DATESBETWEEN ( Sales[Date], DATE ( 2015, 11, 01 ), DATE ( 2016, 12, 31 ) )
)```

You need to adjust the date part to be affect by your slicers but without further information regarding the tipe of slice and dice you wan to do I can't give you a better explanation.

Regards,

MFelix

Regards

Miguel Félix

Proud to be a Super User!

Check out my blog: Power BI em Português

Anonymous
Not applicable

@MFelix Thanks for the response.

I'm looking to compare last year and 2 years prior to the current year's YTD.   So for 2016, the two extra months (Nov and Dec 2015) plus all the months up to September.  I tried your suggested measure, but I wasn't able to get it to work correctly.  After more searching and trial and error, I was able to get the correct YTD amounts for 2016 with the following measure:

```LY YTD =
VAR MaxDate =
CALCULATE ( MAX ( Sales[Date] ), ALL ( Sales ) )
RETURN
CALCULATE (
[LY Total],
SAMEPERIODLASTYEAR (
INTERSECT (
VALUES ( Dates[Date] ),
DATESBETWEEN ( Dates[Date].[Date], BLANK (), MaxDate ))))```

Where [LY Total] is the total sales amount for 2016.  Now, I'm having trouble with 2 years prior YTD.  I'm just getting the total amounts for the entire year 2015 instead of the YTD.   I haven't been successful in getting the right dates filter.  I'd like it to look something like this

Community Support

Hi @Anonymous,

You can try to create a measure like below:

If it doesn't work, please share the pbix file with dummy data for us to test.

Best Regards,
Qiuyun Yu

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Helper I

Just wanted to say thanks for this formula
I was struggling to get a previous YTD and this helped me a treat

Community Champion

Hi @Anonymous

Couple of questions.

1. Just like the measure [LY Total] have you calculated any measure for 2 years total.

2. Do you have a calendar table and linked with your Sales Fact table.

3. Going forward what is your financial year. Will it be from Nov to Oct in the future.

I feel you will have to write specific YTD totals for the years 2016 and 2015 as they do not fall under the same financial period.

If you can share the pbix please load it on one drive and provide the link here to formulate a solution.

Cheers

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
Anonymous
Not applicable

Thanks for your reply.  Yes, I have created a measure for the total from 2 years ago and created a calendar table that's linked to the Sales table.  This year's financial year will match the calendar year (Jan - Dec).

Here's a link to a test pbix

Community Champion

Hi @Anonymous,

A quick question.  Your financial years are different for each year. Nov - Oct 2015 (12 Months),  Nov-Dec 16 (14 Months) and Jan-Dec 2017.

For Profit & Loss, Balance Sheet one may have to consolidate the 12 monhts, 14 months and so on for different financial years.

For Sales Reporting , when the current year is reproted for Jan - Sep 2017 - for 9 months. It would be wrong to compare Nov2015 to Sep 2016 which would be 11 months.  One should compare the same number of months in the previous year Jan-Sep 16.

Likewise for Jan-Sep 2015.  Otherwise comparisons reflect different number of months.

Cheers

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
Anonymous
Not applicable

@CheenuSing

How would you go about cutting down the months and getting the ytd numbers? Also,  could you explain how this

```LY YTD =
VAR MaxDate =
CALCULATE ( MAX ( Sales[Date] ), ALL ( Sales ) )
RETURN
CALCULATE (
[LY Total],
SAMEPERIODLASTYEAR (
INTERSECT (
VALUES ( Dates[Date] ),
DATESBETWEEN ( Dates[Date].[Date], BLANK (), MaxDate ))))```

works with the 14 month year and is it possible to manipulate this to work with 2 years back?

Community Champion

Hi @Anonymous,

The approach I had taken is as under.

1.  I changed your Dates table formation as  -  Dates = CALENDAR(MIN(Sales[Date]), MAX(Sales[Date]))

This is the best way to create a calendar table based on the minimum and maximum date of the fact tables.

2.  Created a column called MonthNumber = Month(Dates[Date])

3.  Set the MonthName to sorted by column MonthNumber.

4.  Created a MonthSequentialNumber column

MonthSequentialNumber = ('Dates'[Year] - MIN( 'Dates'[Year] )) * 12 + 'Dates'[MonthNumber]

This generates a sequential number for each month and incremented by 1 for every month in the Dates table.

In the sample file this number ranges from 11 to 45.

To go back 1 year just need to subtract 12 from this. Similarly to go back 2 years subtract by 24 and so on.

5.  Created a measure called

YTD1YearB4 = Calculate([Total Sales],Filter(ALL(Dates), Dates[Year] = Max(Dates[Year]) - 1  && Dates

[MonthSequentialNumber] <= Max(Dates[MonthSequentialNumber]) - 12) )

6. Created a measure called

YTD2YearB4 = Calculate([Total Sales],Filter(ALL(Dates), Dates[Year] = Max(Dates[Year]) - 2  && Dates

[MonthSequentialNumber] <= Max(Dates[MonthSequentialNumber]) - 24) )

Depending on the Year selected it will report the 1year before and 2 year before total sales.

I have  uploaded the file in one drive. and the link is

https://1drv.ms/u/s!ApP3mBZyGaHfzyeQc9QxgrROjsFD

If this works for you please accept this as a solution and also give KUDOS.

Cheers

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
Frequent Visitor

Hi CheenuSing,

I searched for hours on how to figure out how to do YTD LY, YTDL2, L3 years ago and used this calculation and it is great for month end.  But how do I get it to work for a YTD in the middle of the month?  YTD 2023 works summing up through 3/11/23 but YTD 2022, YTD 2021, YTD 2020 and YTD 2019 sums up all the weeks through the end of March even though we are only through the middle of March.  I would like YTD 2022 to be as of 3/12/22, YTD 2021 as of 3/10/21, etc.

BUT Amazing formula!!!

Community Champion

Hi @krider71 ,

I have created new measures to take care of your reequirements.

The code is as under

VAR MaxDate = MAX ('Dates'[Date])
VAR MAXYEAR = YEAR(MaxDate)
VAR MAXMONTH = MONTH(MAxDate)
VAR MAXDAY = DAY(MaxDate)
VAR PrevYear = MAXYEAR - 1
VAR LeapYear = MOD(PrevYear ,4)
VAR ADjMAxDay = IF (LeapYear = 0 && MAXDAY = 28,29, MAXDAY)
VAR StartDateYear = DATE(PrevYear, 1,1)
Return
Calculate([Total Sales], DATESBETWEEN('Dates'[Date],StartDateYear,EndDate))

For 2 years before
VAR MaxDate = MAX ('Dates'[Date])
VAR MAXYEAR = YEAR(MaxDate)
VAR MAXMONTH = MONTH(MAxDate)
VAR MAXDAY = DAY(MaxDate)
VAR PrevYear = MAXYEAR - 2
VAR LeapYear = MOD(PrevYear ,4)
VAR ADjMAxDay = IF (LeapYear = 0 && MAXDAY = 28,29, MAXDAY)
VAR StartDateYear = DATE(PrevYear, 1,1)
Return
Calculate([Total Sales], DATESBETWEEN('Dates'[Date],StartDateYear,EndDate))

The code also takes care of February for leap year. If the previous year is leap year it will point to 29 Feb when the max date for current non leap year is 28 February.
You can decide how you want to treat this.

The data is only upto Sep 11, 2017.

Cheers
CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!
Frequent Visitor

Thank you!  I thought I responded and with some additional questions but not sure where that post went?  Anyways, That pretty much worked  - you are amazing -  the issue I keep having is when years start and end.  For Years before and same issue I have in other Powerbi sametimeperiod last year it is using YTD 12/26/21-3/5/22 to compare to 202

YTD 1/1/23-3/11/23.   VS I want it to be YTD 1/2/22-3/12/22 to compare to the 2023

Example:

This is what powerbi is showing as my week end dates and Week # of Year.  It has week 10 as 3/11/23 and then 3/5/22. I want week 10 to be 3/12/22.  Its that week 53 thing and they only way I can fix for comparing TY vs LY is not use sametimeperiod but a formula like:

 Week End Date Week of Year 3/11/2023 10 3/4/2023 9 2/25/2023 8 2/18/2023 7 2/11/2023 6 2/4/2023 5 1/28/2023 4 1/21/2023 3 1/14/2023 2 1/7/2023 1 12/31/2022 53 3/12/2022 11 3/5/2022 10 2/26/2022 9 2/19/2022 8 2/12/2022 7 2/5/2022 6 1/29/2022 5 1/22/2022 4 1/15/2022 3 1/8/2022 2 1/1/2022 1 12/25/2021 52

Thank you for any help!

Announcements

#### Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

#### Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

#### Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

#### The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors