cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
Amrselim1989
Helper II
Helper II

Allselected with filtration

hello Guys,

i hope you can help me with this,

 

i have this code

VAR MaxSales = MAXX ( ALLSELECTED ( dimDate[Year], dimDate[Month], DimDate[MMM] ), [Sum of Net Sales Value] ) 
VAR
MinSales = MINX ( ALLSELECTED ( dimDate[Year], dimDate[Month], dimDate[MMM] ), [Sum of Net Sales Value] )
VAR
Result = IF ( [Sum of Net Sales Value] = MaxSales || [Sum of Net Sales Value] = MinSales,
[Sum of Net Sales Value],
" " )
RETURN
Result

 which is working so fine for me, except for one thing, i wish i can filter this code the min and max value doesn't consider the current month's date in it, because it always shows the current month as the Min sales wish is natural cause the month didn't finish yet...

so I wish to remove the month of July 2023 from this code...

Any help, please?!!

1 ACCEPTED SOLUTION
Martin_D
Super User
Super User

Hi @Amrselim1989 ,

I have two options in my mind to solve this. 

#1 You could in you measure get the latest date with sales data, derive year and month from this date, and filter your data table for including only months before that current month.
# 2 You can add a column to your dimDate table that marks current and previous months and then in the measure you can use this column to only include previous months. This is what I'm gonna show you.

 

Add this as a calculated column to you dimDate Table:

 

Running Month = DATEDIFF ( TODAY(), 'dimDate'[Date], MONTH )

 

This will give you a column that contains 0 for the current month, -1, -2, -3, ... for past months and 1, 2, 3, ... for future month. Instead of using TODAY() in the code, you can alternatively relate to your last date with sales as today if there is some delay in your data processing pipeline until sales data actually arrives in your report.
Then wrap you code into a CALCULATE() function that exludes the current (and future) months:

 

CALCULATE (

    VAR MaxSales = MAXX ( ALLSELECTED ( dimDate[Year], dimDate[Month], DimDate[MMM] ), [Sum of Net Sales Value] ) 
    VAR MinSales = MINX ( ALLSELECTED ( dimDate[Year], dimDate[Month], dimDate[MMM] ), [Sum of Net Sales Value] ) 
    VAR Result = IF ( [Sum of Net Sales Value] = MaxSales || [Sum of Net Sales Value] = MinSales, 
    [Sum of Net Sales Value],
     " " ) 
    RETURN Result

    ,KEEPFILTERS( 'dimDate'[Running Month] < 0 )
)

 

 BR

Martin

github.pnglinkedin.png

View solution in original post

2 REPLIES 2
Martin_D
Super User
Super User

Hi @Amrselim1989 ,

I have two options in my mind to solve this. 

#1 You could in you measure get the latest date with sales data, derive year and month from this date, and filter your data table for including only months before that current month.
# 2 You can add a column to your dimDate table that marks current and previous months and then in the measure you can use this column to only include previous months. This is what I'm gonna show you.

 

Add this as a calculated column to you dimDate Table:

 

Running Month = DATEDIFF ( TODAY(), 'dimDate'[Date], MONTH )

 

This will give you a column that contains 0 for the current month, -1, -2, -3, ... for past months and 1, 2, 3, ... for future month. Instead of using TODAY() in the code, you can alternatively relate to your last date with sales as today if there is some delay in your data processing pipeline until sales data actually arrives in your report.
Then wrap you code into a CALCULATE() function that exludes the current (and future) months:

 

CALCULATE (

    VAR MaxSales = MAXX ( ALLSELECTED ( dimDate[Year], dimDate[Month], DimDate[MMM] ), [Sum of Net Sales Value] ) 
    VAR MinSales = MINX ( ALLSELECTED ( dimDate[Year], dimDate[Month], dimDate[MMM] ), [Sum of Net Sales Value] ) 
    VAR Result = IF ( [Sum of Net Sales Value] = MaxSales || [Sum of Net Sales Value] = MinSales, 
    [Sum of Net Sales Value],
     " " ) 
    RETURN Result

    ,KEEPFILTERS( 'dimDate'[Running Month] < 0 )
)

 

 BR

Martin

github.pnglinkedin.png

Thank you man, you are a genius 🙂

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors