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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Max function does not work when using slicer?

The measure below works fine below until I select the year in a slicer.  The years in my app are 2014 - 2017 and the expression below correctly assumes max year -1 (2016) when nothing is selected, but when I select 2017 it goes blank.

 

The goal is if I select 2017 in the slicer then expression below counts for year 2016 and so on...

 

CountHeadcountPriorYear =
calculate(COUNTROWS(LnkAssociatesPeriod),FILTER(LnkAssociatesPeriod,LnkAssociatesPeriod[Period_Year]=Max(LnkAssociatesPeriod[Period_Year])-1),LnkAssociatesPeriod[AssociatesATSkey])

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@Anonymous,

I create two new measures using the following DAX , both work well. Could you please post a screenshot about your scenario and share sample data of your table here?

CountHeadcountPriorYear =
calculate(COUNTROWS(LnkAssociatesPeriod),FILTER(ALL(LnkAssociatesPeriod),LnkAssociatesPeriod[Period_Year]=Max(LnkAssociatesPeriod[Period_Year])-1),LnkAssociatesPeriod[AssociatesATSkey])
CountHeadcountPriorYear =VAR targetyear = Max(LnkAssociatesPeriod[Period_Year])-1
RETURN CALCULATE(COUNTROWS(LnkAssociatesPeriod),FILTER(ALL(LnkAssociatesPeriod),LnkAssociatesPeriod[Period_Year]=targetyear),LnkAssociatesPeriod[AssociatesATSkey])



Regards,
Lydia

View solution in original post

4 REPLIES 4
KHorseman
Community Champion
Community Champion

If you select a year in the slicer, the entire table is filtered to that year. That year - 1 is excluded by your slicer selection. You would need to remove the filter context from the measure using ALL.

 

CountHeadcountPriorYear =
calculate(COUNTROWS(LnkAssociatesPeriod),FILTER(ALL(LnkAssociatesPeriod),LnkAssociatesPeriod[Period_Year]=Max(LnkAssociatesPeriod[Period_Year])-1),LnkAssociatesPeriod[AssociatesATSkey])

 

...should work, but to be safe I would use a variable to keep the slicer selection separate from your filter statement...

 

CountHeadcountPriorYear =VAR targetyear = Max(LnkAssociatesPeriod[Period_Year])-1
RETURN CALCULATE(COUNTROWS(LnkAssociatesPeriod),FILTER(LnkAssociatesPeriod,LnkAssociatesPeriod[Period_Year]=targetyear),LnkAssociatesPeriod[AssociatesATSkey])





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Thanks so much for the help.

 

I used your expression but now I am getting an error that says "the following syntax error occurred during parsing; invalid token , line 2, offset 57". 

 

Z

Anonymous
Not applicable

@Anonymous,

I create two new measures using the following DAX , both work well. Could you please post a screenshot about your scenario and share sample data of your table here?

CountHeadcountPriorYear =
calculate(COUNTROWS(LnkAssociatesPeriod),FILTER(ALL(LnkAssociatesPeriod),LnkAssociatesPeriod[Period_Year]=Max(LnkAssociatesPeriod[Period_Year])-1),LnkAssociatesPeriod[AssociatesATSkey])
CountHeadcountPriorYear =VAR targetyear = Max(LnkAssociatesPeriod[Period_Year])-1
RETURN CALCULATE(COUNTROWS(LnkAssociatesPeriod),FILTER(ALL(LnkAssociatesPeriod),LnkAssociatesPeriod[Period_Year]=targetyear),LnkAssociatesPeriod[AssociatesATSkey])



Regards,
Lydia

Anonymous
Not applicable

Thanks - it worked!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.