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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Groupby Maximum in Transform Data

Hi, 

 

I'd like to create a database, using the transform data query editor, with one row grouped and summed by [AFE], [Major], [Minor], [Cost] when [GL_YEAR] is a MAXIMUM. This is because there is a potentially an afe, major minor and cost for each year, but I only want the most recent year. 

 

I'm able to use the groupby function in the Transform data to group and sum, however, like I mentioned, I only want to group and sum if the [GL_YEAR] is a maximum. I'm assuming we will split the [GL_YEAR] to remove the first two characters and turn the column into a number type, although I'm not sure how to work the maximum code since the groupby feature I'm using is a "gui". The first table below is a sample of my data, the following table is the result I'm pursuing:

 

Thanks again for your efforts on the forum, the community has helped me many times! 

 

AFEMajorMinorCostGL_YEAR
10130010YR19
10130020YR20
10130015YR20
10140010YR19
1014005YR20
20130020YR19
20130010YR20
20140020YR20
20240010YR19
20240020YR19
     
AFEMajorMinorCostGL_YEAR MAX
10130035YR20
1014005YR20
20130010YR20
20140020YR20
20240030YR19

 

1 ACCEPTED SOLUTION
HotChilli
Super User
Super User

I split the column, did a groupBy to get Max Year, (maintaining original data).

Then Expand the column and select the rows that match Max Year.

Then another GroupBy to get Total Cost

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRQ0lEyBGJjAzALREQGGVoqxepgSBpBJIEUFklDU6ySJviMhUiiaDTCaiVUoxFWx6LrNMHiWDDPCId70CSR7IwFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AFE = _t, Major = _t, Minor = _t, Cost = _t, GL_YEAR = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"AFE", Int64.Type}, {"Major", Int64.Type}, {"Minor", Int64.Type}, {"Cost", Int64.Type}, {"GL_YEAR", type text}}),
    #"Split Column by Positions" = Table.SplitColumn(#"Changed Type", "GL_YEAR", Splitter.SplitTextByPositions({2}), {"GL_YEAR.1"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Positions",{{"GL_YEAR.1", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"AFE", "Major", "Minor"}, {{"MaxYear", each List.Max([GL_YEAR.1]), type number}, {"all", each _, type table [AFE=number, Major=number, Minor=number, Cost=number, GL_YEAR.1=number]}}),
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Cost", "GL_YEAR.1"}, {"all.Cost", "all.GL_YEAR.1"}),
    Custom1 = Table.SelectRows(#"Expanded all", each ( [MaxYear] = [all.GL_YEAR.1])),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"all.GL_YEAR.1"}),
    #"Grouped Rows1" = Table.Group(#"Removed Columns", {"AFE", "Major", "Minor", "MaxYear"}, {{"TotCost", each List.Sum([all.Cost]), type number}})
in
    #"Grouped Rows1"

 

Let me know how you get on

View solution in original post

1 REPLY 1
HotChilli
Super User
Super User

I split the column, did a groupBy to get Max Year, (maintaining original data).

Then Expand the column and select the rows that match Max Year.

Then another GroupBy to get Total Cost

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRQ0lEyBGJjAzALREQGGVoqxepgSBpBJIEUFklDU6ySJviMhUiiaDTCaiVUoxFWx6LrNMHiWDDPCId70CSR7IwFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AFE = _t, Major = _t, Minor = _t, Cost = _t, GL_YEAR = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"AFE", Int64.Type}, {"Major", Int64.Type}, {"Minor", Int64.Type}, {"Cost", Int64.Type}, {"GL_YEAR", type text}}),
    #"Split Column by Positions" = Table.SplitColumn(#"Changed Type", "GL_YEAR", Splitter.SplitTextByPositions({2}), {"GL_YEAR.1"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Positions",{{"GL_YEAR.1", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"AFE", "Major", "Minor"}, {{"MaxYear", each List.Max([GL_YEAR.1]), type number}, {"all", each _, type table [AFE=number, Major=number, Minor=number, Cost=number, GL_YEAR.1=number]}}),
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Cost", "GL_YEAR.1"}, {"all.Cost", "all.GL_YEAR.1"}),
    Custom1 = Table.SelectRows(#"Expanded all", each ( [MaxYear] = [all.GL_YEAR.1])),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"all.GL_YEAR.1"}),
    #"Grouped Rows1" = Table.Group(#"Removed Columns", {"AFE", "Major", "Minor", "MaxYear"}, {{"TotCost", each List.Sum([all.Cost]), type number}})
in
    #"Grouped Rows1"

 

Let me know how you get on

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.