Forum Discussion

zander140's avatar
zander140
Copper Contributor
Jun 19, 2022
Solved

How to filter by subtotals if over $5000 - Pivot Table/VBA?

Hello everyone, 

 

I was wondering in a Pivot Table, how can I filter/use a slicer to display all individuals who have a subtotal over $5000 per year. 

I did conditional formatting to identify such individuals (highlights subtotals in red) but the pivot table would not let me isolate them to show only the individuals with subtotals exceeding $5000 per year. 

Please see the picture below for a visual example. I am not sure how to attached the excel file with the pivot table but I pasted the table below. Thanks.

 

If you can provide VBA code or share how it would be possible via the Pivot table, that would be awesome. Thank you. 

Kind regards,

Zander

 

 

First Name Last Name    Trip           Price           Year

Esther AllanUS1806.452019-04-19
Esther AllanCanda359.262019-07-20
Esther AllanItaly5078.452022-05-04
John SmithUS2000.262021-02-02
John SmithPoland2500.452022-05-05
John SmithJapan3516.562021-05-07
John SmithJapan3500.892022-07-04
Ayana GrantCanada54.542021-03-05
Ayana GrantCanada500.652021-04-09
Ayana GrantCanada256.232021-02-25
JimmyLeeCanada455.232019-04-19
JimmyLeeCanada454.562021-05-02
JimmyLeeCanada670.212021-07-02

 

  • zander140 

    We may play with Power Query. For such model

    first prepare the source table

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(
            Source,
            {
                {"Year", type date},
                {"Price", Currency.Type},
                {"Trip", type text}
            }         ),
        #"Merged Columns" = Table.CombineColumns(
            #"Changed Type",
            {"First Name", "Last Name"},
            Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Name"),
        #"Extracted Year" = Table.TransformColumns(
            #"Merged Columns",
            {{"Year", Date.Year, Int64.Type}})
    in
        #"Extracted Year"

    Next generate list of people who spent more than 5000 annually. We ignore years with no trips.

    let
        Source = Table,
        Bar = Excel.CurrentWorkbook(){[Name="PriceBar"]}[Content]{0}[Column1],
    
        #"Grouped Rows" = Table.Group(
            Source,
            {"Name", "Year"},
            { {"Total", each List.Sum([Price]), type nullable number}}),
        #"Grouped Rows1" = Table.Group(
            #"Grouped Rows",
            {"Name"}, {{"Data", each List.Min( _[Total] ) > Bar }}),
        #"Filtered Rows" = Table.SelectRows(
            #"Grouped Rows1",
            each ([Data] = true)),
        #"Removed Other Columns" = Table.SelectColumns(
            #"Filtered Rows",{"Name"}),
        Name = #"Removed Other Columns"[Name]
    in
        Name

    Finally filter source table by these names and land query to the grid as PivotTable

    let
        Source = Table,
        #"Filtered Rows" = Table.SelectRows(
            Source,
            each List.Contains(#"Names Filtered", [Name] ))
    in
        #"Filtered Rows"

14 Replies

  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    zander140 

    In the initial pivot table, right-click on the row labels containing the First Name. Choose Filter, then Value Filters.... Now set the filter to Sum of Amount, is greater than and type 5000 in the empty box and press OK.

    • zander140's avatar
      zander140
      Copper Contributor

      Riny_van_Eekelen 

      Thank you for your reply. I should have mentioned that I already tried doing that but it also shows if the grand total of the individual is greater than $5000 for all years instead of isolating for the subtotal of each year and checking that. Please see below. Thanks.

       

       

      Dataset:

      First Name Last NameTrip PriceYear

      Esther AllanUS1806.452019-04-19
      Esther AllanCanda359.262019-07-20
      Esther AllanItaly5078.452022-05-04
      John SmithUS2000.262021-02-02
      John SmithPoland2500.452022-05-05
      John SmithJapan3516.562021-05-07
      John SmithJapan3500.892022-07-04
      Ayana GrantCanada653.232021-03-05
      Ayana GrantCanada756.232022-04-09
      Ayana GrantUS4000.562021-02-25
      JimmyLeeCanada455.232019-04-19
      JimmyLeeCanada454.562021-05-02
      JimmyLeeCanada670.212021-07-02
      • SergeiBaklan's avatar
        SergeiBaklan
        Diamond Contributor

        zander140 

        We may play with Power Query. For such model

        first prepare the source table

        let
            Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
            #"Changed Type" = Table.TransformColumnTypes(
                Source,
                {
                    {"Year", type date},
                    {"Price", Currency.Type},
                    {"Trip", type text}
                }         ),
            #"Merged Columns" = Table.CombineColumns(
                #"Changed Type",
                {"First Name", "Last Name"},
                Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Name"),
            #"Extracted Year" = Table.TransformColumns(
                #"Merged Columns",
                {{"Year", Date.Year, Int64.Type}})
        in
            #"Extracted Year"

        Next generate list of people who spent more than 5000 annually. We ignore years with no trips.

        let
            Source = Table,
            Bar = Excel.CurrentWorkbook(){[Name="PriceBar"]}[Content]{0}[Column1],
        
            #"Grouped Rows" = Table.Group(
                Source,
                {"Name", "Year"},
                { {"Total", each List.Sum([Price]), type nullable number}}),
            #"Grouped Rows1" = Table.Group(
                #"Grouped Rows",
                {"Name"}, {{"Data", each List.Min( _[Total] ) > Bar }}),
            #"Filtered Rows" = Table.SelectRows(
                #"Grouped Rows1",
                each ([Data] = true)),
            #"Removed Other Columns" = Table.SelectColumns(
                #"Filtered Rows",{"Name"}),
            Name = #"Removed Other Columns"[Name]
        in
            Name

        Finally filter source table by these names and land query to the grid as PivotTable

        let
            Source = Table,
            #"Filtered Rows" = Table.SelectRows(
                Source,
                each List.Contains(#"Names Filtered", [Name] ))
        in
            #"Filtered Rows"

Resources