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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
flipfis
Frequent Visitor

Count matching values in different queries

Situation

We offer two different plans to our customers. I imported the data from this via a web query in JSON. The two subscriptions both have their own data table.

 

Problem

We want to calculate which customers have both subscriptions by searching based on a customer id. So basically we would like to see how many rows have the same customer id in both datasheets to calculate the number of customers who have both plans.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @flipfis ,

I created the data:

Subscriptions1:

v-yangliu-msft_0-1622092883635.png

Subscriptions2:

v-yangliu-msft_1-1622092883640.png

Here are the steps you can follow:

1. Create calculated column

Flag1 =
var _plan=SELECTCOLUMNS(FILTER(ALL(Subscriptions1),[Customer]=EARLIER([Customer])),"1",[Plan])
return
IF("PlanA" in _plan&&"PlanB" in _plan,1,0)
Flag2 =
var _plan=SELECTCOLUMNS(FILTER(ALL('Subscriptions2'),[Customer]=EARLIER([Customer])),"1",[Plan])
return
IF("PlanA" in _plan&&"PlanB" in _plan,1,0)

2. Create measure.

Flag all =
var _flag1=MAX('Subscriptions1'[Flag1])
var _flag2=CALCULATE(MAX('Subscriptions2'[Flag2]),FILTER(ALL('Subscriptions2'),[Customer]=MAX('Subscriptions1'[Customer])))
return
IF(_flag1=1&&_flag1=_flag2,1,0)

3. Put measure[Flag all] into Filter, set is=1, and Apply filter.

v-yangliu-msft_2-1622092883658.png

4. Result:

v-yangliu-msft_3-1622092883665.png

 

Best Regards,

Liu Yang

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

6 REPLIES 6
Anonymous
Not applicable

Hi  @flipfis ,

I created the data:

Subscriptions1:

v-yangliu-msft_0-1622092883635.png

Subscriptions2:

v-yangliu-msft_1-1622092883640.png

Here are the steps you can follow:

1. Create calculated column

Flag1 =
var _plan=SELECTCOLUMNS(FILTER(ALL(Subscriptions1),[Customer]=EARLIER([Customer])),"1",[Plan])
return
IF("PlanA" in _plan&&"PlanB" in _plan,1,0)
Flag2 =
var _plan=SELECTCOLUMNS(FILTER(ALL('Subscriptions2'),[Customer]=EARLIER([Customer])),"1",[Plan])
return
IF("PlanA" in _plan&&"PlanB" in _plan,1,0)

2. Create measure.

Flag all =
var _flag1=MAX('Subscriptions1'[Flag1])
var _flag2=CALCULATE(MAX('Subscriptions2'[Flag2]),FILTER(ALL('Subscriptions2'),[Customer]=MAX('Subscriptions1'[Customer])))
return
IF(_flag1=1&&_flag1=_flag2,1,0)

3. Put measure[Flag all] into Filter, set is=1, and Apply filter.

v-yangliu-msft_2-1622092883658.png

4. Result:

v-yangliu-msft_3-1622092883665.png

 

Best Regards,

Liu Yang

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

flipfis
Frequent Visitor

@amitchandak Thank you very much for your quick reply! But I'm not quite sure what to do with your solution. I'm quite new to BI, sorry. I don't know where to create these things. It would be great if you could explain it a little bit more 🙂 

@flipfis , You need to have a common customer table, assumed PlanAData and PlanBData are your tables with the customer as one of the columns

 

New Table Customer 

 

Customer = distinct(Union(distinct(PlanAData[Customer),distinct(PlanBData[Customer)))

 

Join the new table to another two tables on Customer 

 

Create three measure as I suggested in last post, thrid measure is what need.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak So I now created a new table and used your forumula from the video:

BothMemberships = DISTINCT(UNION(ALL('PlanA'[customer_id]),ALL('PlanB'[customer_id])) )
 
That works like a charm, but I really would like to apply a filter inside that formula. I want to exclude the memberships that have the status: "cancelled" and "pending". So I only want to get the active memberships and THEN count the duplicates. I'm still not quite sure how to count the duplicates via DAX. 

When I try to create a new table and enter the measure it's giving me the following error: The expression specified in the query is not a valid table expression.

 

PlanA = count('MembersPlanA'[customer_id])
amitchandak
Super User
Super User

@flipfis , You need a common customer table

 

You need have measure from both tables like .

PlanA = count(PlanAData[Customer])
PlanB = count(PlanBData[Customer])

 

Then create a measure and display it with customer from common customer table

 

Countx(filter(Values(Customer[Customer]), not(isblank([PlanA])) && not(isblank([PlanB]))), [Customer])

 

Common Table: https://www.youtube.com/watch?v=Bkf35Roman8&list=PLPaNVDMhUXGaaqV92SBD5X2hk3TMNlHhb&index=19

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.