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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
GuldagerHerDK
Frequent Visitor

Distinct count IDs from query, where IDs NOT in other query

Hi.

I'm having trouble with counting a certain amount of IDs based on a date input.  It works great when i run one query at a time. 

I want to exclude all the ids from VAR DoNotWithtake when counting the id's from VAR withtake

heres my DAX. Do anyone have an idea to what i'm doing wrong?. 

Measure =
VAR SAEndDatePlusOneMonth =
FIRSTDATE ( DATEADD ( Dato[Dato], 1, MONTH ) )
VAR SAStartDatePlusTwoMonths =
FIRSTDATE ( DATEADD ( Dato[Dato], 2, MONTH ) )
VAR DoNotWithtake =
CALCULATE (
DISTINCTCOUNT ( 'Målerpunkt'[Metering Point ID] ),
FILTER (
CALCULATETABLE ( VALUES ( FactSA ), ALL ( 'Dato' ) ),
FactSA[SAStartDate] >= CALCULATE ( MIN ( Dato[Dato] ) )
&& FactSA[SAStartDate] < SAStartDatePlusTwoMonths
&& FactSA[SAEndDate] >= SAEndDatePlusOneMonth
),
FILTER ( SA, NOT ( SA[SA status] IN { "Canceled", "Pending Start" } ) ),
ALL ( Dato[Dato] )
)
VAR withtake =
CALCULATE (
DISTINCTCOUNT( 'Målerpunkt'[Metering Point ID] ),
FILTER (
CALCULATETABLE ( VALUES ( FactSA[SAEndDate] ), ALL ( 'Dato' ) ),
FactSA[SAEndDate] >= CALCULATE ( MIN ( Dato[Dato] ) )
&& FactSA[SAEndDate] < SAEndDatePlusOneMonth
),
FILTER ( SA, SA[SA status] IN { "Stopped", "Closed" } ),
ALL ( Dato[Dato] )
)
return
CALCULATE(withtake,FILTER('Målerpunkt',not('Målerpunkt'[Metering Point ID] in {DoNotWithtake})))


There is typicyally two errors i stump upon. 

1. A table of multiple values was supplied where a single value was expected
2. Function 'CONTAINSROW' does not support comparing values of type integer with values of type text.

Any ideas or tips will be much appreciated

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @GuldagerHerDK ,

In your formula bellow, DoNotWithtake returns a number of count, while MeteringPointID is the content of ID, they dont have an inclusive relationship.

 

vkalyjmsft_0-1636092834217.png

You can modify DoNotWithtake like this:

VAR _Table=

FILTER (

CALCULATETABLE ( VALUES ( FactSA ), ALL ( 'Dato' ) ),

FactSA[SAStartDate] >= CALCULATE ( MIN ( Dato[Dato] ) )

&& FactSA[SAStartDate] < SAStartDatePlusTwoMonths

&& FactSA[SAEndDate] >= SAEndDatePlusOneMonth

),

FILTER ( SA, NOT ( SA[SA status] IN { "Canceled", "Pending Start" } )

)

VAR _DoNotWithtake =

SELECTCOLUMNS(_Table, Metering Point ID, [Metering Point ID])

Best Regards,
Community Support Team _ kalyj

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

3 REPLIES 3
v-yanjiang-msft
Community Support
Community Support

Hi @GuldagerHerDK ,

In your formula bellow, DoNotWithtake returns a number of count, while MeteringPointID is the content of ID, they dont have an inclusive relationship.

 

vkalyjmsft_0-1636092834217.png

You can modify DoNotWithtake like this:

VAR _Table=

FILTER (

CALCULATETABLE ( VALUES ( FactSA ), ALL ( 'Dato' ) ),

FactSA[SAStartDate] >= CALCULATE ( MIN ( Dato[Dato] ) )

&& FactSA[SAStartDate] < SAStartDatePlusTwoMonths

&& FactSA[SAEndDate] >= SAEndDatePlusOneMonth

),

FILTER ( SA, NOT ( SA[SA status] IN { "Canceled", "Pending Start" } )

)

VAR _DoNotWithtake =

SELECTCOLUMNS(_Table, Metering Point ID, [Metering Point ID])

Best Regards,
Community Support Team _ kalyj

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

lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services. Please show the expected outcome.

Hi @lbendlin 🙂

What i want to is count the number of id's from one query (QUERY 1) which are NOT IN in other query, based on query 2s filters.

I have the following two queries.

Query 1: 
 CALCULATE (
DISTINCTCOUNT('Målerpunkt'[Metering Point ID] ),
FILTER (
CALCULATETABLE ( VALUES ( FactSA[SAEndDate] ), ALL ( 'Dato' ) ),
FactSA[SAEndDate] >= CALCULATE ( MIN ( Dato[Dato] ) )
&& FactSA[SAEndDate] < SAEndDatePlusOneMonth
),
FILTER ( SA, SA[SA status] IN { "Stopped", "Closed" } ),
ALL ( Dato[Dato] )
)


Query 2:

CALCULATE (
VALUES('Målerpunkt'[Metering Point ID] ),
FILTER (
CALCULATETABLE ( VALUES ( FactSA ), ALL ( 'Dato' ) ),
FactSA[SAStartDate] >= CALCULATE ( MIN ( Dato[Dato] ) )
&& FactSA[SAStartDate] < SAStartDatePlusTwoMonths
&& FactSA[SAEndDate] >= SAEndDatePlusOneMonth
),
FILTER ( SA, NOT ( SA[SA status] IN { "Canceled", "Pending Start" } ) ),
ALL ( Dato[Dato] )
)

in TSQL i would do something like this:

SELECT '2021-09-01' AS PrimoDato,
AccountBrand,
a.AccountCustomerType,
COUNT(DISTINCT ServicePointMeteringPoint) AS AntalMPChurn
FROM [ElsalgDW].[fact].[SAs] sa
LEFT JOIN ElsalgDW.dim.SA dsa ON sa.SAID = dsa.SAID
LEFT JOIN ElsalgDW.dim.ServicePoint sp ON sa.ServicePointID = sp.ServicePointID
LEFT JOIN ElsalgDW.dim.Account a ON sa.AccountID = a.AccountID
WHERE SAEndDate >= '2021-09-01'
AND SAEndDate < DATEADD(mm, 1, '2021-09-01')
AND ServicePointMeteringPoint NOT IN
(
SELECT DISTINCT
ServicePointMeteringPoint
FROM [ElsalgDW].[fact].[SAs] sa
LEFT JOIN ElsalgDW.dim.SA dsa ON sa.SAID = dsa.SAID
LEFT JOIN ElsalgDW.dim.ServicePoint sp ON sa.ServicePointID = sp.ServicePointID
LEFT JOIN ElsalgDW.dim.Account a ON sa.AccountID = a.AccountID
WHERE(SAstartDate >= '2021-09-01'
AND SAstartDate < DATEADD(mm, 2, '2021-09-01'))
AND SAEndDate >= DATEADD(mm, 1, '2021-09-01')
AND SAStatusName NOT IN('Canceled', 'Pending Start')
)
AND SAStatusName IN('Stopped', 'Closed')
GROUP BY a.AccountBrand,
a.AccountCustomerType;



which would output this.2021-11-03_15h56_22.png




Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.