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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
PowerToBe
Frequent Visitor

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 @PowerToBe ,

 

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
PowerToBe
Frequent Visitor

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  @PowerToBe ,

 

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



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

Share

Hi @PowerToBe ,

 

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



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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors