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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Dicken
Continued Contributor
Continued Contributor

Count previous months

If i have  a  pivot or table  with Year / Month  ; 
I want to count the previous number of months  present so not just the month number  
as some do not have any data so are not present 

so if i had 
Jan sales 1

March sales 2 
May sales 3    

so the best i have so far is ; 

Count Previous Months:=VAR Myear =
    IF( HASONEVALUE( 'Calendar'[Year]),VALUES( 'Calendar'[Year]))
VAR Mmonth = 
    IF (HASONEVALUE( 'Calendar'[Month Number]), VALUES('Calendar'[Month Number]))
RETURN
CALCULATE (
    DISTINCTCOUNT ( 'Calendar'[Month Number]),   
      FILTER(ALL('Calendar'), 'Calendar'[Month Number]<= Mmonth && 'Calendar'[Year] = Myear))

so this counts the months regardless of whether there are 'sales' present 

Richard

3 ACCEPTED SOLUTIONS
krishnakanth240
Impactful Individual
Impactful Individual

Hi @Dicken 

 

Can you please try this measure

Count Previous Months=
CALCULATE(DISTINCTCOUNT('Calendar'[Month Number]),FILTER(ALL('Calendar'),
'Calendar'[Date]<=MAX('Calendar'[Date])),
KEEPFILTERS('Sales'))

View solution in original post

FreemanZ
Community Champion
Community Champion

hi @Dicken ,

 

try like:

Count Previous Months:=

VAR Myear =

    IF( 

     HASONEVALUE( 'Calendar'[Year]),

     VALUES( 'Calendar'[Year])

)

VAR Mmonth = 

    IF (HASONEVALUE( 'Calendar'[Month Number]), VALUES('Calendar'[Month Number]))

RETURN

CALCULATE (

    COUNTROWS ( 'sales'),   

      FILTER(ALL('Calendar'), 'Calendar'[Month Number]<= Mmonth && 'Calendar'[Year] = Myear))

View solution in original post

Thank you all, I'll work through all of them.

View solution in original post

4 REPLIES 4
FreemanZ
Community Champion
Community Champion

hi @Dicken ,

 

try like:

Count Previous Months:=

VAR Myear =

    IF( 

     HASONEVALUE( 'Calendar'[Year]),

     VALUES( 'Calendar'[Year])

)

VAR Mmonth = 

    IF (HASONEVALUE( 'Calendar'[Month Number]), VALUES('Calendar'[Month Number]))

RETURN

CALCULATE (

    COUNTROWS ( 'sales'),   

      FILTER(ALL('Calendar'), 'Calendar'[Month Number]<= Mmonth && 'Calendar'[Year] = Myear))

mizan2390
Advocate II
Advocate II

It may work.
Count Previous Months :=
CALCULATE(
DISTINCTCOUNT( 'Calendar'[Month Number] ),
FILTER(
ALL( 'Calendar' ),
'Calendar'[Date] <= MAX( 'Calendar'[Date] )
),
KEEPFILTERS( 'Sales' )
)

krishnakanth240
Impactful Individual
Impactful Individual

Hi @Dicken 

 

Can you please try this measure

Count Previous Months=
CALCULATE(DISTINCTCOUNT('Calendar'[Month Number]),FILTER(ALL('Calendar'),
'Calendar'[Date]<=MAX('Calendar'[Date])),
KEEPFILTERS('Sales'))

Thank you all, I'll work through all of them.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.