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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
domi25
Frequent Visitor

Remove duplicate rows from the table based on condition and summarize

I have a very large table which I want to transform in the most optimal way. I need to have:

- Unique ID 

- SUM of Revenue  which depends on

* Gender, if Sale Id have both Male and Female I only need to get the Females ones,

* if i only Have one Gender "Male" i also need to check the date, if date is 2023 i want to have this row if the date is 2024 i don;t want this row

- and a lot of other colums.

 

domi25_0-1706006831428.png

 

1 ACCEPTED SOLUTION
amustafa
Solution Sage
Solution Sage

In Power Query, you can filter your data first then group it. Here's a sample code that works against your sample data and expected outcome.

 

let
Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Desktop\Book1.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table, {
{"Sale ID", Int64.Type}, {"Date", Int64.Type}, {"Place", type text}, {"Gender", type text}, {"Revenue", Int64.Type}
}),

// Filter rows where Gender is Female or where Date is not 2024
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Gender] = "Female" or [Date] <> 2024),

// Group by Sale ID, Date, and all other columns
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Sale ID", "Date", "Place", "Gender"}, {
{"Total Revenue", each List.Sum([Revenue]), type number}
})
in
#"Grouped Rows"

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
amustafa
Solution Sage
Solution Sage

In Power Query, you can filter your data first then group it. Here's a sample code that works against your sample data and expected outcome.

 

let
Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Desktop\Book1.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table, {
{"Sale ID", Int64.Type}, {"Date", Int64.Type}, {"Place", type text}, {"Gender", type text}, {"Revenue", Int64.Type}
}),

// Filter rows where Gender is Female or where Date is not 2024
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Gender] = "Female" or [Date] <> 2024),

// Group by Sale ID, Date, and all other columns
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Sale ID", "Date", "Place", "Gender"}, {
{"Total Revenue", each List.Sum([Revenue]), type number}
})
in
#"Grouped Rows"

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.