Results 1 to 2 of 2

Thread: PQ - counting rows filtered and after filtering

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sir Moderator sandy666's Avatar
    Join Date
    May 2018
    Posts
    239
    Rep Power
    7

    Cool PQ - counting rows filtered and after filtering

    Code:
    // FName
    let
        Source = Table.Distinct(Table.SelectRows(Excel.CurrentWorkbook(){[Name="FName"]}[Content], each [FName] <> null and [FName] <> ""))
    in
        Source
    Code:
    // FRegion
    let
        Source = Table.Distinct(Table.SelectRows(Excel.CurrentWorkbook(){[Name="FRegion"]}[Content], each [FRegion] <> null and [FRegion] <> ""))
    in
        Source
    Code:
    // FCountry
    let
        Source = Table.Distinct(Table.SelectRows(Excel.CurrentWorkbook(){[Name="FCountry"]}[Content], each [FCountry] <> null and [FCountry] <> ""))
    in
        Source
    Code:
    // src
    let
        src = [
        Source = Csv.Document(File.Contents("D:\test\countrows\data-v9i8Rbfh1ul6_1diL56pb.csv"),[Delimiter=",", Columns=3, Encoding=65001, QuoteStyle=QuoteStyle.None]),
        TPH = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    
        Fname = Table.AddColumn(TPH, "Fname", each FName),
        Exp = Table.ExpandTableColumn(Fname, "Fname", {"FName"},  {"fname"} ),
        TCname = Table.AddColumn(Exp, "TCN", each Text.Contains([name],[fname],Comparer.OrdinalIgnoreCase)),
        TSR_true = Table.SelectRows(TCname, each ([TCN] = true)),
        Fname_left = Table.NestedJoin(TPH, {"name"}, TSR_true, {"name"}, "TSR_true", JoinKind.LeftAnti),
    
        Fregion = Table.AddColumn(Fname_left, "Fregion", each FRegion),
        Exp1 = Table.ExpandTableColumn(Fregion, "Fregion", {"FRegion"}, {"fregion"}),
        TCregion = Table.AddColumn(Exp1, "TCR", each Text.Contains([region],[fregion],Comparer.OrdinalIgnoreCase)),
        TSR_true1 = Table.SelectRows(TCregion, each ([TCR] = true)),
        Fregion_left = Table.NestedJoin(TCregion, {"region"}, TSR_true1, {"region"}, "TSR_true1", JoinKind.LeftAnti),
    
        Fcountry = Table.AddColumn(Fregion_left, "fcountry", each FCountry),
        Exp2 = Table.ExpandTableColumn(Fcountry, "fcountry", {"FCountry"}, {"FCountry"}),
        TCcountry = Table.AddColumn(Exp2, "TCC", each Text.Contains([country], [FCountry], Comparer.OrdinalIgnoreCase)),
        TSR_true2 = Table.SelectRows(TCcountry, each ([TCC] = true)),
        Fcountry_left = Table.NestedJoin(TCcountry, {"country"}, TSR_true2, {"country"}, "TSR_true2", JoinKind.LeftAnti)
        ]
    in
        src

    Code:
    // Count
    let
        Count = Text.Format("Total: #[T]#(lf) Name: #[N]#(lf) Region: #[R]#(lf) Country: #[C]",
        [
        T = Text.From(Table.RowCount(src[TPH])),
        N = Text.From(Table.RowCount(src[TSR_true]))&" filtered / left "&Text.From(Table.RowCount(src[Fname_left])),
        R = Text.From(Table.RowCount(src[TSR_true1]))&" filtered / left "&Text.From(Table.RowCount(src[Fname_left])-Table.RowCount(src[TSR_true1])),
        C = Text.From(Table.RowCount(src[TSR_true2]))&" filtered / left "&Text.From(Table.RowCount(src[Fname_left])-Table.RowCount(src[TSR_true1])-Table.RowCount(src[TSR_true2]))
        ]),
        C2T = #table(1, {{Count}}),
        SplitR = Table.ExpandListColumn(Table.TransformColumns(C2T, {{"Column1", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1"),
        Ren = Table.RenameColumns(SplitR,{{"Column1", "Count"}})
    in
        Ren
    Filters:
    FName FRegion FCountry
    rr sh us
    ll


    Result:
    Count
    Total: 100
    Name: 20 filtered / left 80
    Region: 2 filtered / left 78
    Country: 3 filtered / left 75
    Attached Files Attached Files
    Last edited by sandy666; 09-03-2023 at 03:21 PM. Reason: update
    sandy
    I know you know but I forgot my Crystal Ball and don't know what you know also I may not remember what I did weeks, months or years ago so answer asap. Thinking doesn't hurt

Similar Threads

  1. VBA - Count filtered rows in the table
    By sandy666 in forum Excel Help
    Replies: 50
    Last Post: 08-26-2023, 09:35 PM
  2. PQ - Filtering by filenames
    By sandy666 in forum Power Query, Power Pivot and Power BI
    Replies: 0
    Last Post: 02-26-2021, 12:00 PM
  3. Subtotal with Filtering
    By Sakalansalex in forum Excel Help
    Replies: 2
    Last Post: 10-25-2017, 02:54 PM
  4. Delete Filtered Rows Excluding The Header
    By xander1981 in forum Excel Help
    Replies: 5
    Last Post: 04-01-2014, 11:44 PM
  5. Drop-down list (with filtering)
    By mahmoud-lee in forum Excel Help
    Replies: 2
    Last Post: 02-22-2014, 12:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •