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.
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 👍
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
43 | |
23 | |
21 | |
15 | |
15 |
User | Count |
---|---|
45 | |
31 | |
30 | |
18 | |
17 |