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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
sdm2211
Regular Visitor

How to count values that appear in two columns of same table when selected through Slicer

Hello everyone,

 

I have below table called "Matches"

 

Team 1Team 2
Chennai Spure KingsMumbai Indians
Delhi CapitalRajasthan Royal
Mumbai IndiansDelhi Capital
Rajasthan RoyalChennai Super Kings
Delhi CapitalMumbai Indians

 

I am adding a Slicer where there are season like 2008, 2009, 2010

i am adding a Slicer where there are teams like Chennai Super Kings, Mumbai Indians

Assume I have selected season 2010 in seasons slicer and selected Delhi Capital in team slicer then it should give the total count of matches played as 3, considering from Column Team 1 & Team 2.

 

I am not able to fetch this details. Please help me

 

Regards,

Sandeep Marathe

 

2 ACCEPTED SOLUTIONS
mark_endicott
Super User
Super User

@sdm2211 - I would take the following steps

 

Create a disconnected "Team" table with the DAX below - to do this, go to the "Modelling" tab and select "New Table" then enter this in to the formula bar:

 

Team = DISTINCT(UNION( VALUES('Table'[Team 1]), VALUES( 'Table'[Team 2] )))

 

You must ensure there is no releationship to your 1st table so delete this if one is created. 

 

Then you can create a DAX measure that adds the two counts from each column together, use this DAX:

 

VAR selected_team =
    SELECTEDVALUE ( Team[Team 1] )
VAR count_1 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 1] = selected_team )
VAR count_2 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 2] = selected_team )
RETURN
    count_1 + count_2

 

And then add the "Team 1" column from your new "Team" table to a slicer:

 

mark_endicott_0-1736251268055.png

 

If this works for you, please accept it as the solution to help others with the same challenge. 

 

 

 

View solution in original post

@sdm2211 - You didn't specify this as a requirement, I've ammended my DAX measure below for to account for someone selecting "All" for the teams - here it will now just count the number of matches. 

 

VAR selected_team =
    SELECTEDVALUE ( Team[Team 1] )
VAR count_1 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 1] = selected_team )
VAR count_2 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 2] = selected_team )
RETURN
    IF ( HASONEVALUE ( Team[Team 1] ), count_1 + count_2, COUNTROWS ( 'Table' ) )

 

Also re-attached PBIX file. 

View solution in original post

9 REPLIES 9
SantoshPothnak
Regular Visitor

@mark_endicottGreat solution! I've tested it as well, and it works perfectly. 😊 I've also created an explanation video to help others. Thanks again!

https://youtu.be/ErBA9wGYZMo

EricVieira
New Member

TotalMatchesPlayed =
VAR SelectedTeam = SELECTEDVALUE('Teams'[Team])
RETURN
CALCULATE(
COUNTROWS('Matches'),
FILTER(
'Matches',
'Matches'[Team 1] = SelectedTeam || 'Matches'[Team 2] = SelectedTeam
)
)



TotalMatchesPlayed =
VAR SelectedTeam = SELECTEDVALUE('Teams'[Team])
VAR SelectedSeason = SELECTEDVALUE('Seasons'[Season])
RETURN
CALCULATE(
COUNTROWS('Matches'),
FILTER(
'Matches',
('Matches'[Team 1] = SelectedTeam || 'Matches'[Team 2] = SelectedTeam) &&
'Matches'[Season] = SelectedSeason
)
)

mark_endicott
Super User
Super User

@sdm2211 - I would take the following steps

 

Create a disconnected "Team" table with the DAX below - to do this, go to the "Modelling" tab and select "New Table" then enter this in to the formula bar:

 

Team = DISTINCT(UNION( VALUES('Table'[Team 1]), VALUES( 'Table'[Team 2] )))

 

You must ensure there is no releationship to your 1st table so delete this if one is created. 

 

Then you can create a DAX measure that adds the two counts from each column together, use this DAX:

 

VAR selected_team =
    SELECTEDVALUE ( Team[Team 1] )
VAR count_1 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 1] = selected_team )
VAR count_2 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 2] = selected_team )
RETURN
    count_1 + count_2

 

And then add the "Team 1" column from your new "Team" table to a slicer:

 

mark_endicott_0-1736251268055.png

 

If this works for you, please accept it as the solution to help others with the same challenge. 

 

 

 

bhanu_gautam
Super User
Super User

@sdm2211 ,Create a new measure to count the matches played by the selected team in the selected season. Assuming you have a column named Season in your table, the DAX formula would look like this:

 

Total Matches Played =
VAR SelectedTeam = SELECTEDVALUE('Teams'[Team])
VAR SelectedSeason = SELECTEDVALUE('Seasons'[Season])
RETURN
CALCULATE(
COUNTROWS('Matches'),
FILTER(
'Matches',
('Matches'[Team 1] = SelectedTeam || 'Matches'[Team 2] = SelectedTeam) &&
'Matches'[Season] = SelectedSeason
)
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






@bhanu_gautam @EricVieira The Measure you gave is not working. I have created measure you gave but it is giving (BLANK)

 

sdm2211_0-1736258388117.png

my data model is as above

1st Relation is from table matches - id to table deliveries - match_id

2nd Relation is from Table Teams-Team Names to table matches-team1

@sdm2211 - Did you try my solution? My screenshot shows it works. 

 

Attached is the PBIX.

 

The reason the other measures will not work is because of your 2nd relationship. This is why my solution uses a disconnected "Team" table. 

@mark_endicott thanks a lot for your help, It is counting as per my expectation. I just observed when in team slicer when I select "ALL", total number of matches shows as (BLANK). But if selected single value in slicer then it is showing correctly. I am trying to attached pbix file here, but not allowing

@sdm2211 - You didn't specify this as a requirement, I've ammended my DAX measure below for to account for someone selecting "All" for the teams - here it will now just count the number of matches. 

 

VAR selected_team =
    SELECTEDVALUE ( Team[Team 1] )
VAR count_1 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 1] = selected_team )
VAR count_2 =
    CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Team 2] = selected_team )
RETURN
    IF ( HASONEVALUE ( Team[Team 1] ), count_1 + count_2, COUNTROWS ( 'Table' ) )

 

Also re-attached PBIX file. 

@mark_endicott You are a Magician 😀. That additional line made wonders 😀

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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.