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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
waisal02
Regular Visitor

Calculate Month to Date Variance based on the Selected Month in Slicer

Hi! I'm seeking help from the experts on dax coding to calculate Month-to-Date variance based on the selected month in a month slicer.

 

I have created a running total variance and difference chart based on the selected months, Max Month, and Min Month. The variance and difference work perfectly if both are past months or full months.

 

However, if the selected months include the current month, I would like the variance and difference to reflect the Month-to-Date values for both months. The desired result is illustrated in Image 2.

Current date = 10 Aug 2024

 

Image A: Both Selected Past Month

Full Month.JPG

 

Image B: Selection Including Current Month

Desire.JPG

Below are my measure dax coding:

Max Month Count =
Calculate(sum('MOM'[Count]), filter(ALL('MOM'[Date].[Month]), 'MOM'[Date].[Month]=max('MOM'[Date].[Month])))
 
Min Month Count =
Calculate(sum('MOM'[Count]), filter(ALL('MOM'[Date].[Month]), 'MOM'[Date].[Month]=min('MOM'[Date].[Month])))
 
Diff = 'MOM'[Max Month Count]-'MOM'[Min Month Count]
 
Variance = DIVIDE('MOM'[Max Month Count]-'MOM'[Min Month Count], 'MOM'[Min Month Count])
1 ACCEPTED SOLUTION
v-xianjtan-msft
Community Support
Community Support

Hi @waisal02 

 

May I ask if your problem has been solved? If not, I hope my solution was helpful to you.
1. I created the following measures in the test file you provided:

_Max Month Count =
VAR MaxDate = MAX(MOM[Date])
VAR MaxMonth = MONTH(MaxDate)
VAR MaxYear = YEAR(MaxDate)
VAR MaxMonthStart = DATE(MaxYear, MaxMonth, 1)
VAR MaxMonthEnd = IF(
MaxMonth = MONTH(TODAY()) && MaxYear = YEAR(TODAY()),
MAX(MOM[Date]),
EOMONTH(MaxMonthStart, 0)
)
RETURN
CALCULATE(
SUM(MOM[Count]),
FILTER(
MOM,
MOM[Date] >= MaxMonthStart &&
MOM[Date] <= MaxMonthEnd
)
)
_Min Month Count =
VAR MaxDate = MAX(MOM[Date])
VAR MinDate = MIN(MOM[Date])
VAR MinMonth = MONTH(MinDate)
VAR MinYear = YEAR(MinDate)
VAR MaxDayInMonth = DAY(MaxDate)
VAR MinMonthStart = DATE(MinYear, MinMonth, 1)
VAR MinMonthEnd = DATE(MinYear, MinMonth, MaxDayInMonth)
RETURN
CALCULATE(
SUM(MOM[Count]),
FILTER(
MOM,
MOM[Date] >= MinMonthStart &&
MOM[Date] <= MinMonthEnd
)
)
_Diff = MOM[_Max Month Count] - MOM[_Min Month Count]
_Variance = DIVIDE(MOM[_Max Month Count] - MOM[_Min Month Count], MOM[_Min Month Count])

 

2. Here is my final result:
The variance and difference work perfectly when both are full months.

1.png

However, if the selected months include the current month, need you to filter to the current date in the slicer, which ensures that the selected full month is based on the same date range as the current month calculation.

2.png

 

Best Regards,

Jarvis Tang

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

4 REPLIES 4
v-xianjtan-msft
Community Support
Community Support

Hi @waisal02 

 

May I ask if your problem has been solved? If not, I hope my solution was helpful to you.
1. I created the following measures in the test file you provided:

