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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Chris2016
Resolver I
Resolver I

Measure to count the distinct number of months in a date table

Hi,

 

Could someone help with a measure to count the distinct number of months in a date column (not coming from a Date table)? I need this to calculate an average per month. I know that I could just create a calculated column of Y&Mo from Date, but the report is already heavy and don't want to burden it unnecessarily.

I already tried using DATEDIFF, but I don't think that will work properly if I start missing months in the Date column going forward (e.g. the current month of May is almost over, and I still do not have any data for this month - I expect it to be blank starting June 1st, and the calculation will be off when I start getting data in June).

NoofMo = CALCULATE(DATEDIFF( CALCULATE( MIN('Table'[Date]), ALLEXCEPT('Table', 'Table'[Date])), CALCULATE( MAX('Table'[Date]), ALLEXCEPT('Table', 'Table'[Date])), MONTH)+1, FILTER('Table',NOT(ISBLANK([Date]))))

Though this calculation works for now, I just need an actual distinct count of months in the Date column.
Many thanks for any idea!

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

Here's a measure expression that shows one way to do it. Although, I would consider adding a proper date table or yearmonth column and optimize your model in other areas.

MonthsInColumn =
VAR dateswithmonth =
    ADDCOLUMNS (
        DISTINCT ( Sales[SaleDate] ),
        "cMon", EOMONTH ( Sales[SaleDate], 0 )
    )
RETURN
    COUNTROWS ( SUMMARIZE ( dateswithmonth, [cMon] ) )

Pat

Microsoft Employee

View solution in original post

2 REPLIES 2
ppm1
Solution Sage
Solution Sage

Here's a measure expression that shows one way to do it. Although, I would consider adding a proper date table or yearmonth column and optimize your model in other areas.

MonthsInColumn =
VAR dateswithmonth =
    ADDCOLUMNS (
        DISTINCT ( Sales[SaleDate] ),
        "cMon", EOMONTH ( Sales[SaleDate], 0 )
    )
RETURN
    COUNTROWS ( SUMMARIZE ( dateswithmonth, [cMon] ) )

Pat

Microsoft Employee

Thank you, Pat, that worked very well.

Many thanks for your help!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.