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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DJ1977
Helper I
Helper I

Dax with relative date

Hello. 

I would have greatly appreciated if someone could help me with a dax formula for the following case:
I have a dimension table for product, a fact table for sales and a calendertable.  Relevant tables:

Tables.JPG


The products are food and have a shelf life. The different products have different shelf lives. I have this DAX that calculates how many customer have purchased the selected products within the shelf life (duration time) based on today's date:

 

# Customers Purchased in Shelf life =
VAR Last_date = TODAY()
VAR Shelf = SELECTEDVALUE(DimProduct[ProductShelflife])

Return
CALCULATE( DISTINCTCOUNT(FactSales[CustomerID]),
FILTER( ALL(DimCalendar),
DimCalendar[Date] > Last_date - Shelf &&
DimCalendar[Date] < Last_date))

 

Now i want to add a relative date filter in my report so that the users can for example choose last 2 weeks and then see if the product is purchased within the last two weeks, insteed of the shelf life date. I assume i have to make Dax to be calculated with the selected filter instead of VAR Shelf. How can i adjust my DAX to achieve this? If the relative date filter is blank, i want the dax to use VAR Shelf.

Relative date.JPG

 

I'd really appreciate some help here

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Can you just use DimCalendar instead of ALL ( DimCalendar ) or is there some local filter context that prevents that from working?

Quantity Purchased Shelf life =
VAR Last_date = TODAY ()
VAR Shelf = SELECTEDVALUE ( DimProduct[ProductShelflife] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( FactSales[CustomerID] ),
        FILTER (
            DimCalendar,
            DimCalendar[Date] > Last_date - Shelf
                && DimCalendar[Date] < Last_date
        )
    )

 

If that doesn't work, you could try ALLSELECTED instead of ALL.

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

Can you just use DimCalendar instead of ALL ( DimCalendar ) or is there some local filter context that prevents that from working?

Quantity Purchased Shelf life =
VAR Last_date = TODAY ()
VAR Shelf = SELECTEDVALUE ( DimProduct[ProductShelflife] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( FactSales[CustomerID] ),
        FILTER (
            DimCalendar,
            DimCalendar[Date] > Last_date - Shelf
                && DimCalendar[Date] < Last_date
        )
    )

 

If that doesn't work, you could try ALLSELECTED instead of ALL.

The removal of All solved my case. Thank you so much for the quick response @AlexisOlson 👍

 

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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