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
Kritika_
Frequent Visitor

Previous Year Same Time Period only till todays date

So I have a single global filter with yr,qtr,month.The columns don't use the date calendar in power BI.I have the columns date(Type:date), qtr(Type:whole number), Month(Type:whole number), year(Type:whole number).

I need the values for current and previous year for YOY % change.If the selected current year from the filter is actually the current year then I want my previous year to calculate only for the data untill last year today's date.And the values should change properly when I select year,quarter or month.I'm using the conditions:

return if (
          selected_prev_year<>Original_prev_yr,   
            //for all the selected current yr < 2023 as we'll have data for all months
 
          alllogic,   
          //calculates for the required metric between min date and max date of selected year-1.     
                                   
          if (                                                                
              And(                                                          
                prev_year=Original_prev_yr,                    
                MindateInQuarter<=prev_end_date   
                 //for actual current yr as the data will be available only till Today(),I added the 2nd condition because if I select quarter then it was showing the values of 2nd quarter for 3rd and 4th also.Whereas it should be blank.  
 
                    prev_end_date=date made from todays date & month and selecetd-1 yr.
 
                    MindateInQuarter=min date for selected quarter
              ),                                                                
              curryearlogic,               
              //calculates for the required metric between min date to today's date of selected year-1.
 
          if(
            AND(    prev_year=Original_prev_yr,                      
                        MaxdateInSelectedQuarter < MindateInNextQuarter   
                          //Now only after the 2nd condition is true then this should be checked that if I select a quarter then the                                                calculations should be max date of quarter should be only till the max date of quarter.
 
            ),  
            forQuarter       
            //calculates for the required metric untill max date of selected quarter for selecetd yr-1.
 
          )
        ))
I have cross checked if the dates in my variables are correct except for 'MindateInNextQuarter'.
 
var MindateInNextQuarter = CALCULATE(min(d_date_c[day_dt]),d_date_c[cal_qtr_nbr]=SELECTEDVALUE(d_date_c[cal_qtr_nbr])+1,d_date_c[cal_yr_nbr] = SELECTEDVALUE(d_date_c[cal_yr_nbr])-1)
If I return this it gives blank even tho the data type of cal_qtr_nbr is whole number.All other date variables return correct dates.
 
I am getting the same value when I select on 2023,1st quarter and all th emonths in the 1st quarter.I'm getting different value for 2nd quarter which is correct but then the same value for all the months in the second quarter.

How I've calculated other variables:
var prev_year_min_date = CALCULATE(MIN(d_date_c[day_dt]),ALL(d_date_c[cal_yr_nbr],d_date_c[cal_qtr_nm],d_date_c[cal_mth_nbr]),REMOVEFILTERS(d_date_c[cal_yr_nbr],d_date_c[cal_qtr_nm],d_date_c[cal_mth_nbr]),d_date_c[cal_yr_nbr] = SELECTEDVALUE(d_date_c[cal_yr_nbr])-1)
And subsequently others.

Can any one please suggest how do I get the correct values for quarter and month if the prev year=actual prev year.
I can just - 1 from the selected yr and skip all these conditions but it doesn't work for my purpose.

Thankyou so much for your time!


1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Kritika_ , Date table time intelligence

 

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

 

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),SAMEPERIODLASTYEAR('Date'[Date]))

 

YTD =
var _max = if(isfiltered('Date'),MAX( 'Date'[Date]) , today())
var _min = eomonth(_max,-1*MONTH(_max))+1
return
CALCULATE([net] ,DATESBETWEEN('Date'[Date],_min,_max))

 

 

LYTD =
var _max1 = if(isfiltered('Date'),MAX( 'Date'[Date]) , today())
var _max = Date(Year(_max1)-1, Month(_max1), Day(_max1))
var _min = eomonth(_max,-1*MONTH(_max))+1
return
CALCULATE([net] ,DATESBETWEEN('Date'[Date],_min,_max))

 

 

Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
Custom Period till date: https://youtu.be/aU2aKbnHuWs&t=145s


Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
https://www.youtube.com/watch?v=km41KfM_0uA
Power BI — Qtr on Qtr with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-qtd-questions-time-intelligence-2-5-d842063da839
https://www.youtube.com/watch?v=8-TlVx7P0A0
Power BI — Month on Month with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
https://www.youtube.com/watch?v=6LUBbvcxtKA
Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8
Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
Day Intelligence - Last day, last non continous day
https://medium.com/@amitchandak.1978/power-bi-day-intelligence-questions-time-intelligence-5-5-5c324...

View solution in original post

2 REPLIES 2
GJeanes1
Frequent Visitor

Checking back on this, were you able to write a Dax code that looks at last year today's date?

amitchandak
Super User
Super User

@Kritika_ , Date table time intelligence

 

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

 

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),SAMEPERIODLASTYEAR('Date'[Date]))

 

YTD =
var _max = if(isfiltered('Date'),MAX( 'Date'[Date]) , today())
var _min = eomonth(_max,-1*MONTH(_max))+1
return
CALCULATE([net] ,DATESBETWEEN('Date'[Date],_min,_max))

 

 

LYTD =
var _max1 = if(isfiltered('Date'),MAX( 'Date'[Date]) , today())
var _max = Date(Year(_max1)-1, Month(_max1), Day(_max1))
var _min = eomonth(_max,-1*MONTH(_max))+1
return
CALCULATE([net] ,DATESBETWEEN('Date'[Date],_min,_max))

 

 

Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
Time Intelligence, DATESMTD, DATESQTD, DATESYTD, Week On Week, Week Till Date, Custom Period on Period,
Custom Period till date: https://youtu.be/aU2aKbnHuWs&t=145s


Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
https://www.youtube.com/watch?v=km41KfM_0uA
Power BI — Qtr on Qtr with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-qtd-questions-time-intelligence-2-5-d842063da839
https://www.youtube.com/watch?v=8-TlVx7P0A0
Power BI — Month on Month with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
https://www.youtube.com/watch?v=6LUBbvcxtKA
Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8
Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510
Day Intelligence - Last day, last non continous day
https://medium.com/@amitchandak.1978/power-bi-day-intelligence-questions-time-intelligence-5-5-5c324...

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.