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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Merleau
Helper II
Helper II

Filtering using a condition on another column in M and in DAX

 

Hello,

I need help to filter a table both in M and in DAX.

My original table looks like:

 

ID

Date

Other columns

Location

Other Columns

1

10/11/2018

 

Domestic

 

4

10/12/2019

 

International

 

2

5/13/2020

 

Domestic

 

4

1/14/2020

 

International

 

6

10/15/2018

 

Domestic

 

 

I would like to select only rows where date is before Feb 15, 2020 if the location is “Domestic” or before Dec 15, 2019 if the location is “International”.

I need the solution both in M and in DAX.

 

The final table should look like:

 

ID

Date

Other columns

Location

Other Columns

1

10/11/2018

 

Domestic

 

4

10/12/2019

 

International

 

6

10/15/2018

 

Domestic

 

 

In M, I tried the following, but the result is all wrong.

 

 

#"Filtered Rows" = Table.SelectRows(#"Source", each ( if [Location]="Domestic" then [Date] < #date(2020, 15, 2) else [Date] < #date(2019, 15, 12) ) ),

 

I have no idea how to go about it in DAX.

Can somebody pls help?

Thank you

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Merleau 

 

this is the solution for Power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI00Dc01DcyMLQEclzyc1OLSzKTlWJ1opVMoLJGMFnPvJLUorzEksz8vMQcsBIjoKipvqExUIWRAVb9+oYmMElM7WZQG0xBNlig6I8FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Location = _t]),
    Trans = Table.TransformColumns(Source,{"Date",each Date.From(_, "en-US" )}),
    SelRows = Table.SelectRows(Trans, each (_[Location]="Domestic" and _[Date] < #date(2020, 2, 15)) or (_[Location]="International" and _[Date] < #date(2019,12,15)))
in
    SelRows

 

in DAX you can add a new column like this and filter in the visuals

FilterColumn = if ( and([Location]="Domestic", [Date]<date(2020,2,15)),TRUE(), if (and([Location]="International", [Date]<date(2019,12,15)),true,false))

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @Merleau 

 

this is the solution for Power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI00Dc01DcyMLQEclzyc1OLSzKTlWJ1opVMoLJGMFnPvJLUorzEksz8vMQcsBIjoKipvqExUIWRAVb9+oYmMElM7WZQG0xBNlig6I8FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Location = _t]),
    Trans = Table.TransformColumns(Source,{"Date",each Date.From(_, "en-US" )}),
    SelRows = Table.SelectRows(Trans, each (_[Location]="Domestic" and _[Date] < #date(2020, 2, 15)) or (_[Location]="International" and _[Date] < #date(2019,12,15)))
in
    SelRows

 

in DAX you can add a new column like this and filter in the visuals

FilterColumn = if ( and([Location]="Domestic", [Date]<date(2020,2,15)),TRUE(), if (and([Location]="International", [Date]<date(2019,12,15)),true,false))

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

This is perfect @Jimmy801 

Thank you.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors