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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
vickychill
Frequent Visitor

Calculate count in Matrix visual in PowerBI Desktop

Hello,

I have a matrix visual which looks something like below on left. Category and Fruit/Veg are rows, names is column while "First Fruit/Veg" is values. I want to create a count column (like in the right) which will provide me with count of each fruit/veg is being consumed as below. Also I have filters on columns (Names), so if we can change the count as filter changes that would be great.

Any help would be appreaciated.

Thanks

vickychill_0-1731592239703.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @vickychill 

Thanks for the reply from Bibiano_Geraldo. You can refer to the following methods to achieve your need.

Method 1:

vyaningymsft_1-1732863700566.png

 

And then modify the Subtotal label in the Column subtotals.

vyaningymsft_2-1732863864338.png

 

 

Counts = 
VAR _FruitVeg = MAX('Table'[Fruit/Veg])
VAR _counts = COUNT('Table'[Fruit/Veg])
RETURN
IF(ISINSCOPE('Table'[Name]),_FruitVeg,_counts)

 

 


Method 2:

vyaningymsft_6-1732865533707.png
Create a dim table in Power Query:

vyaningymsft_7-1732865579690.png

 

let
    Source = Table,
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Name"}),
    #"Removed Duplicates" = List.Combine( {{"Count"},Table.Distinct(#"Removed Other Columns")[Name]}),
    #"Converted to Table" = Table.FromList(#"Removed Duplicates", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Index" = Table.AddIndexColumn(#"Converted to Table", "Index", 0, 1, Int64.Type)
in
    #"Added Index"

 

Then use this measure:

 

Measure = 
VAR __cur_column = SELECTEDVALUE('Table (2)'[Column1])
VAR __result = IF( __cur_column<> "Count", CALCULATE(MAX('Table'[Fruit/Veg]),'Table'[Name]=__cur_column), CALCULATE(COUNTROWS('Table')))
RETURN
    __result

 


Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi, @vickychill 

Thanks for the reply from Bibiano_Geraldo. You can refer to the following methods to achieve your need.

Method 1:

vyaningymsft_1-1732863700566.png

 

And then modify the Subtotal label in the Column subtotals.

vyaningymsft_2-1732863864338.png

 

 

Counts = 
VAR _FruitVeg = MAX('Table'[Fruit/Veg])
VAR _counts = COUNT('Table'[Fruit/Veg])
RETURN
IF(ISINSCOPE('Table'[Name]),_FruitVeg,_counts)

 

 


Method 2:

vyaningymsft_6-1732865533707.png
Create a dim table in Power Query:

vyaningymsft_7-1732865579690.png

 

let
    Source = Table,
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Name"}),
    #"Removed Duplicates" = List.Combine( {{"Count"},Table.Distinct(#"Removed Other Columns")[Name]}),
    #"Converted to Table" = Table.FromList(#"Removed Duplicates", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Index" = Table.AddIndexColumn(#"Converted to Table", "Index", 0, 1, Int64.Type)
in
    #"Added Index"

 

Then use this measure:

 

Measure = 
VAR __cur_column = SELECTEDVALUE('Table (2)'[Column1])
VAR __result = IF( __cur_column<> "Count", CALCULATE(MAX('Table'[Fruit/Veg]),'Table'[Name]=__cur_column), CALCULATE(COUNTROWS('Table')))
RETURN
    __result

 


Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

vickychill
Frequent Visitor

Thanks Bibiano_Geraldo.

Sorry I should have mentioned this before but I have around 50 names in my Name column and do not want to add all of them togther like you did. Any different way? Below is how table looks like FYI

vickychill_0-1731595887553.png

I modified your formula which gives me the answer but not in format I want.

I changed it like:

Measure =
VAR fruitVeg = SELECTEDVALUE('Table'[Fruit /Veg])
VAR result =
CALCULATE(
    COUNT('Table'[Name]),
    'Table'[Fruit /Veg] = fruitVeg
)
RETURN
result
 which gives me:
vickychill_2-1731596178938.png

 


I am looking for only "Total measure" column and also in the front if possible, all other columns are just unecessary.

let me know if there are anysuggestions

 

Its clear now, but the desired result in your Matrix will be not reached, due there's no native way to hide column and keep show in total field, but you can keep your data to something link this:

Bibiano_Geraldo_0-1731605450665.png

 

Bibiano_Geraldo
Super User
Super User

Hi, @vickychill ,

Create a measure using this DAX:

Measure = 
VAR fruitVeg = SELECTEDVALUE(Sheet1[Fruit/Veg])

VAR result = 
CALCULATE(
    COUNT(Sheet1[John]) + COUNT(Sheet1[Dave]) + COUNT(Sheet1[harry]) + COUNT(Sheet1[Tom]),
    Sheet1[Fruit/Veg] = fruitVeg
)

RETURN
result

 

Your result should now look like this:

 

Bibiano_Geraldo_0-1731594496193.png

 

Note: Make sure to replace column and table names with your owns.

 

I hope this help you, if yes, please accept as solution and give a Kudo.

 

Thank you

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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