Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
mikelee1701
Helper III
Helper III

Comparing different dates same time period using slicers to show trending previous vs current period

Hi,

 

I have a sample pbix here that I am comparing holiday season for example Thanksgiving(TG) from 2011 vs 2012 with slicers for days before and after.

I want to get my graphs to show Trending comparing day to day (first day to second day and so on) based on the slicers before TG and after.

Right now my graph looks like this when I select the two years:

Screenshot_112421_065845_PM.jpg

my chart looks like this:

Screenshot_112921_084506_AM.jpg

That's because the values are on a different timeframe, but how can I move it so that it is comparing from day 1?

 

I need the trend lines to stack on top of each other instead of either ends of the chart.

 

How can I do that?

sample holiday slicers graph 

 

 

TIA,

Mike

 

 

1 ACCEPTED SOLUTION

Hi @mikelee1701 ,

 

I see. So we need to add the date difference to the formula.

 

Holiday Between max =
var HolStart = max(Holidays[Date]) - 'Days Before'[Days Before Value]
var HolEnd = max(Holidays[Date]) + 'Days After'[Days After Value]
var Hol_diff = datediff(MIN(Holidays[Date]),MAX(Holidays[Date]),DAY)
var _max = CALCULATE(sum(Sales[SalesAmount]),
FILTER(ALL('Sales'),
Sales[DateKey]>=HolStart&&sales[DateKey]<=HolEnd&&Sales[DateKey]=SELECTEDVALUE('Calendar'[DateKey])+Hol_diff
))
RETURN
IF(SELECTEDVALUE('Calendar'[DateKey])>=HolStart-Hol_diff&&SELECTEDVALUE('Calendar'[DateKey])<=HolEnd-Hol_diff,_max)
 
Capture.PNG
 
Best Regards,
Jay
Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

9 REPLIES 9
v-jayw-msft
Community Support
Community Support

Hi @mikelee1701 ,

 

Modify the formula as below:

 

Holiday Between max = 
var HolStart = max(Holidays[Date]) - 'Days Before'[Days Before Value]
var HolEnd = max(Holidays[Date]) + 'Days After'[Days After Value]
var _max = CALCULATE(sum(Sales[SalesAmount]),
   FILTER(ALL('Sales'),
       Sales[DateKey]>=HolStart&&sales[DateKey]<=HolEnd&&Sales[DateKey]=SELECTEDVALUE('Calendar'[DateKey])+365
))
RETURN 
IF(SELECTEDVALUE('Calendar'[DateKey])>=HolStart-365&&SELECTEDVALUE('Calendar'[DateKey])<=HolEnd-365,_max)

 

Result would be shown as below:

Capture.PNG

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

@v-jayw-msft ,

Thank you!  I changed it to 364 instead of 365, but I can't figure out why it does not work when I select different year comparisons like 2012 and 2013.  It should still shift correctly no matter the year right?

 

TIA,

Mike

Hi @mikelee1701 ,

 

It's working fine on my side.

As you can see in the screenshot, 2012 Thanksgiving is from 11/20 to 11/26 and 2013 Thanksgiving is from 11/26 to 12/2.

Capture.PNG

It may be because they only overlap one day, so it's not obvious in the line chart.

And if you want compare 2011 with 2013, you will need use year difference * 365, like:

var year_difference = year(max(date))-year(min(date))
+/- year_difference*365

 

Holiday Between max = 
var HolStart = max(Holidays[Date]) - 'Days Before'[Days Before Value]
var HolEnd = max(Holidays[Date]) + 'Days After'[Days After Value]
var year_difference = YEAR(MAX(Holidays[Date]))-YEAR(min(Holidays[Date]))
var _max = CALCULATE(sum(Sales[SalesAmount]),
   FILTER(ALL('Sales'),
       Sales[DateKey]>=HolStart&&sales[DateKey]<=HolEnd&&Sales[DateKey]=SELECTEDVALUE('Calendar'[DateKey])+year_difference*365
))
RETURN 
IF(SELECTEDVALUE('Calendar'[DateKey])>=HolStart-year_difference*365&&SELECTEDVALUE('Calendar'[DateKey])<=HolEnd-year_difference*365,_max)

Capture.PNG

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

Hi @v-jayw-msft ,

Thank you.

In the screenshot you sent, the lines are offset from each other.  That's what I'm trying to avoid.  

I need a comparison day to day and so on (on top of each other) like this:

no matter which years or holidays selected.

Screenshot_120121_020742_PM.jpg

 

TIA,

Mike

Hi @mikelee1701 ,

 

I see. So we need to add the date difference to the formula.

 

Holiday Between max =
var HolStart = max(Holidays[Date]) - 'Days Before'[Days Before Value]
var HolEnd = max(Holidays[Date]) + 'Days After'[Days After Value]
var Hol_diff = datediff(MIN(Holidays[Date]),MAX(Holidays[Date]),DAY)
var _max = CALCULATE(sum(Sales[SalesAmount]),
FILTER(ALL('Sales'),
Sales[DateKey]>=HolStart&&sales[DateKey]<=HolEnd&&Sales[DateKey]=SELECTEDVALUE('Calendar'[DateKey])+Hol_diff
))
RETURN
IF(SELECTEDVALUE('Calendar'[DateKey])>=HolStart-Hol_diff&&SELECTEDVALUE('Calendar'[DateKey])<=HolEnd-Hol_diff,_max)
 
Capture.PNG
 
Best Regards,
Jay
Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

@v-jayw-msft ,

 

Thank you!  that works!

Now final question, but this is easy.

 

Can you tell me a bit about the type of person (like you) who can just simply type all that out so seemingly so easy?  I am not a developer at heart.  I have taken programming classes in the past like assembly, Pascal, Visual Basic, maybe one ore two others, but they were not career paths, nor I really want to get deep into.

This is something that just happen to land on me, and for the most part once it gets longer then 3 to 5 lines, I start to have a hard time.  I spend too much time thinking about it even when "the day is over".

 

For the most part, I find what I can from the internet (and here), and then use their code, piece together what I can, maybe tweek it to get it to work most of the time.

 

What advice do you have for someone like me.  Is picking this stuff up easier as you do it more?  Is it a good idea to reuse code from the internet?  What should I read to approach your level?  I've been using PBI for about 3 years now.

 

Thanks again!

Mike

Hi @mikelee1701 ,

 

You are welcome.

The formula you wrote is already quite remarkable. What I did was just a little modification based on your formula.

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
AllisonKennedy
Super User
Super User

@mikelee1701 

 

See if this post helps: https://excelwithallison.blogspot.com/2021/09/power-bi-forecasting-with-irregular.html

 

Should be similar to what you're trying to do.


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy ,

 

thank you... but my little brain is having a hard time making the connection.

I also google a lot of places and I look for 'hot' areas to clue in(before actually diving into the code) on to help me and I'm not making the connection...

Helpful resources

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