_Max Month Count =
VAR MaxDate = MAX(MOM[Date])
VAR MaxMonth = MONTH(MaxDate)
VAR MaxYear = YEAR(MaxDate)
VAR MaxMonthStart = DATE(MaxYear, MaxMonth, 1)
VAR MaxMonthEnd = IF(
MaxMonth = MONTH(TODAY()) && MaxYear = YEAR(TODAY()),
MAX(MOM[Date]),
EOMONTH(MaxMonthStart, 0)
)
RETURN
CALCULATE(
SUM(MOM[Count]),
FILTER(
MOM,
MOM[Date] >= MaxMonthStart &&
MOM[Date] <= MaxMonthEnd
)
)
_Min Month Count =
VAR MaxDate = MAX(MOM[Date])
VAR MinDate = MIN(MOM[Date])
VAR MinMonth = MONTH(MinDate)
VAR MinYear = YEAR(MinDate)
VAR MaxDayInMonth = DAY(MaxDate)
VAR MinMonthStart = DATE(MinYear, MinMonth, 1)
VAR MinMonthEnd = DATE(MinYear, MinMonth, MaxDayInMonth)
RETURN
CALCULATE(
SUM(MOM[Count]),
FILTER(
MOM,
MOM[Date] >= MinMonthStart &&
MOM[Date] <= MinMonthEnd
)
)
_Diff = MOM[_Max Month Count] - MOM[_Min Month Count]
_Variance = DIVIDE(MOM[_Max Month Count] - MOM[_Min Month Count], MOM[_Min Month Count])

 

2. Here is my final result:
The variance and difference work perfectly when both are full months.

1.png

However, if the selected months include the current month, need you to filter to the current date in the slicer, which ensures that the selected full month is based on the same date range as the current month calculation.

2.png

 

Best Regards,

Jarvis Tang

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

Rakesh1705
Resolver III
Resolver III

Preapared a Source Data for 01st Jan'24 to 31st Dec'24 with random values

Rakesh1705_0-1723356188186.png

Calculation of Month

Rakesh1705_1-1723356256093.png

Calculation of Day

Rakesh1705_2-1723356274571.png

Running total for current Month

Rakesh1705_3-1723356298469.png

Running total for Previous Month

Rakesh1705_4-1723356327408.png

Data Date Calculation

Rakesh1705_5-1723356363549.png

max date of current month

Rakesh1705_6-1723356423058.png

Previous Month Same Date

Rakesh1705_7-1723356448780.png

Previous month max date

Rakesh1705_8-1723356476725.png

Previous month Min date

Rakesh1705_9-1723356499820.png

Current month running total is modified with condition

Rakesh1705_10-1723356547783.png

Previous month running total is modified with previous months dates condition

Rakesh1705_11-1723356597125.png

Current month final total

Rakesh1705_12-1723356632216.png

Previous month same day final total

Rakesh1705_13-1723356655008.png

Variance is calculated

Rakesh1705_14-1723356680996.png

Present view is for current month and its previous month

Rakesh1705_15-1723356728834.png

View for Jun and Jul

Rakesh1705_16-1723356757319.png

If this method is serving your purpose then please accept the same as your solution

tharunkumarRTK
Super User
Super User

[Max Month Count] = 
Var __IsCurrentMonth = calculate( max('MOM'[Date].[Month]) allselected('mom')) = month(today()) Var result = if (__IsCurrentMonth, Calculate(sum('MOM'[Count]), filter(ALL('MOM'), 'MOM'[Date].[Month]=max('MOM'[Date].[Month]) && 'MOM'[DayNum] <= day(today()) )) , Calculate(sum('MOM'[Count]), filter(ALL('MOM'[Date].[Month]), 'MOM'[Date].[Month]=max('MOM'[Date].[Month]))) ) 
return result 

[Min Month Count] =
Var __IsCurrentMonth = calculate( max('MOM'[Date].[Month]) allselected('mom')) = month(today()) = month(today()) 
Var result = if (__IsCurrentMonth, Calculate(sum('MOM'[Count]), filter(ALL('MOM'), 'MOM'[Date].[Month]=min('MOM'[Date].[Month]) && 'MOM'[DayNum] <= day(today()) )) , Calculate(sum('MOM'[Count]), filter(ALL('MOM'[Date].[Month]), 'MOM'[Date].[Month]=min('MOM'[Date].[Month]))) )
return result 

 

you can modify the above two measures and it should solve your problem. 

 

 

Need a Power BI Consultation? Hire me on Upwork

 

Connect on LinkedIn

 




Did I answer your question? Mark my post as a solution!
If I helped you, click on the Thumbs Up to give Kudos.

Proud to be a Super User!

PBI_SuperUser_Rank@2x.png

@waisal02

Hi! @tharunkumarRTK,
Thanks for your great help. Your dax coding provided should be work, for for some reason, i am bumped into some formating error.
Error.JPG

I am hereby enclosed a test file and test data, hope you can assist.

MOM-TEST.pbix 

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!

December 2024

A Year in Review - December 2024

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