Forum Discussion

amitgajkal2007's avatar
amitgajkal2007
Frequent Visitor
2 years ago
Solved

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

team1team2winner
aca
abb
bab
bcc
caa
cbb
daa
add

 

Ouput data:

Teammatcheswins
a63
b43
c41
d21
  • 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

  • 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

     

     

    • amitgajkal2007's avatar
      amitgajkal2007
      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)

      • Ritaf1983's avatar
        Ritaf1983
        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.

  • 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 = 
     ADDCOLUMNS(
        _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

     

    • amitgajkal2007's avatar
      amitgajkal2007
      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