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

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.

Reply
eliasayyy
Super User
Super User

Create Dynamic Previous Result

hello everyone i am using timeline slicer custom visual with all granuality on from by day to by year.

i want to make a measure that will compare current vs previous based on selected date

so if i i am on the "By year" slicer , if i select 2023, i want to compare 2023 result to 2022 result ,
if i am on "By week" slicer , if i select week 3 of 2023 i want to compare to week 2 of 2023
if i am on "By month" slicer , if i select january 2023 i want to compare to december 2023

is this possible?

1 ACCEPTED SOLUTION
v-yueyunzh-msft
Community Support
Community Support

Hi , @eliasayyy 

According to your description, you are using the  timeline slicer custom visual . And your dimension is Year,Month and week . And you want to select one and then get the comparsion of the current object and previous object.

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1682920150929.png

For your need ,i recommend the 'Calendar' Table contains the all year's date.

Calendar = CALENDAR( DATE(2014,1,1), DATE(2016,12,31) )
 
(2)Then we can create a measure like this:

Measure = 
 var _slicer = MAX('Calendar'[Date])
var _cur_year =  YEAR(_slicer)
var _pre_year  = _cur_year -1 
var _diff_year =SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date]) = _pre_year)
var _curmonth = EOMONTH(_slicer ,-2)+1
var _diff_month = SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date])*100+ MONTH('Calendar'[Date]) = YEAR(_curmonth)*100+ MONTH(_curmonth)      )
var _preweek = YEAR( _slicer-7)*100 + WEEKNUM(_slicer-7)
var _diff_week = SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date])*100+ WEEKNUM('Calendar'[Date]) = _preweek      )
var _count = COUNTROWS('Calendar')
 return
IF(_count>300 , _diff_year , IF(_count>27 , _diff_month , _diff_week))

 

(3)Then we can put this measure on the card visual and we can get the comparsion.

vyueyunzhmsft_1-1682920850763.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

1 REPLY 1
v-yueyunzh-msft
Community Support
Community Support

Hi , @eliasayyy 

According to your description, you are using the  timeline slicer custom visual . And your dimension is Year,Month and week . And you want to select one and then get the comparsion of the current object and previous object.

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1682920150929.png

For your need ,i recommend the 'Calendar' Table contains the all year's date.

Calendar = CALENDAR( DATE(2014,1,1), DATE(2016,12,31) )
 
(2)Then we can create a measure like this:

Measure = 
 var _slicer = MAX('Calendar'[Date])
var _cur_year =  YEAR(_slicer)
var _pre_year  = _cur_year -1 
var _diff_year =SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date]) = _pre_year)
var _curmonth = EOMONTH(_slicer ,-2)+1
var _diff_month = SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date])*100+ MONTH('Calendar'[Date]) = YEAR(_curmonth)*100+ MONTH(_curmonth)      )
var _preweek = YEAR( _slicer-7)*100 + WEEKNUM(_slicer-7)
var _diff_week = SUM('Sales'[Sales])- CALCULATE( SUM('Sales'[Sales]) , YEAR('Calendar'[Date])*100+ WEEKNUM('Calendar'[Date]) = _preweek      )
var _count = COUNTROWS('Calendar')
 return
IF(_count>300 , _diff_year , IF(_count>27 , _diff_month , _diff_week))

 

(3)Then we can put this measure on the card visual and we can get the comparsion.

vyueyunzhmsft_1-1682920850763.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.