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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
gzai
Helper I
Helper I

DAX sum last year but some data doesn't have data in the last year

I have spend data displayed in a matrix like this :

Current Year :

Name20212022202320242025
SUZUKI59663365865418
HYUNDAI261773765167159
TOYOTA165843885229150
DAIHATSU726691602179802
MITSUBISHI28413725136272
WULING83853827118587
HONDA99636430978778
CITROEN  71235587
BYD   568117
CHERY677776900783109
OTHER815136858909131


and I want to show each name prev year but it can't be displayed because there are some names that don't have data in the previous year, how do I fix the measurement?

Last Year Spend =
VAR _max = MAXX(Raw, Raw[Date])
RETURN 
    CALCULATE(
        SUM(Raw[Spend]),
        DATESYTD(dateadd(Raw[Date],-1,Year)),
        Raw[Date] <= _max
    )
1 ACCEPTED SOLUTION
v-agajavelly
Community Support
Community Support

Hi @gzai 

Thank you for reaching out to Microsoft Fabric Community Forum.

The issue where some names don't have data in the previous year, you need a DAX measure that gracefully handles missing prior year data while still returning blank (or 0 if you prefer) rather than failing or skipping rows.

As for my experience your current measure has some issues.

You're using DATESYTD on a DATEADD, which assumes a full calendar, if previous year values are missing, DATEADD returns nothing, you're referencing Raw[Date] <= _max directly, which is unnecessary and might not work well in matrix context, you're not preserving context correctly for Name.

Correct DAX Last Year Spend, assuming you have a proper date table (DimDate) with a relationship to Raw[Date], try below DAX:

Last Year Spend = CALCULATE(SUM(Raw[Spend]),SAMEPERIODLASTYEAR(DimDate[Date]))

This measure returns the spend for the same period in the previous year. It works even if some names have no data in prior year, they’ll return blank (which is what Power BI shows for missing values) and also works perfectly in matrix visuals.

Try to Show 0 Instead of Blank, If you want to show 0 instead of blank when data is missing. Use below dax.

Last Year Spend = VAR SpendLastYear =  CALCULATE(SUM(Raw[Spend]), SAMEPERIODLASTYEAR(DimDate[Date]))
RETURN
COALESCE (SpendLastYear, 0)

Use DATEADD if No Time Intelligence Table, If you're not using a date table, then you can use below DAX.

Last Year Spend = CALCULATE (SUM(Raw [Spend]),DATEADD(Raw[Date], -1, YEAR))

But this only works, if Raw[Date] is at daily granularity when You’re showing matrix by Year (e.g., from YEAR(Raw[Date]))

Please use a calendar/date table (best practice for all time-based DAX) and Ensure that Raw[Date] has a relationship to the DimDate[Date] column. Use SAMEPERIODLASTYEAR or DATEADD depending on your model.

If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.

Regards,
Akhil.

View solution in original post

3 REPLIES 3
gzai
Helper I
Helper I

Hi @v-agajavelly ,

Thank you for the solution. 
I have used the formula : 

Last Year Spend = VAR SpendLastYear =  CALCULATE(SUM(Raw[Spend]), SAMEPERIODLASTYEAR(DimDate[Date]))
RETURN
COALESCE (SpendLastYear, 0)

 

Thanks.

Regards,

v-agajavelly
Community Support
Community Support

Hi @gzai 

Thank you for reaching out to Microsoft Fabric Community Forum.

The issue where some names don't have data in the previous year, you need a DAX measure that gracefully handles missing prior year data while still returning blank (or 0 if you prefer) rather than failing or skipping rows.

As for my experience your current measure has some issues.

You're using DATESYTD on a DATEADD, which assumes a full calendar, if previous year values are missing, DATEADD returns nothing, you're referencing Raw[Date] <= _max directly, which is unnecessary and might not work well in matrix context, you're not preserving context correctly for Name.

Correct DAX Last Year Spend, assuming you have a proper date table (DimDate) with a relationship to Raw[Date], try below DAX:

Last Year Spend = CALCULATE(SUM(Raw[Spend]),SAMEPERIODLASTYEAR(DimDate[Date]))

This measure returns the spend for the same period in the previous year. It works even if some names have no data in prior year, they’ll return blank (which is what Power BI shows for missing values) and also works perfectly in matrix visuals.

Try to Show 0 Instead of Blank, If you want to show 0 instead of blank when data is missing. Use below dax.

Last Year Spend = VAR SpendLastYear =  CALCULATE(SUM(Raw[Spend]), SAMEPERIODLASTYEAR(DimDate[Date]))
RETURN
COALESCE (SpendLastYear, 0)

Use DATEADD if No Time Intelligence Table, If you're not using a date table, then you can use below DAX.

Last Year Spend = CALCULATE (SUM(Raw [Spend]),DATEADD(Raw[Date], -1, YEAR))

But this only works, if Raw[Date] is at daily granularity when You’re showing matrix by Year (e.g., from YEAR(Raw[Date]))

Please use a calendar/date table (best practice for all time-based DAX) and Ensure that Raw[Date] has a relationship to the DimDate[Date] column. Use SAMEPERIODLASTYEAR or DATEADD depending on your model.

If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.

Regards,
Akhil.

SamsonTruong
Super User
Super User

Hi @gzai ,

Please try the adjusted measure and let me know if it achieves your desired result:

Last Year Spend =
VAR _max = MAXX(Raw, Raw[Date])
VAR _year = YEAR(_max)
VAR _lastYearStart = DATE(_year - 1, 1, 1)
VAR _lastYearEnd = DATE(_year - 1, 12, 31)
RETURN
CALCULATE(
    SUM(Raw[Spend]),
    Raw[Date] >= _lastYearStart &&
    Raw[Date] <= _lastYearEnd
)

 

If this helped, please mark it as the solution so others can benefit too. And if you found it useful, kudos are always appreciated.

Thanks,

Samson

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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