Forum Discussion

KNP's avatar
KNP
Icon for Super User rankSuper User
6 years ago
Solved

Filter Table based on values in a "rules" table

Hi,    I have an interesting scenario where I'd like to filter my main data table...   Row Customer  Consignor  Supplier  Region  SubRegion  Count  Value 1 A ZZ       20 11.3 ...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi KNP 

     

    if your rules table not excessively big - under 50-60 rows (even 100 should be fine, but a bit slower).

    This is the main data table:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZBNDsQgCEav0rhuiIC/y3Y6l6jp/a8xgnZCmnTxkRieT7A1h251W8959rKYkO8FEdhda3M0seVJSQMJwsDY2vZdegL1cFIncFAw9NNnOpS7hazCAr4oFw1nE4uykLNiIj+sDuehSicGoPFsNpxNqqIrUEtUTuz3VIe9EJJMRKm/TAOtBt1kd/kDWS/q6gUKDhD9UxrvSGEEnycpN7/W+Z9T1uEMVAdIb6B2EuTxi8ivQvEBd931Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Row = _t, Customer = _t, Consignor = _t, Supplier = _t, Region = _t, SubRegion = _t, Count = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Row", Int64.Type}, {"Customer", type text}, {"Consignor", type text}, {"Supplier", type text}, {"Region", Int64.Type}, {"SubRegion", Int64.Type}, {"Count", Int64.Type}, {"Value", type number}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Filter", each fApplyRules(_)),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Filter] = false))
    in
        #"Filtered Rows"

     

    This is the fApplyRules function:

    (pRow as record) =>
        let
            mRow = pRow,
            Source = Rules,
    
            // Filter Supplier/Customer
            
            #"SC Filtered" = Table.SelectRows(Source, each ([Field] = "Supplier" and [Company] = mRow[Supplier]) or ([Field] = "Customer" and [Company] = mRow[Customer])),
            Output = List.Accumulate(#"SC Filtered"[Check Field], true, (s, a)=> s and (Record.Field(mRow, a) <> null  and Text.Trim(Text.From(Record.Field(mRow, a))) <> "" ))
        in
            Output

     

    Kind regards,

    JB