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! Request now

Reply
bml123
Post Patron
Post Patron

Get value of maximum date of month selected in the slicer and subtract maximum value of next month

Hi,

 

I have this data in the table as below

 

IdDateAmount
101/04/2021100
110/04/202150
120/05/202175
125/05/2021120

 

In the slicer, I have calendar date in month and year format, for eg if I choose April 2021

I need to get the maxumum value of the month selected in the slicer which is April and the maximum date of April in the table is 10/04/2021 and the value of that is 50.

I also need to get the maximum value of the next month selected in the slicer which is May and the maximum date of May in the table is 25/05/2021 and value of that is 120

So, the final value should be 120-50=70 for id=1. How do I achieve that?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @bml123,

Please break the relationship from the calendar table to the fact table to let the slicer works as a selector instead of a filter, then you can try to use the following measure formula to achieve your requirement:

measure =
VAR selected =
    MAX ( 'Calendar'[Date] )
VAR cMonth =
    CALCULATE (
        MAX ( Table[Date] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Date] ) = YEAR ( selected )
                && MONTH ( [Date] ) = MONTH ( selected )
        ),
        VALUES ( Table[ID] )
    )
VAR nMonth =
    CALCULATE (
        MAX ( Table[Date] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Date] ) = YEAR ( selected )
                && MONTH ( [Date] )
                    = MONTH ( selected ) + 1
        ),
        VALUES ( Table[ID] )
    )
RETURN
    CALCULATE (
        MAX ( Table[Amount] ),
        FILTER ( ALLSELECTED ( Table ), [Date] = nMonth ),
        VALUES ( Table[ID] )
    )
        - CALCULATE (
            MAX ( Table[Amount] ),
            FILTER ( ALLSELECTED ( Table ), [Date] = cMonth ),
            VALUES ( Table[ID] )
        )

Regards,

Xiaoxin Sheng

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

HI @bml123,

Please break the relationship from the calendar table to the fact table to let the slicer works as a selector instead of a filter, then you can try to use the following measure formula to achieve your requirement:

measure =
VAR selected =
    MAX ( 'Calendar'[Date] )
VAR cMonth =
    CALCULATE (
        MAX ( Table[Date] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Date] ) = YEAR ( selected )
                && MONTH ( [Date] ) = MONTH ( selected )
        ),
        VALUES ( Table[ID] )
    )
VAR nMonth =
    CALCULATE (
        MAX ( Table[Date] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Date] ) = YEAR ( selected )
                && MONTH ( [Date] )
                    = MONTH ( selected ) + 1
        ),
        VALUES ( Table[ID] )
    )
RETURN
    CALCULATE (
        MAX ( Table[Amount] ),
        FILTER ( ALLSELECTED ( Table ), [Date] = nMonth ),
        VALUES ( Table[ID] )
    )
        - CALCULATE (
            MAX ( Table[Amount] ),
            FILTER ( ALLSELECTED ( Table ), [Date] = cMonth ),
            VALUES ( Table[ID] )
        )

Regards,

Xiaoxin Sheng

bml123
Post Patron
Post Patron

Hi @amitchandak,

 

I don't want the sum, I just want the value of the maximum date of the month selected for previous and next month. Also I need the value for each id.  How do I achieve that?

 

@bml123 , I have used lastnonblankvalue for that purpose. Idea is to make it generic. Also lastnonblankvalue need measure so I used sum. 

Second, if you use a date table it will handle taking max for each ID

 

Another option is , check the measure below

 

Measure =
var _min = maxx(allselected(Table), Table[Date])
var _max = maxx(filter(all(Table) , eomonth(table[Date],0) = eomonth(_min,1)), Table[Date])
return
calculate(sum(Table[Amount]), filter(all(table), Table[Date] = _max)) - calculate(sum(Table[Amount]), filter(all(table), Table[Date] = _min))

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
amitchandak
Super User
Super User

@bml123 , Use a date table joined with your date of the table, then try measures

 

This month =

calculate(lastnonblankvalue('Date'[Date], sum(Table[Amount])), datesmtd('Date'[Date]))

 

next month =

calculate(lastnonblankvalue('Date'[Date], sum(Table[Amount])), datesmtd(dateadd('Date'[Date],1,month)))

 

or

next month =

calculate(lastnonblankvalue('Date'[Date], sum(Table[Amount])), nextmonth('Date'[Date]))

 

diff = [Next month] -[This month]

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

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 @amitchandak 

 

I tried both your suggestions and i got very big numbers and are not right.

@bml123 , please find the file

Please check diff total, how to correct grand total, you might need that for current and next too

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

@bml123 , Updated file

 

Current and Next use time intelligence and total are wrong.

Current 1 and Next 1 do not use time intelligence and total are wrong.

The current total and Next total are built on current 1 and the next 1 and the totals are correct. they can be built on current and next

 

Diff total and diff 1 have correct totals.

 

Code for correct total is also important

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

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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