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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
lherbert501
Post Patron
Post Patron

Average range by slicer

Hi there,

 

I currently have my dax code below that gives me the average over the previous 6 months which works correctly.

 

6MonthSalesAvg =
CALCULATE (
SUMX ( VALUES ( SalesOrders), [Total Sales] ),
DATESINPERIOD (RS_Dates[DateFull], MAX (RS_Dates[DateFull] ), -6, MONTH ), RS_Dates[IsWorkingDay]=true()
) /6
 
 
What I want to do is to give me the average of that month selected and the months previous.
E.g image 1 below would be 202201 - 202206 (6months)

Image 2 would be 202201 - 202205 with the 5 months and so on with less months. 

 

Is this possible? 

 

Thankyou

 

1)

 

Capture.PNG

 

2)

 

Capture1.PNG

 

 

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

Hi @lherbert501 ,

 

Based on your description, I have created a simple sample:

vjianbolimsft_0-1659093016646.png

 

Please try:

First create a table for slicer:

vjianbolimsft_1-1659093016648.png

 

Then change the slicer to single select:

vjianbolimsft_2-1659093016649.png

 

Then create a measure like this:

Average =

var _a = VALUE( RIGHT( SELECTEDVALUE('For Slicer'[Value]),2))

return SUMX(FILTER('SalesOrders',MONTH([Date])<=_a),[Total Sales])/_a

Final output:

vjianbolimsft_3-1659093016650.png

 

vjianbolimsft_4-1659093016650.png

Best Regards,

Jianbo Li

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

6 REPLIES 6
v-jianboli-msft
Community Support
Community Support

Hi @lherbert501 ,

 

Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or if you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

Refer to:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

 

Best Regards,

Jianbo Li

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

v-jianboli-msft
Community Support
Community Support

Hi @lherbert501 ,

 

Based on your description, I have created a simple sample:

vjianbolimsft_0-1659093016646.png

 

Please try:

First create a table for slicer:

vjianbolimsft_1-1659093016648.png

 

Then change the slicer to single select:

vjianbolimsft_2-1659093016649.png

 

Then create a measure like this:

Average =

var _a = VALUE( RIGHT( SELECTEDVALUE('For Slicer'[Value]),2))

return SUMX(FILTER('SalesOrders',MONTH([Date])<=_a),[Total Sales])/_a

Final output:

vjianbolimsft_3-1659093016650.png

 

vjianbolimsft_4-1659093016650.png

Best Regards,

Jianbo Li

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

Thankyou @v-jianboli-msft  🙂

MahyarTF
Memorable Member
Memorable Member

Hi,

In your dax function, check if the Month number is less than 6, then use StartOfYear('Date[Date]') in your date condition, otherwise use the current one(existing Dax that you wrote)

Mahyartf

Hi @MahyarTF 

 

Thanks for the reply.

 

How would you check if its less than 6 and where would the start of the year be in the query?

 

I think this is the part im struggling with?

 

Thanks in advance

 

Liam

If you have a Date dimension, you need to add the Number of months and then use it in your dax,

Otherwise, you could you the Right() function :

if (Right([Period], 2)>'06'), Date[Date] > Your Function && date[date] <= Max(Date[Date]),  date[date] >=  StartOfYear('Date[Date]') && date[date] <= Max(Date[Date])

Mahyartf

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.