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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Charcoal55
Regular Visitor

DATEADD and MAX?

Hi,
I understand that the below filter function returns a table containing all dates in the date table and where the latest date in that table is defined by the filter context. Thus, if the date table contains all dates between 2000 and 2024 and the filter context is 2023, the below function will return a table with all dates between 2000-2023.
 
    FILTER(
        ALL(Dates),
        Dates[Date] <= MAX(Dates[Date])
    )

However, if I want (for the very same filter context, that is, 2023) to return a table will all dates between 2000-2022, I guess I can modify the above function so it instead reads:
 
FILTER(
        ALL(Dates),
        Dates[Date] <= MAX(Dates[Date]) - 365
    )
 
But there should be some more elegant solution using DATEADD or SAMEPERIODLASTYEAR instead, something like below but I don't get it to work. I assume this is super simple, but for now I got stuck...
 
FILTER(
        ALL(Dates),
        Dates[Date] <= DATEADD(MAX(Dates[Date]), -1, YEAR) 
    )
1 ACCEPTED SOLUTION
FreemanZ
Super User
Super User

hi @Charcoal55 ,

 

try like:

 

FILTER(

        ALL(Dates),

        Dates[Date] <= EDATE(MAX(Dates[Date]), -12) 

    )

 

EDATE is not time intelligence function and thus less picky.

View solution in original post

4 REPLIES 4
FreemanZ
Super User
Super User

hi @Charcoal55 ,

 

try like:

 

FILTER(

        ALL(Dates),

        Dates[Date] <= EDATE(MAX(Dates[Date]), -12) 

    )

 

EDATE is not time intelligence function and thus less picky.

FarhanJeelani
Solution Supplier
Solution Supplier

Hi @Charcoal55 

You're right in thinking that DATEADD can be used to adjust the filter context, but there’s a small issue with how you're applying it in the FILTER function.

In your case, you want to return all dates from 2000 to 2022 when the filter context is for 2023. You can achieve this by shifting the latest date in the context (which is defined by MAX(Dates[Date])) back by one year. Here's the correct approach using DATEADD:

FILTER(
    ALL(Dates),
    Dates[Date] <= DATEADD(MAX(Dates[Date]), -1, YEAR)
)

Explanation:

  • MAX(Dates[Date]): This gets the latest date in the current filter context (e.g., 2023).
  • DATEADD(MAX(Dates[Date]), -1, YEAR): This shifts that latest date back by one year, so if the filter context is 2023, it becomes 2022.
  • ALL(Dates): This removes any filters that might be applied to the Dates table, ensuring that you're looking at the entire range of dates.
  • Dates[Date] <= DATEADD(...): Finally, the FILTER ensures that you're only including dates up to (and including) the adjusted date (2022 in this case).

This approach is cleaner and avoids hardcoding the date offset (e.g., -365). The DATEADD function handles the exact date subtraction, which accounts for leap years and other complexities.

 

Please mark this as solution if it helps. Appreciate Kudos.

The problem is that I get the following error message when I am trying the formula you suggest:

"The first argument to 'DATEADD' must specify a column."

I interpret it that since MAX returns a scalar value and that is not acceptable by DATEADD because it requires a column as it's first argument.

 

bhanu_gautam
Super User
Super User

@Charcoal55 , Formula is correc

DAX
FILTER(
ALL(Dates),
Dates[Date] <= DATEADD(MAX(Dates[Date]), -1, YEAR)
)

 

Just ensure that

The Dates table is properly related to other tables in your data model.
The filter context is correctly applied.
There are no syntax errors or other issues in the broader DAX expression where this formula is used.




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






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 MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.