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
Sebastian
Advocate II
Advocate II

calculate a sum with a date filter today - 1 year

Hi,

 

i try to calculate a sum with values between today and today -1 year.

 

following syntax doesn't work:

 

Sum = Calculate(sum(table[value]);table[date]>= dateadd(today();-1;year))

 

Can't use today() function in time functions like dateadd or datesinperiod

 

hope someone could help.

 

4 REPLIES 4
ZweiNeunSieben
Regular Visitor

You can use something like this:

 

DATE(YEAR(TODAY())-1; MONTH(TODAY()); DAY(TODAY()))
greggyb
Resident Rockstar
Resident Rockstar

Time intelligence functions work on a date field in a date dimension. They do not work on scalar date values.

 

That being said, you can do simple arithmetic on dates:

 

RollingYearMeasure = 
CALCULATE(
	SUM( FactStupid[Amount] )
	;DimDate[Date] >= TODAY() - 365
)

Hi,

 

yes that works but it isn't a completly correct solution. The count of days switch between 364 to 365 days in addiction of a "normal" year and a leap year.

That depends entirely on your calendar. If this is a concern for you, I'd set up a field in my date dimension that indicates today, say TodayFlag:

// Power Query
TodayFlag =
[Date] = DateTime.Date( DateTime.LocalNow() )

This will run as part of your queries every time the model is refreshed. So long as you refresh >=1 / day, it'll be up to date with your data.

 

In your report you can filter to TodayFlag = True - this will always filter to today's value. Then your measures can take advantage of SAMEPERIODLASTYEAR():

SamePeriodLastYear =
CALCULATE(
    <expression>
    ,SAMEPERIODLASTYEAR( DimDate[Date] )
)

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.