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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
damit23183
Microsoft Employee
Microsoft Employee

Caculate Last N Month by using Slicer

Hi,

 

I am trying to calculate count by using slicer which means when i select monthyear and put number in parameter slicer than i should get value in bar chart.

For Example, Slicer is created base on Month so if put number 5 in slicer (Which is parameter created for Month) and then select Month from another slicer (e.g September 2020) then I would like to see data for last 5 months from today.

 

Please see screen shot below What I am trying to achieve here,

 

In screen shot below, when i choose "CY MONT-YEAR" = September - 2020 (Simple FIlter based on Month-Year from Date Table)

and "SINCE LAST 5 MONTHS" (This is another slicer = 5 from Parameter) then i would like to see data for last 5 months in barchart from September - 2020.

 

Table.PNG

 

 

To calculate count for Last N Months, this is DAX I have used but not working;

 

CALCULATE(DISTINCTCOUNT(ID), DATESINPERIOD('Date'[Date],MAX('Date'[Date]), -[N Value], MONTH))

 

ID = Counting ID

Date[Date] = Date column from Date Table

N Value = Parameter value (1-24)

 

Thanks

1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @damit23183 ,

I have created a simliar sample file in the below that you can check, hopes it could help you in some ways.

Create a new year-month column both in date table and acutal table:

YearMonth = YEAR([Date])*100+MONTH([Date])

Create a measure like this, put it in the visual filter and set its value as 1:

Measure =
VAR tab =
    TOPN (
        SELECTEDVALUE ( Parameter[Parameter] ),
        CALCULATETABLE (
            DISTINCT ( 'Table'[YearMonth] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[YearMonth] <= SELECTEDVALUE ( 'Date'[YearMonth] )
            )
        ),
        [YearMonth]
    )
RETURN
    IF ( SELECTEDVALUE ( 'Table'[YearMonth] ) IN tab, 1, 0 )

 

Best Regards,
Yingjie 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

3 REPLIES 3
v-yingjl
Community Support
Community Support

Hi @damit23183 ,

I have created a simliar sample file in the below that you can check, hopes it could help you in some ways.

Create a new year-month column both in date table and acutal table:

YearMonth = YEAR([Date])*100+MONTH([Date])

Create a measure like this, put it in the visual filter and set its value as 1:

Measure =
VAR tab =
    TOPN (
        SELECTEDVALUE ( Parameter[Parameter] ),
        CALCULATETABLE (
            DISTINCT ( 'Table'[YearMonth] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[YearMonth] <= SELECTEDVALUE ( 'Date'[YearMonth] )
            )
        ),
        [YearMonth]
    )
RETURN
    IF ( SELECTEDVALUE ( 'Table'[YearMonth] ) IN tab, 1, 0 )

 

Best Regards,
Yingjie Li

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

amitchandak
Super User
Super User

@damit23183 , You formula seem correct, You try this version

 

Rolling N =
var _max = MAXX(allselcted('Date'),'Date'[Date ])
return
CALCULATE(DISTINCTCOUNT(ID),DATESINPERIOD('Date'[Date ],_max,-1 * [N Value],MONTH))

 

Yours is also correct
Rolling N = CALCULATE(DISTINCTCOUNT(ID),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-1 * [N Value],MONTH))

 

But both will not give last 5-month trend, They will give you rolling 5 months. for trend refer this: https://www.youtube.com/watch?v=duMSovyosXE

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi Amit,

THanks for your response.

I have tried both your formula as well as formula from you tube channel.

 

Formula looks like working but visulization is only showing one month data every time whenever i choose or select specific month number.

 

Not sure, whether if there is any other setting to see for more columns in bar chart because I have been only able to see single chart so far. This is the case with all months for example if i select January 2020 then its only shows January 2020 data instead of other months data.

 

Please see below screen shot how does bar chart looks like,

Table.PNG

 

NOTE: I have tried to change month from slicer for many times and result is only one column instead more than one column.

 

Thanks

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors