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
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
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.

Top Solution Authors