SOLVED

How can I filter a table by two columns in Power BI

%3CLINGO-SUB%20id%3D%22lingo-sub-3068559%22%20slang%3D%22en-US%22%3EHow%20can%20I%20filter%20a%20table%20by%20two%20columns%20in%20Power%20BI%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3068559%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20have%20a%20table%20of%20order%20quantities%2C%20which%20has%20a%20date%20column%20(format%3A%20date)%20and%20a%20country%20column%20(text).%3C%2FP%3E%3CP%3EI%20would%20like%20to%20filter%20this%20column%20by%20not%20using%20e.g.%20orders%20from%20US%20in%20second%20quarter%20of%202021.%3C%2FP%3E%3CP%3EIn%20the%20normal%20filtering%20I%20either%20can%20say%20%22not%20US%22%2C%20but%20then%20filters%20also%20the%20orders%20from%20US%20outside%20from%20second%20quarter%20in%202021%2C%20or%20can%20say%20%22not%20Q2%20in%202021%22%20then%20it%20filters%20all%20countries%2C%20but%20not%20only%20US!%3C%2FP%3E%3CP%3EMany%20thanks%2C%20maybe%20this%20is%20a%20simple%20question%2C%20but%20I%20am%20driving%20crazy%20now!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3068559%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hi,

I have a table of order quantities, which has a date column (format: date) and a country column (text).

I would like to filter this column by not using e.g. orders from US in second quarter of 2021.

In the normal filtering I either can say "not US", but then filters also the orders from US outside from second quarter in 2021, or can say "not Q2 in 2021" then it filters all countries, but not only US!

Many thanks, maybe this is a simple question, but I am driving crazy now! 

5 Replies

@carloswalder 

I suggest advanced filter. In the attached sheet you can execute advanced filter by clicking cell E5. In cells E8 and E9 you can enter the country and month you want to exclude. The criteria range is E1:E2 and cells E2 holds the rule for the advanced filter.

@carloswalder 

=FILTER(A2:D22,(C2:C22<>"US")+((C2:C22="US")*(D2:D22<>8)))

 

If you work with Office365 or 2021 you can apply FILTER function as well.

best response confirmed by Hans Vogelaar (MVP)
Solution
I would suggest creating a helper column with a formula like:
="Q"&INT(MONTH([Date])/4)+1 & "-" & YEAR([Date]) & "-" & [Country]
Then filter out "Q2-2021-US"

@mtarler 

This was the right hint, as I need to do this in power query, my formular now looks like this - this of course can be optimized:

#"Extracted Year" = Table.TransformColumns(#"Added Custom",{{"Quarter", Date.Year, Int64.Type}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Extracted Year",{{"Quarter", type text}}),
#"Renamed Columns1" = Table.RenameColumns(#"Changed Type1",{{"Quarter", "Year"}}),
#"Added Custom1" = Table.AddColumn(#"Renamed Columns1", "Quarter", each [WKOrderIntakeDate]),
#"Calculated Quarter" = Table.TransformColumns(#"Added Custom1",{{"Quarter", Date.QuarterOfYear, Int64.Type}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Calculated Quarter",{{"Quarter", type text}}),
#"Added Custom2" = Table.AddColumn(#"Changed Type2", "BU", each [WKBusinessUnit]),
#"Changed Type3" = Table.TransformColumnTypes(#"Added Custom2",{{"BU", type text}}),
#"Added Custom3" = Table.AddColumn(#"Changed Type3", "BU & QUARTER", each "Q" & [Quarter] & "BU" & [BU] & "Y" & [Year]),
#"Filtered Rows5" = Table.SelectRows(#"Added Custom3", each [#"BU & QUARTER"] <> "Q2BU3Y2021")
in
#"Filtered Rows5"

@carloswalder 

In general you may create column for filtering at once

...

    #"Changed Type" = Table.TransformColumnTypes(
        PrevStep,
        {
            {"Date", type date},
            {"WKBusinessUnit", type text}
        }),
    #"Inserted Filter" = Table.AddColumn(
        #"Changed Type",
        "Filter",
        each "Q" & Text.From( Date.QuarterOfYear([Date]) ) &
             "BU" & [WKBusinessUnit] &
             "Y" & Text.From( Date.Year( [Date] )),
        type text)
in
    #"Inserted Filter"

and apply to it filter with parameter like Q2BU3Y2021