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
drav11
Frequent Visitor

Tricky task: is it possible to create a negative filter + positive filter?

Hi all,

 

I'm not sure if this task is possible to do it on Power BI.

I have a sales database with customer_id, date, product. I want to create a visual table where the user can extract a list of customers according to the visual filters of his choice. These filters are the date and the product purchased. In particular, the user can choose a range of dates in which the customer bought product X and another range of dates in which he did not buy product Y.

Example: list of customers who bought pears and tomatoes in March-23 , but who have not bought meat on May-23. Taking into account the large amount of input data, do you think it is possible to create such a dashboard?

1 ACCEPTED SOLUTION

Hi @drav11 ,

 

Please try:

First create two new table:

 

Positive = 
var _a = DISTINCT('transaction'[date (dd/mm/yyyy)])
var _b = DISTINCT('transaction'[item])
return CROSSJOIN(_a,_b)

Negative = 
var _a = DISTINCT('transaction'[date (dd/mm/yyyy)])
var _b = DISTINCT('transaction'[item])
return CROSSJOIN(_a,_b)

 

Then create slicers:

vjianbolimsft_0-1686538275194.png

Then apply the measure to the visual's filter:

Flag = 
var _a = SELECTCOLUMNS('Positive',"date",[date (dd/mm/yyyy)])
var _b = SELECTCOLUMNS('Positive',"item",[item])
var _c = SELECTCOLUMNS('Negative',"date",[date (dd/mm/yyyy)])
var _d = SELECTCOLUMNS('Negative',"item",[item])
var _e = SELECTCOLUMNS(FILTER(ALL('transaction'),[date (dd/mm/yyyy)] in _a&&'transaction'[item] in _b),"id",[customer_id])
var _f = SELECTCOLUMNS(FILTER(ALL('transaction'),[date (dd/mm/yyyy)] in _c&&'transaction'[item] in _d),"id",[customer_id])
var _g = EXCEPT(_e,_f)
return IF(SELECTEDVALUE(customer_base[customer_id]) in _g,1)

vjianbolimsft_0-1686547157962.png

Final output:

vjianbolimsft_1-1686547189750.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@drav11 Should be possible using something like a Complex Selector. Also, new and returning customers have a similar pattern. 

The Complex Selector - Microsoft Fabric Community

Better Sales from New Customers - Microsoft Fabric Community

 

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks for the answer, I'll try to insert an example here.

I have two imported table:

1) customer_base: all registered customers (not all have made purchases)

customer_id
1
2
3
4

 

2) transaction: table with all transaction of customer_id:

customer_iddate (dd/mm/yyyy)item
101/03/2023tomato
101/03/2023pear
101/05/2023tomato
201/03/2023pear
201/03/2023tomato
201/05/2023pear
301/03/2023tomato
301/05/2023meat
401/05/2023pear

 

The visual table will have to provide the list of customers who bought pears or tomatoes in March, but didn't buy meat in May, therefore:

 

customer_id
1
2

Hi @drav11 ,

 

Please try:

First create two new table:

 

Positive = 
var _a = DISTINCT('transaction'[date (dd/mm/yyyy)])
var _b = DISTINCT('transaction'[item])
return CROSSJOIN(_a,_b)

Negative = 
var _a = DISTINCT('transaction'[date (dd/mm/yyyy)])
var _b = DISTINCT('transaction'[item])
return CROSSJOIN(_a,_b)

 

Then create slicers:

vjianbolimsft_0-1686538275194.png

Then apply the measure to the visual's filter:

Flag = 
var _a = SELECTCOLUMNS('Positive',"date",[date (dd/mm/yyyy)])
var _b = SELECTCOLUMNS('Positive',"item",[item])
var _c = SELECTCOLUMNS('Negative',"date",[date (dd/mm/yyyy)])
var _d = SELECTCOLUMNS('Negative',"item",[item])
var _e = SELECTCOLUMNS(FILTER(ALL('transaction'),[date (dd/mm/yyyy)] in _a&&'transaction'[item] in _b),"id",[customer_id])
var _f = SELECTCOLUMNS(FILTER(ALL('transaction'),[date (dd/mm/yyyy)] in _c&&'transaction'[item] in _d),"id",[customer_id])
var _g = EXCEPT(_e,_f)
return IF(SELECTEDVALUE(customer_base[customer_id]) in _g,1)

vjianbolimsft_0-1686547157962.png

Final output:

vjianbolimsft_1-1686547189750.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank You Jianbo,

 

It's work, do you think the dashboard is sustainable with a large amount of data?

Hi @drav11 ,

 

This is a question to which it is difficult to give a definitive answer.
It is difficult to have a specific criteria to define large amount of data.
This requires a case-by-case analysis. The calculations involved in this solution are not complex and usually will not cause problems.

If your problem has been solved, kindly Accept it as the solution. More people will benefit from it.

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.