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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Distinct count of dimension values in matrix

Hi, I have a matrix that contains products on the rows, (1 or 2) years on the columns and 2 measures.

I'm trying to achieve that measure 2 is only shown if there are 2 years in de matrix. (it is always 1 or 2)

Therefore I'm trying to create a variable that I can use to count the number of years in the matrix.

I've tried COUNTROWS(ALLSELECTED('Table'[Year])), but then I get a count of all years in the dimension, instead of only the years in the matrix.

I've tried CALCULATE(SUMX(VALUES('Table'[Year]),1)) but then it always counts 1 because it uses the context of the year of that particular column in the table.

Can anyone help?

Best regards!

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

The problem with this is the context of the year based on the slice of the product, since you are not directly filtering the year but you are showing the years were there we sales values, you need to calculate this on a different way:

NumberOfYears = COUNTROWS(ALLSELECTED(Sales[YearId]))

MFelix_0-1730991509275.png

 

This raises a problem that is the fact that even for the year that don't have sales you have values so you can redo it like this:

NumberOfYears = IF(SUM(Sales[Sales]) <> BLANK(), COUNTROWS(ALLSELECTED(Sales[YearId])))

MFelix_1-1730991671909.png

On another way if you have the slicer or a filter on visual level to force the year then you can use your original metric:

 

MFelix_2-1730991772393.png

 

So this is a question about the context in your case the year context is not getting directly picked up and you are always getting 6 because you are not forcing the years to be filtered on the Years column itself.

 

File with both options attached.

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português





View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Here is the example pbx. The measure is called "NumberOfYears"; see what happens if you add it to the matrix

Share

MFelix
Super User
Super User

Hi  @Anonymous ,

 

The first syntax you used should get the correct result but this may depend on the way you are getting the data filter on the matrix is it based on a filter on the visual, a slicer?

 

Is the table where the column year is being filtered the same one you use for your calculations?

 

If possible can you please share a mockup data or sample of your PBIX file. You can use a onedrive, google drive, we transfer or similar link to upload your files.

If the information is sensitive please share it trough private message.


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português





Anonymous
Not applicable

Here is the example pbx. The measure is called "NumberOfYears"; see what happens if you add it to the matrix

Share

Hi @Anonymous ,

 

The problem with this is the context of the year based on the slice of the product, since you are not directly filtering the year but you are showing the years were there we sales values, you need to calculate this on a different way:

NumberOfYears = COUNTROWS(ALLSELECTED(Sales[YearId]))

MFelix_0-1730991509275.png

 

This raises a problem that is the fact that even for the year that don't have sales you have values so you can redo it like this:

NumberOfYears = IF(SUM(Sales[Sales]) <> BLANK(), COUNTROWS(ALLSELECTED(Sales[YearId])))

MFelix_1-1730991671909.png

On another way if you have the slicer or a filter on visual level to force the year then you can use your original metric:

 

MFelix_2-1730991772393.png

 

So this is a question about the context in your case the year context is not getting directly picked up and you are always getting 6 because you are not forcing the years to be filtered on the Years column itself.

 

File with both options attached.

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português





Anonymous
Not applicable

Hi Miguel Félix, I'll try to get you a mock up example. But in the mean time, to answer your question: yes, the matrix is filtered by a slicer. And yes the table to filter the matrix is the one used in the dax calculation.

Thanks for your quick response!

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.