Forum Discussion

Merleau's avatar
Merleau
Icon for Helper II rankHelper II
6 years ago
Solved

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

  • 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

2 Replies

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity 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