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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
BI_Cub
Frequent Visitor

sum rows based on condition

Hello, 

I want to add two rows based on a value in one of the columns in my query. Add up the Qty for instructor and other for each year and department and have it as a new row.  Here is a sample:

Query: 

 

YearDepartmentClassificationQty 
2018Dept 1Assistant1
2018Dept 1Instructor2
2018Dept 1Other5
2018Dept 2Assistant5
2019Dept 1Instructor1
2019Dept 1Other2
2019Dept 1Assistant4
2019Dept 2Assistant3

 

This is what I want:

YearDepartmentClassificationQty
2018Dept 1Assistant1
2018Dept 1Instructor2
2018Dept 1Other5
2018Dept 1Instructor/Other7
2019Dept 1Assistant4
2019Dept 1Instructor1
2019Dept 1Other2
2019Dept 1Instructor/Other3
2019Dept 2Assistant3

 

Thanks for the help! 

3 REPLIES 3
wdx223_Daniel
Community Champion
Community Champion

NewStep= let a={"Instructor","Other"} in Table.Combine(Table.Group(PreviousStepName,{"Year","Department"},{"n",each let s=List.Sum(List.Transform(Table.ToRows(_),each if List.Contains(a,_{2}) then _{3} else 0)) in if s=0 then _ else _&#table(Table.ColumnNames(_),{{[Year]{0},[Department]{0},Text.Combine(a,"/"),s}})})[n])

wdx223_Daniel_0-1658978480326.png

 

jennratten
Super User
Super User

Here is how you can do it.  

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFDSUXJJLShRMAQyHIuLM4tLEvNKgGxDpVgdTBWeecUlRaXJJflFQI4RViX+JRmpIFlTDFkjNCvgKixxWmGIVQnMCiOssshWmGCoQHeEsVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, Department = _t, Classification = _t, #"Qty " = _t]),
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Department", type text}, {"Classification", type text}, {"Qty ", Int64.Type}}),
    Table1 = Table.AddColumn(ChangeTypes, "Level", each 1, Int64.Type),
    RenameColumns = Table.RenameColumns(Table1,{{"Classification", "ClassificationOld"}}),
    Classification = Table.AddColumn(RenameColumns, "Classification", each if [ClassificationOld] = "Instructor" or [ClassificationOld] = "Other" then "Instructor/Other" else "", type text),
    SelectRows = Table.SelectRows(Classification, each ([Classification] = "Instructor/Other")),
    Group = Table.Group(SelectRows, {"Year", "Department", "Classification"}, {{"Qty ", each List.Sum([#"Qty "]), type nullable number}}),
    Table2 = Table.AddColumn(Group, "Level", each 2, Int64.Type),
    Append = Table.Combine ( { Table1, Table2 } ),
    #"Sorted Rows" = Table.Sort(Append,{{"Year", Order.Ascending}, {"Department", Order.Ascending}, {"Level", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Level"})
in
    #"Removed Columns"

jennratten_0-1658960060306.png

 

Thank you for your responses these have been helpful and got it done!

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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