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 am trying to solve a situation with date slicers and a custom offset. I will try to explain it.
I have a table with orders and returns like this:
id | order_date | return_date | units_bought | units_returned |
3 | 2020-04-20 | null | 1 | 0 |
4 | 2020-04-25 | null | 1 | 0 |
1 | 2020-04-27 | 2020-05-06 | 1 | 1 |
2 | 2020-04-28 | 2020-05-06 | 4 | 3 |
(If the order has not been returned, it will have a null value in return_date column)
Then, I have a date slicer linked with order_date column. So, when I select a time period from 1 of April to 30 of April, I will get all the lines of the table.
However, I want to add a value called "offset" that will be related to the time the client has to return the order. For example, an offset of 10 days, will say that if the order has been bought on 1 of april, it can be returned up to 10 of april.
Then, what I want is to use that offset to filter values of the table:
1) If the order hasn't any return, we should check that the order_date + offset is less than selected period. This is because we want to exclude the orders that are still live, the ones that the client can still return it
2) If the order has been returned, we should check that order_date + offset is less than return_date
I have tried to start with the first condition, ignoring for the moment, the second one. However, my approach didn't work.
Here is the measure that I did:
var max_selected_date = MAX(dates[id]) --the max day of date slicer
var var_offset = SELECTEDVALUE(OFFSET[OFFSET]) --the selected offset value
metric = CALCULATE(
SUM(orders[units_bought]);
FILTER(
orders;
DATEADD(orders[order_date];var_offset;DAY) <= max_selected_date
)
)--the valid orders
return metric
Imagine that I select a date period from 1 to 30 of April, and an offset of 4 days. What I want is only the orders of the days 20 and 25 of april. The orders of 27 and 28 should not be considered because 27 + 4days = 1 of May, and 1 of May is greather than 30 of April. The order of 25 of may should be considered because 25 + 4 days = 29 of April, and 29 of April is included in the selected date period.
What I am doing wrong with my formula?
Best regards!
Solved! Go to Solution.
Like this?
sales_sum_slicer =
VAR MaxDate = MAX ('Date'[Date] )
VAR MinDate = MIN('Date'[Date])
VAR DaysBeforeDatea= MaxDate - 4
RETURN
CALCULATE (
CALCULATE(SUM('Table'[units_bought])); --
'Date'[Date]<= DaysBeforeDatea; 'Date'[Date] >= MinDate;
ALL ( 'Date' )
)
File is here.
Kind regards, Steve
pls mark as solved when your query has been answered.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
What exactly is going wrong? Which output are you getting / not getting compared to expected?
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
The problem is that the orders are not filtered. It seems that the dateadd function is not working
Like this?
sales_sum_slicer =
VAR MaxDate = MAX ('Date'[Date] )
VAR MinDate = MIN('Date'[Date])
VAR DaysBeforeDatea= MaxDate - 4
RETURN
CALCULATE (
CALCULATE(SUM('Table'[units_bought])); --
'Date'[Date]<= DaysBeforeDatea; 'Date'[Date] >= MinDate;
ALL ( 'Date' )
)
File is here.
Kind regards, Steve
pls mark as solved when your query has been answered.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
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 |
---|---|
41 | |
20 | |
19 | |
15 | |
15 |
User | Count |
---|---|
47 | |
28 | |
25 | |
17 | |
17 |