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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Subsetting a table based on other table

Hi, newbie here. I've been trying and researching how to do this all weekend, but nothing worked for me yet. Any help would be appreciated.

Basically, I have two tables:

  1. The first one is called "competition" and has the columns id_competition, id_competitor, and other types of data about how the competitor performed in the competition;

  2. The second table is called "winners", and have only the columns id_competition and id_competitor, representing the competitor that won each competition.

1 - Table competition:

id_competitionid_competitorbest_timeaverageetc
119085...
129690...
13102110...
245963...
256160...
265451...
217281...
34125114...
36120120...
37122117...

2 - Table winners:

id_competitionid_competitor
11
24
36

I want to create a new table with all the rows and columns from the table competition where the values of id_competition and id_competitor match those of the table winners. In other words, I just want to get the data of the winners of each competition from the table competitions.

I know how to do this using the power query editor, but I want to do it using DAX. In power query this is a simple "merge query as new", I think.

Result expected:

id_competitionid_competitorbest_timeaverageetc
119085...
245963...
36120120...

Thanks in advance.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I did it!! I just used the FILTER function like "NewTable = FILTER('competition', 'competition'[id_competition] = RELATED('winners'[id_competition]) && 'competition'[id_competitor] = RELATED('winners'[id_competitor]))"

Thanks again for the help!

View solution in original post

5 REPLIES 5
MURTAZA
Resolver I
Resolver I

Hi  @Anonymous 

Your data model would be one to many relationship, and you can not bring the RELATED data to the one side of the relationship (it can be done only to Many side of the relationship)

The way around I can suggest is to create a one to one relationship by creating unique key in both tables using:
1. "Add new Column" and then use

Concatenate =

 

'Table competition'[id_competition] & "-" & 'Table competition'[id_competitor]

 

 Similarly in the other table

Concatenate =

 

'Table winners'[id_competition] & "-" & 'Table winners'[id_competitor]

 

Now create the relation between both concatenate columns.
3. Then using the RELATED function, you can bring in all the winners data.

For example, Goto Table Winners and create a new column using:

Average =

 

RELATED('Table competition'[average])
Similarly for other columns as well.

MURTAZA_1-1615223022127.png

 

 

PaulDBrown
Community Champion
Community Champion

@Anonymous 

 

Edited: I realised we need measures fr the correct result

If the data is as you have shown, all you need to do is create a one-to-many relationship between both tables by joining the Id competition fields:
Model.JPG

 

You can the create a visual using the fields from the "Winner" table andthe following measures:

Average Time =
CALCULATE (
    AVERAGE ( Competitions[average] ),
    TREATAS (
        VALUES ( Winners[id_competitor Winner] ),
        Competitions[id_competitor]
    )
)

 

Best Time =
CALCULATE (
    MIN ( Competitions[best_time] ),
    TREATAS (
        VALUES ( Winners[id_competitor Winner] ),
        Competitions[id_competitor]
    )
)

Result.JPG

 

 

If, however, you have a more complex data structure (competion by meeting etc) then you will need to change the model structure slightly.





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Hi! Thanks for helping. I do indeed have a more complex data structure. In fact, my database is already normalized. The columns "id_competition" are foreign keys to a table that has "id_competition, name_competition, country, etc"; The columns "id_competitor" are also foreign keys. They reference a table that has "id_competitor, name_competitor, age, etc".

 

The two tables I want to "join" are not directly connected. I think there must be a way to compare the two columns on both of the tables, like "filter the rows from table competition if id_competition and id_competitor from table competition are equal to id_competition and id_competitor from table winners".

 

Let me know if I can provide more details.

@Anonymous 

You can create a virtual relationship using TREATAS (see my edited post above) to filter unrelated fields. Can you show the model view of the relevant tables (both dimension and fact tables)?

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

I did it!! I just used the FILTER function like "NewTable = FILTER('competition', 'competition'[id_competition] = RELATED('winners'[id_competition]) && 'competition'[id_competitor] = RELATED('winners'[id_competitor]))"

Thanks again for the help!

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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