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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
PBI_newuser
Post Prodigy
Post Prodigy

Calculate Month Diff between selected date and min date

Hi, I would like to calculate the month difference between the selected date and min available date.

For example, the min date of this dataset is 2023-01, so MonthDiff is the expected result. How to create MonthDiff measure?

MonthMonthDiff
2023-011
2023-022
2023-033
2023-044
2023-055

 

PBI_newuser_0-1708055123325.png

 

1 ACCEPTED SOLUTION
Ritaf1983
Super User
Super User

Hi @PBI_newuser 
You can use this type of formula modified by your parameters:

month_diff =
var selected_date = SELECTEDVALUE('Calendar'[Date])
var min_order_date = CALCULATE(min('orders'[Order Date]),ALL('Calendar'))
RETURN
DATEDIFF(min_order_date,selected_date,MONTH)

Ritaf1983_0-1708056834270.png

pbix is attached

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

 

View solution in original post

2 REPLIES 2
RossEdwards
Solution Specialist
Solution Specialist

Is there a relationship between the DATES table and the Inventory table?  If so, the problem is likely to be the filtering context being applied.

You could get around this problem using variables like in the DATEDIFF example linked below and using a CALCULATE statement and an ALL(DATES) i.e. var EndDate =  CALCULATE(MAX(Inventory[Date]), ALL(DATES))
DATEDIFF function (DAX) - DAX | Microsoft Learn

Ritaf1983
Super User
Super User

Hi @PBI_newuser 
You can use this type of formula modified by your parameters:

month_diff =
var selected_date = SELECTEDVALUE('Calendar'[Date])
var min_order_date = CALCULATE(min('orders'[Order Date]),ALL('Calendar'))
RETURN
DATEDIFF(min_order_date,selected_date,MONTH)

Ritaf1983_0-1708056834270.png

pbix is attached

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.