cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Frequent Visitor

## Dax query

I have a table named (data) and two columns team1 and team2

want to undersand

1. count number of maches played by each team and
2. count of wins by each team

input data

 team1 team2 winner a c a a b b b a b b c c c a a c b b d a a a d d

Ouput data:

 Team matches wins a 6 3 b 4 3 c 4 1 d 2 1
1 ACCEPTED SOLUTION
Super User

Hi @amitgajkal2007
The first step is to transform the table to vertical to make the analysis easier.

You can do it with the "unpivot" option of PQ

Then you will get the table as in the picture :

From here you can create 2 simple dax measures :

1. For matches :

matches = COUNTROWS('Table')
2. For wins:
wins = CALCULATE([matches],SELECTEDVALUE('Table'[Team])='Table'[winner])
Result :

pbix is attached, you can follow my steps at pq and dax.

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly

6 REPLIES 6
Solution Supplier

Hello @amitgajkal2007

As per your question has stated "Dax Query",

Below is the complete DAX code without doing any manipulation in PQ.

Step : Create a Calculated Table for Supporting purpose. Below is the code.

SupportingTable =
UNION(
SELECTCOLUMNS(
'Table',"Teams",'Table'[team1]
),
SELECTCOLUMNS(
'Table',"Teams",'Table'[team2]
) )

Step : Use the below code for your output in DAX studio or DAX query view in Power BI desktop

EVALUATE
VAR _2 =
SELECTCOLUMNS(
SUMMARIZE(
UNION(
SUMMARIZECOLUMNS( 'Table'[team1] ),
SUMMARIZECOLUMNS( 'Table'[team2]
)
), 'Table'[team1]
), "@Teams",'Table'[team1] )
VAR _3 =
_2,"MatchCount"
CALCULATE( COUNT( SupportingTable[Teams] ), FILTER( SupportingTable, SupportingTable[Teams] = [@Teams] ) ),
"Wins",CALCULATE( COUNT('Table'[winner] ), FILTER( 'Table','Table'[winner] = [@Teams] ) )
)
RETURN
_3

Below is the screenshot

Regards

sanalytics

Frequent Visitor

I am trying to use above (mentioned in DAX query) code in a measure but i am not able to get it.

This is a reference table which i have build...actual tbale has 4 table, like 20 columns and realted with each other.

I am not able to use unpivot option if i use it then it create havoc in other dashboards (the slicer simply doesnt filter).

thank you but if you can let me know how i can use it above query in Dax measure then i will use the analogy on actual data.

Thank you

Super User

Hi @amitgajkal2007
The first step is to transform the table to vertical to make the analysis easier.

You can do it with the "unpivot" option of PQ

Then you will get the table as in the picture :

From here you can create 2 simple dax measures :

1. For matches :

matches = COUNTROWS('Table')
2. For wins:
wins = CALCULATE([matches],SELECTEDVALUE('Table'[Team])='Table'[winner])
Result :

pbix is attached, you can follow my steps at pq and dax.

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly

Frequent Visitor

Thats good one....i spent hours to figure it out using DAX. Prbolem with this is that it forms many to many relationship that is difficult to handle
Is there a solution using DAX? (Just Curious)

Super User

Hi @amitgajkal2007
Happy to help 🙂 Why many to many,
it is just 1 table..
According to DAX, I see that @sanalytics gave a solution.

Frequent Visitor

Unpivoting creates a havoc on other reports. the slicers simply stop to work and doesnt filter.

hence i was asking for DAX approach. I have 4 tables, 20+ columns and 20k+ rows.

Thanks

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - June 2024

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

#### Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors
Top Kudoed Authors