Forum Discussion

olimilo's avatar
olimilo
Post Prodigy
1 month ago
Solved

How to filter a Table column in Power Query?

From the File combine custom function, you can filter a Record attribute using the following syntax:   each [RecordColumnName]?[AttributeColumnName]?     Below I have a dataset where I g...
  • SamInogic's avatar
    1 month ago

    Hi,

     

    Yes, you can filter the nested table without expanding it, and in most cases this is preferable because it keeps the transformation within each grouped table.

    For example, if you only want the row with the highest Index from each nested table, you can use:

    Table.TransformColumns(

        #"Grouped rows",

        {

            "Custom",

            each Table.Max(_, "Index")

        }

    )

    or equivalently:

    Table.TransformColumns(

        #"Grouped rows",

        {

            "Custom",

            each

                let

                    maxIndex = List.Max(_[Index])

                in

                    Table.SelectRows(_, each [Index] = maxIndex)

        }

    )

    Which is more efficient?

    Table.Max() is generally the better choice because:

    • It directly returns the row with the maximum value.
    • It avoids creating an intermediate list (List.Max) and then scanning the table again with Table.SelectRows.
    • The code is simpler and easier to maintain.

    That said, the actual performance difference is usually small unless you're working with very large nested tables.

    Can this be done without nested tables?

    Yes. If your goal is simply to keep the latest record per Source.Name, another common approach is:

    1. Sort the table by Source.Name and Timestamp (descending).
    2. Remove duplicates on Source.Name.

    This avoids creating grouped/nested tables altogether and is often faster, especially if query folding is preserved.

    For example:

    let

        Sorted =

            Table.Sort(

                Source,

                {

                    {"Source.Name", Order.Ascending},

                    {"Date created", Order.Descending}

                }

            ),

        Latest =

            Table.Distinct(Sorted, {"Source.Name"})

    in

        Latest

    Regarding Table.Buffer(), I'd recommend using it only after profiling your query. While it can improve performance in some scenarios by preventing repeated evaluation, it also breaks query folding and loads the buffered table into memory, which can actually degrade performance for large datasets.

     

    If your source supports query folding (e.g., SQL Server, Fabric, Dataverse), the Sort + Distinct approach is often the most efficient. If query folding isn't available, Table.Max() within the grouped tables is a clean and efficient solution.

     

    Hope this helps.

     

    Thanks!