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
katyfailoo
Advocate I
Advocate I

Calculation Excluding Months if date for the upcoming Month is not present

Hi All,

 

I am struggling with a calculation. Hoping to get some guidance around this

 

I have a matrix as seen below that pulls from the following tables: Calendar and Employees. Columns used are

  • Calendar - Month
  • Calendar - Year
  • Employees - Employee Count (Measure)
  • Employee - Category

 

 Month      
YearJanuaryFebruaryMarchAprilMayJune July
2018108610121010
Category A5435655
Category B5435655
201920304050402020
202020304050701010
202120304020   

 

Question, how can I add all the values for all the months and years from January - April. However I don't want to include May- July because I don't have the data for 2021 for those months. If data is not found for the current year and that month do not add those values (exclude those months for the previous years too).

 

I tried using a a flag to flag the months that are less than the current month, but it didn't seem to work.

 

Your help and guidance is appreciated.

 

Thank you!

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @katyfailoo 

 

I think you original idea to use a flag is doable. You could add a calculated column to the Employee table with below code. 

flag =
VAR vMaxMonth = MONTH ( MAX ( 'Employee'[Date] ) )
RETURN
    IF ( MONTH ( 'Employee'[Date] ) <= vMaxMonth, 1, 0 )

 

Then create a measure

Employee Count New = CALCULATE ( [Employee Count], 'Employee'[flag] = 1 )

 

Use this new measure in the matrix. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

3 REPLIES 3
v-jingzhang
Community Support
Community Support

Hi @katyfailoo 

 

I think you original idea to use a flag is doable. You could add a calculated column to the Employee table with below code. 

flag =
VAR vMaxMonth = MONTH ( MAX ( 'Employee'[Date] ) )
RETURN
    IF ( MONTH ( 'Employee'[Date] ) <= vMaxMonth, 1, 0 )

 

Then create a measure

Employee Count New = CALCULATE ( [Employee Count], 'Employee'[flag] = 1 )

 

Use this new measure in the matrix. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

amitchandak
Super User
Super User

@katyfailoo , Assume you are using a measure , M1

 

Then try a measure like

M1 New =

var _max = maxx(allselcted(employee_, employee[Date])

return

calculate([M1], filter(calendar, Calendar[Date] <=_max))

Thank you so much for the speedy response. It did not work for me. It doesn't filter out May, June and July. Is there another way I can get to filter out the sum of May June and July for years (2018, 2019, and 2020) because 2021 does not have any values in those months?

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.