cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Sperling
Helper I
Helper I

DATEADD based on relative date number in page filter

Hi,

 

Is it possible to get DATEADD to be dynamic based on the number written manually in the filter?

 

So I have a relative date filter on the page showing last X days, and I want DATEADD to work like this:

 

DATEADD(
'Date'[Date],
- X,

DAYS

)

1 ACCEPTED SOLUTION

@Sperling , refer if these can help

 

same period based on date range
Last Period =
var _max =maxx(allseleceted(date),date[date])
var _min =maxx(allseleceted(date),date[date])
var datediff1 = datediff(_min,_max,day)
var _maxX = _max-datediff1
var _minX = _min -datediff1
return
CALCULATE(SUM(Sales[Sales Amount]),filter(all(date,date[date]<=_maxX &&date[date]>=_minX)))

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
!! Subscribe to my youtube Channel !!

View solution in original post

3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Hi  @Sperling ,

I created some data:

vyangliumsft_0-1642992408853.png

For example:

To display the past X days of today, we can refer to a parameter to display by selecting the parameter.

Here are the steps you can follow:

1. Modeling – New parameter.

vyangliumsft_1-1642992408856.png

2. Select parameters according to your needs.

vyangliumsft_2-1642992408857.png

3. Create measure.

Flag =
IF(MAX('Table'[Date])>=TODAY()-[Parameter Value]&&MAX('Table'[Date])<=TODAY(),1,0)

4. Place [Flag] in Filters, set is=1, apply filter.

vyangliumsft_3-1642992408858.png

5. Result:

If parameter selection is 5, the data of today and the previous 5 days will be displayed.

vyangliumsft_4-1642992408859.png

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Sperling
Helper I
Helper I

The following code works, but I'm not a big fan of the extra minus 1 in the DateAdded variable - maybe its just because thats the way the DATEDIFF works?
 
VAR DIFF =
DATEDIFF(
MIN('Date'[Date]),
MAX('Date'[Date]),
DAY
)

 

VAR DateAdded =
DATEADD(
'Date'[Date],
DIFF 1,
DAY
)


@Sperling , refer if these can help

 

same period based on date range
Last Period =
var _max =maxx(allseleceted(date),date[date])
var _min =maxx(allseleceted(date),date[date])
var datediff1 = datediff(_min,_max,day)
var _maxX = _max-datediff1
var _minX = _min -datediff1
return
CALCULATE(SUM(Sales[Sales Amount]),filter(all(date,date[date]<=_maxX &&date[date]>=_minX)))

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
!! Subscribe to my youtube Channel !!

Helpful resources

Announcements
May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through June10th!

Power BI Dev Camp Session 22 without aka link and time 768x460.jpg

Check it Out!

Watch Session 22 Ted's Dev Camp along with past sessions!

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!

Top Kudoed Authors