Forum Discussion
Filter by top value per category while keeping all columns
Hi,
I'm quite newbie in PowerBI, and I put myself to a task that's proving a bit complicated:
I have two tables:
Products (Name is Unique)
| Name | Category | Provider |
| Apples | Fruit | A |
| Butter | Dairy | B |
| Tomatoes | Vegetables | A |
| Bananas | Fruit | A |
| Pears | Fruit | C |
| Lettuce | Vegetables | D |
| Milk | Dairy | E |
| Cheese | Dairy | F |
| Yogurt | Dairy | G |
Sales (Sale_Id is Unique)
| Product | City | Sale | Date | Time | Sale_Id |
| Tomatoes | A | 100 | 03/06/2020 | 09:00 | 1 |
| Tomatoes | A | 90 | 01/06/2020 | 18:00 | 2 |
| Tomatoes | A | 100 | 02/06/2020 | 19:00 | 3 |
| Apples | A | 120 | 01/06/2020 | 12:00 | 4 |
| Apples | B | 120 | 01/06/2020 | 16:00 | 5 |
| Cheese | B | 200 | 01/06/2020 | 14:00 | 6 |
| Yogurt | B | 125 | 01/06/2020 | 10:00 | 7 |
I need to create a table with all columns, that lists only the top sale for each Product/City. For equal Product/City/Sale, the chosen row should be the one with the earliest date/time. The pairs Product/City that do not have sales shall also be listed with sales data empty/null, something like this:
Name | Category | Provider | City | Sale | Date | Time | Sale_Id |
| Apples | Fruit | A | A | 120 | 01/06/2020 | 12:00 | 4 |
| Apples | Fruit | A | B | 120 | 01/06/2020 | 16:00 | 5 |
| Butter | Dairy | B | A | ||||
| Butter | Dairy | B | B | ||||
| Tomatoes | Vegetables | A | A | 100 | 02/06/2020 | 19:00 | 3 |
| Tomatoes | Vegetables | A | B | ||||
| Bananas | Fruit | A | A | ||||
| Bananas | Fruit | A | B | ||||
| Pears | Fruit | C | A | ||||
| Pears | Fruit | C | B | ||||
| Lettuce | Vegetables | D | A | ||||
| Lettuce | Vegetables | D | B | ||||
| Milk | Dairy | E | A | ||||
| Milk | Dairy | E | B | ||||
| Cheese | Dairy | F | A | ||||
| Cheese | Dairy | F | B | 200 | 01/06/2020 | 14:00 | 6 |
| Yogurt | Dairy | G | A | ||||
| Yogurt | Dairy | G | B | 125 | 01/06/2020 | 10:00 | 7 |
Sales table has millions of rows, I suppose that performance wise, the less times we merge into it the better.
It is also acceptable to create two tables, one per city. I almost solved the problem with a series of Merge and one Group By Max(Sale), but I still cannot avoid duplicates in the end table when the same Product/City have several sales with the same value which is also the top one. In the example above, I get two rows for Tomatoes in City A, for sale 1 and sale 3. If I tune the Group By adding Min(Date), Min(Time) and Min(Sale_Id), I lose the relational integrity, as I get only one line for Tomatoes in City A, but date 2020/06/01, time 09:00 and Sale_Id 1.
I also thought of creating a column with a binary value being 1 for each Max(Sale)/Min(Date and time)/Min(Sale_Id), but I would need some kind of MINIF, which I also don't know how to do.
I prefer a Power Query only solution, but I can live with a M/DAX combination. Is there a way to get the table (or tables) I need, if possible in a non overly complicated way?
Your help and expertise is highly appreciated!
António
Hi LetsPBI
Create a duplicated query of "Sales"->"Sales(2)"
In "Sales(2)"
In "Product" table,
Expand to new rows
Merge queries and expand twice
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
2 Replies
- lbendlinSuper User
How about a composite key, concatenating fruit name and city?
- v-juanli-msftCommunity Support
Hi LetsPBI
Create a duplicated query of "Sales"->"Sales(2)"
In "Sales(2)"
In "Product" table,
Expand to new rows
Merge queries and expand twice
Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.