Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
powerlight1
Helper I
Helper I

Find MAX Value within a column

Hi,

I am new to powerquery and can only do very basic things however I have a situation where I want to create a new column that has the MAX or MIN of another column within the same table.

 

In the below table, I have duplicates for code 380270 and I want to create/add a new column that is the MIN/MAX of the SellingClass column grouped by the first 5 columns:

 

powerlight1_0-1777944420926.png

Thanks

1 ACCEPTED SOLUTION
KNP
Super User
Super User

Hi @powerlight1,

Create a new blank query. Open the advanced editor and paste in this code. You should be able to see what is happening in each step.

 

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WMrYwMDI3UNJRSkvMSykD0UWlmSXFQEZqGpAw0k1LLC5RitUhqNJYNzc1JbM0Vyk2FgA=", BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [id = _t, category = _t, #"type" = _t, subtype = _t, sellingclass = _t]
    ),
    ct = Table.TransformColumnTypes(
        Source,
        {
            {"id", Int64.Type},
            {"category", type text},
            {"type", type text},
            {"subtype", type text},
            {"sellingclass", type text}
        }
    ),
    group = Table.Group(
        ct,
        {"id", "category", "type", "subtype"},
        {
            {"minsc", each List.Min([sellingclass]), type nullable text},
            {"maxsc", each List.Max([sellingclass]), type nullable text}
        }
    ),
    merge = Table.NestedJoin(
        ct,
        {"id", "category", "type", "subtype"},
        group,
        {"id", "category", "type", "subtype"},
        "group",
        JoinKind.LeftOuter
    ),
    expandrows = Table.ExpandTableColumn(merge, "group", {"minsc", "maxsc"}, {"minsc", "maxsc"})
in
    expandrows

 

The group step groups everything together and grabs the min and max values. You then merge with the CT step and the group step and expand the min-max columns. Power Query allows you to reference any step; it doesn't have to be the previous one.

Let me know if you have any questions.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
xOIEmaj

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
image
fabric-SUbadge
Proud to be a Super User!

View solution in original post

2 REPLIES 2
MarkLaf
Super User
Super User

This can be done through the UI. I showed the steps by clicking through the ribbons, but you can alternatively perform same actions through right-click menu.

 

1) Select the first four columns (I just used defaults since you hid names). Then go to Transform > Group By

MarkLaf_0-1777952652996.png

 

2) In the Group By window, make selections for the following aggregations

MarkLaf_1-1777952768387.png

3) Select your three new agg columns and then Remove Other Columns (ie select your new columns)

MarkLaf_2-1777953004246.png

 

4 ) Click the '↰↱' button on the ALL ROWS column, uncheck 'Use original column name as prefix', and click okay. This brings back all your original columns/rows

MarkLaf_5-1777953343589.png

 

Output:

 

MarkLaf_4-1777953243342.png

 

KNP
Super User
Super User

Hi @powerlight1,

Create a new blank query. Open the advanced editor and paste in this code. You should be able to see what is happening in each step.

 

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WMrYwMDI3UNJRSkvMSykD0UWlmSXFQEZqGpAw0k1LLC5RitUhqNJYNzc1JbM0Vyk2FgA=", BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [id = _t, category = _t, #"type" = _t, subtype = _t, sellingclass = _t]
    ),
    ct = Table.TransformColumnTypes(
        Source,
        {
            {"id", Int64.Type},
            {"category", type text},
            {"type", type text},
            {"subtype", type text},
            {"sellingclass", type text}
        }
    ),
    group = Table.Group(
        ct,
        {"id", "category", "type", "subtype"},
        {
            {"minsc", each List.Min([sellingclass]), type nullable text},
            {"maxsc", each List.Max([sellingclass]), type nullable text}
        }
    ),
    merge = Table.NestedJoin(
        ct,
        {"id", "category", "type", "subtype"},
        group,
        {"id", "category", "type", "subtype"},
        "group",
        JoinKind.LeftOuter
    ),
    expandrows = Table.ExpandTableColumn(merge, "group", {"minsc", "maxsc"}, {"minsc", "maxsc"})
in
    expandrows

 

The group step groups everything together and grabs the min and max values. You then merge with the CT step and the group step and expand the min-max columns. Power Query allows you to reference any step; it doesn't have to be the previous one.

Let me know if you have any questions.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
xOIEmaj

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
image
fabric-SUbadge
Proud to be a Super User!

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Kudoed Authors