Forum Discussion
Replicate this SQL query in DAX
Hello everyone. I wanted to know if there was a way to replicate, in mesasures, the following SQL queries in DAX, so that it looks as similar as possible to this:
Measure =
var variable1 = select customer from T1 where Filter1 and Filter2
var variable2 = select customer from T1 where Filter2 and Filter3
return
select count(customer) from variable2 where customer in (or not in) (select customer from variable1)
Or
Measure =
select count(customer) from T1
where Filter and Filter and customer in (
select customer from T1 where Filter and YFilter)
So far everything I've tried to do hasn't worked 🙃
Thanks!
Hi cris1196
The following shall return unique count of customers.
Common Customers = VAR T1 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[first_order] = "True", 'Table'[YearMonth] = 202201 ) VAR T2 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[Day] = 1, 'Table'[YearMonth] = 202201 ) RETURN COUNTROWS ( INTERSECT ( T2, T1 ) )New Customers = VAR T1 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[first_order] = "True", 'Table'[YearMonth] = 202201 ) VAR T2 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[Day] = 1, 'Table'[YearMonth] = 202201 ) RETURN COUNTROWS ( EXCEPT ( T2, T1 ) )
7 Replies
- tamerj1Community Champion
Hi cris1196
The following shall return unique count of customers.
Common Customers = VAR T1 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[first_order] = "True", 'Table'[YearMonth] = 202201 ) VAR T2 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[Day] = 1, 'Table'[YearMonth] = 202201 ) RETURN COUNTROWS ( INTERSECT ( T2, T1 ) )New Customers = VAR T1 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[first_order] = "True", 'Table'[YearMonth] = 202201 ) VAR T2 = CALCULATETABLE ( VALUES ( 'Table'[Cutomer] ), 'Table'[Day] = 1, 'Table'[YearMonth] = 202201 ) RETURN COUNTROWS ( EXCEPT ( T2, T1 ) )- cris1196Helper I
It worked, thanks! One last question, is there any way to "automate" this? That is, for example, I have twelve 7 yearmonth values (from 202101 to 202107). I know that if I weren't using a calculate, I could create a variable that is selectedvalue(T1[YearMonth]) and then use it as a filter, but in a "calculatedtable" it wouldn't work and I don't know if there is a way to emulate that.
So at least, if I manage to get the year column to recognize me, I only have to create 31 variables (one per day), instead of 31 variables for each year/month/yearmonth
- CNENFRNLCommunity Champion
The [first_order] column is redundant.
For fun only, a showcase of powerful Excel worksheet formula,