cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
vyacheslavg
Helper II
Helper II

How to filter from Many table to One table in DAX?

Dear community, 

 

I have two simple one-to-many tables.

 

Table "One"

VersionID

1

2

 

 

Table "Many" 

VersionID

1

2

2

3

3

3

 

 

I can easily create new filtered Table for Many, based on One. Below is the example, where I filter ID = 2

Table1 = FILTER(Many, RELATED(One[One.VersionID])=2)

 

However, it is very complicated to do the reverse (filter from Many to One) in DAX...

 

I can see, that filtering works fine, using filters in Report view - i.e. One table is being filtered by ID in Many table. It works absolutely fine! (relationship is one to many, cross-filtering is enabled, of course).

When I try to use Related - it obviously does not work (DAX Related works only from One to Many). I tried to apply RELATEDTABLE, but it seems it works well only for aggregations, and I cannot wrap my head around on how to use for filtering...

I saw several videos by Curbal and Rob Kerr on the topic - still do not understand how to crack it. 

 

 

Filter is working in Report GUI !Filter is working in Report GUI !

 

1 ACCEPTED SOLUTION

Hi @vyacheslavg,

 

Try this formula please:

 

Table =
FILTER (
    NATURALINNERJOIN ( 'Orders', Products ),
    [City] = "Paris"
        && [ID 1] = 3
)

How to filter from Many table to One table in DAX.JPG

 

 

 

 

 

 

 

 

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
BILASolution
Solution Specialist
Solution Specialist

Hi @vyacheslavg

 

I hope this helps

 

filter.png

 

 

 

Regards

BILASolution

 

 

 

 

 

thanks. As I said, filtering from many to one is working in Report view.

I'm pretty much sure, that Power BI uses some aggregation in Many behind the scenes.

I just want to replicate this behaviour in DAX, so I will be able to produce tables, based on table "One", with filter applied on some columns and values in table "Many".

 

For example, below is pseudocode (it will not work in DAX) . I want to create a DAX code for it...

 

Table = Filter (One, Related(Many[ID]) = 1)

 

My thought is I need to use RelatedTable and Count functions somehow, but I cannot figure out now.

I solved this, solution is below - maybe someone can find it helpful...

 

First, create temp table with distinct IDs.

 

Unique_Filter = SUMMARIZECOLUMNS('Many'[ID])

 

Then create relationship in datemodel.

 

and apply filter thru related! Great, it works the same as in Report view!

Not very elegant, but it does the work just fine.  

 

One_filter_applied  = FILTER(One, RELATED(Unique_Filter[ID]))

I want to extend it now to work with more complicated filters, but struggling so far...

 

Assume, that you have Products and Orders tables.

 

Table "Products"

ID   Details

1     red

2     blue

3     green

4     black

 

 

Table "Orders" 

ID   City 

1     London

2     Frankurt

2     Paris

3     London

3     Paris

3     Berlin

 

I want to create the table, where ID =3 and City = Paris.

 

This is how to solve it (phew...)

 

Unique_Filter v2 = SUMMARIZECOLUMNS(Orders[ID], FILTER(Orders, Orders[City]="Paris" && Orders[ID] = 3))

 

 

There SHOULD BE simpler way to do from Orders  side, not Products...  But I want to get all details about products from Products table into this new table.

 

In pseudocode, it should be something like this.

 

Filter Orders table by ID = 3 and City = Paris. Easy.

Get all details from Products, using ID. Hard.

 

I think it could be some kind of join on already filtered Orders table.

 

Hi @vyacheslavg,

 

Try this formula please:

 

Table =
FILTER (
    NATURALINNERJOIN ( 'Orders', Products ),
    [City] = "Paris"
        && [ID 1] = 3
)

How to filter from Many table to One table in DAX.JPG

 

 

 

 

 

 

 

 

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hi Dale, 

 

this is it - this is one step creation of table from Many side with all details from One side.

(I expected that IDs will go into single column during join, but this is minor stuff - I can easily remove this column later).

 

Thanks!

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

March Update

Check it Out!

Click here to read more about the March 2021 Updates!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

secondImage

Experience what’s next for Power BI

See the latest Power BI innovations, updates, and demos from the Microsoft Business Applications Launch Event.