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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Comparison based on date filter

Hi,

 

Let's say I have a date slicer and I choose 2 dates. How can I calculate a KPI based on same number of days in the previous period? For example: If I choose May 10 - May 15...then KPI should calculate the change from previous 5 days before May 10 (May 5 to May 10) and between May 10-15. If I choose 3 months in the date filter, the KPI should compare the previous 3 months before the selection and show the % change. Any ideas?

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Do you want to divide the number of Value in a period of time by the number of Value in a period of time before?

Maybe you can refer this measure, we change the SUM function to COUNT function.

 

Measure = 
var min_date = MIN('Date'[Date])
var max_date = MAX('Date'[Date])
var x = DATEDIFF(min_date , max_date,DAY)
var last_x_date = min_date - x
var max_date_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=max_date))
var min_date_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=min_date))
var last_x_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=last_x_date))
var difference_1 = max_date_value - min_date_value
var difference_2 = min_date_value - last_x_value
return
DIVIDE(difference_1 , difference_2)

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the pbix that we have shared?

 

It will be helpful if you can show us the exact expected result based on the tables. Please upload your files to OneDrive For Business and share the link here.

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

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

View solution in original post

8 REPLIES 8
v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

How about the result after you follow the suggestions mentioned in my original post?

Could you please provide more details or expected result about it If it doesn't meet your requirement?

 

Best regards,

 

Community Support Team _ zhenbw

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

v-zhenbw-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can use the following measure to meet your requirement.

 

Measure = 
var min_date = MIN('Date'[Date])
var max_date = MAX('Date'[Date])
var x = DATEDIFF(min_date , max_date,DAY)
var last_x_date = min_date - x
var max_date_value = CALCULATE(SUM('Table'[Values]),FILTER('Table','Table'[Date]=max_date))
var min_date_value = CALCULATE(SUM('Table'[Values]),FILTER('Table','Table'[Date]=min_date))
var last_x_value = CALCULATE(SUM('Table'[Values]),FILTER('Table','Table'[Date]=last_x_date))
var difference_1 = max_date_value - min_date_value
var difference_2 = min_date_value - last_x_value
return
DIVIDE(difference_1 , difference_2)

 

Com 1.jpg

 

Please notice that there is no relationship between table and date table.

 

Com 2.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that we have shared?

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

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

Anonymous
Not applicable

@v-zhenbw-msft : It doesn't give the output. Here's the expected output based on your PBI file.

If in the date filter I select 1/7/2020 to 1/9/2020 which means 3 days...then the KPI should show the % different based on the count of values from 1/7/2020 to 1/9/2020 and 1/4/2020 and 1/6/2020 (going back three days). Basically the idea is to check the % difference of counts in the timeframe selected and going back the same timeframe before.

If I would have selected 1/7/2020 to 1/8/2020 then the count % difference would be from 1/7/2020 to 1/8/2020 and 1/6/2020 and 1/7/2020

Hi @Anonymous ,

 

Do you want to divide the number of Value in a period of time by the number of Value in a period of time before?

Maybe you can refer this measure, we change the SUM function to COUNT function.

 

Measure = 
var min_date = MIN('Date'[Date])
var max_date = MAX('Date'[Date])
var x = DATEDIFF(min_date , max_date,DAY)
var last_x_date = min_date - x
var max_date_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=max_date))
var min_date_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=min_date))
var last_x_value = CALCULATE(COUNT('Table'[Values]),FILTER('Table','Table'[Date]=last_x_date))
var difference_1 = max_date_value - min_date_value
var difference_2 = min_date_value - last_x_value
return
DIVIDE(difference_1 , difference_2)

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the pbix that we have shared?

 

It will be helpful if you can show us the exact expected result based on the tables. Please upload your files to OneDrive For Business and share the link here.

 

BTW, pbix as attached.

 

Best regards,

 

Community Support Team _ zhenbw

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

amitchandak
Super User
Super User

@Anonymous , Try with a date table

Same Date range last period = 
var _diff = datediff(MIN('Date'[date]),max('Date'[date]),DAY)
var _p_st_date = MINX('Date',DATEADD('Date'[date],-1*_diff,Day))-1
var _p_end_date = MAXX('Date',DATEADD('Date'[date],-1*_diff,Day))-1
Return
CALCULATE(sum(Sales[Sales Amount]),all('Date'[date]),'Date'[date]>=_p_st_date && 'Date'[date]<= _p_end_date
)

 

+/- 1 might be required for adjustment, please check

 

This might combine data in a day. Please refer how deal with such

https://www.youtube.com/watch?v=duMSovyosXE

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184


Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
lbendlin
Super User
Super User

I would look into FIRSTNONBLANK and LASTNONBLANK in your filter context and then flip that around.

 

Something like this (pseudo code)

 

var start = FIRSTNONBLANK(ALLSELECTED(...))

var end = LASTNONBLANK(ALLSELECTED(...))

DesiredStart = 2*start-end

DesiredEnd = start-1

Anonymous
Not applicable

How do I use the desired start? I tried but  it's not working

You use DesiredStart and DesiredEnd in the DATESBETWEEN() filter of your measure.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.