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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

How to calculate Sales for select period (Dynamic)

Hi Team !

 

I'm Struggling with the select period date values for LAST YEAR sales ,

For select period Current Year Sale for DAX is as follows please suggest a DAX for Last YEAR considering followin Current Year DAX.

 

CY Select Period =
VAR TodayDate = TODAY()
VAR CurrentYear = YEAR ( TodayDate )
VAR FilteredYear = YEAR ( MAX ( Dates[Date] ) )
VAR YearShift = FilteredYear - CurrentYear
VAR YTDTotal =
IF(
ISFILTERED(Dates),'Sale Calculation'[Total Sales],
CALCULATE(
'Sale Calculation'[Total Sales],
DATEADD (
DATESYTD ( TREATAS( {TODAY()} ,Dates[Date] ) ),
YearShift,
YEAR
)
)
)
RETURN
COALESCE ( YTDTotal, 0 )
 
When i select any random date e.g 11/11/2021 to 05/02/2022 then the select date Previous year  i.e11/11/2020 to 05/02/2022 values should appear but 
but it calculate 2021 year total sales.
Pl
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

If you want to calcualte the total sales for previous period of selected period, you could create a flag measure:

Flag = 
var _min=DATE(YEAR(MIN('Dates'[Date]))-1,MONTH(MIN('Dates'[Date])),DAY(MIN('Dates'[Date])))
var _max=DATE(YEAR(MAX('Dates'[Date]))-1,MONTH(MAX('Dates'[Date])),DAY(MAX('Dates'[Date])))
return IF( MAX('Sale Calculation'[Date]) >=_min && MAX('Sale Calculation'[Date])<=_max,1,0)

And apply it to visual-filter pane, set as "is 1":

Eyelyn9_0-1647397478505.png

 

Best Regards,
Eyelyn Qin
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

5 REPLIES 5
Anonymous
Not applicable

Hi @Anonymous ,

 

If you want to calcualte the total sales for previous period of selected period, you could create a flag measure:

Flag = 
var _min=DATE(YEAR(MIN('Dates'[Date]))-1,MONTH(MIN('Dates'[Date])),DAY(MIN('Dates'[Date])))
var _max=DATE(YEAR(MAX('Dates'[Date]))-1,MONTH(MAX('Dates'[Date])),DAY(MAX('Dates'[Date])))
return IF( MAX('Sale Calculation'[Date]) >=_min && MAX('Sale Calculation'[Date])<=_max,1,0)

And apply it to visual-filter pane, set as "is 1":

Eyelyn9_0-1647397478505.png

 

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

danielboi
Helper I
Helper I

How about an all selected on your slicer values. Extract min and max date out of that filter context and then reduce both dates by one year and use this in a calculate function?

Anonymous
Not applicable

Hi Daniel,

 

Actually Not getting can you please form a DAX as per your knowlegde here  and will try it on my measures .
Even i'm New to Power BI.
Hope you getting me.

danielboi
Helper I
Helper I

Hi @Anonymous ,

 

cannot comment on your formula, but did you try use 

 

DATESYTD & SAMEPERIODLASTYEAR

 

CALCULATE('Sale Calculation'[Total Sales],          <-Measure or column? If column might need to put it into sum()

DATESYTD(

SAMEPERIODLASTYEAR(Dates[Date])

)

)

Anonymous
Not applicable

YES but using this DAX i'm just fecthing last year whole sales total

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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