Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
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:
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.
I'd really appreciate some help here
Solved! Go to Solution.
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.
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 👍
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
11 | |
10 | |
10 | |
9 | |
8 |
User | Count |
---|---|
17 | |
13 | |
12 | |
11 | |
9 |