Forum Discussion
convert SQL(case) to Dax
Hello guys,
I have this query in sql which I am trying to make a measure in powerbi but having issues,
SQL query:
SELECT COUNT (DISTINCT(ID)) -
COUNT( DISTINCT (case when MONTH(ISNULL(ConvertedDate,Date_of_Last_Status_Change__c)) <> MONTH(TransactionDate) and Year(ISNULL(ConvertedDate,Date_of_Last_Status_Change__c)) <> YEAR(TransactionDate) then DocumentNumber end))
TABLE STRUCTURE
| ID | TransactionDate | ConvertedDate | Date_of_Last_Status_Change__c | DocumentNumber |
| 123 | 12/1/2021 | 12/1/2021 | 12/15/2021 | 001 |
145 | 11/1/2021 | 12/1/2021 | 12/15/2021 | 002 |
| 234 | 10/1/2021 | 9/1/2021 | 10/21/2021 | 003 |
| 265 | NULL | 10/1/2021 | 10/15/2021 | NULL |
| NULL | 2/1/2021 | NULL | NULL | 005 |
correct answer:
4 - 2 = 2
Thanks for your help!
sun-sboyanapall can you try this measure
Measure = CALCULATE ( DISTINCTCOUNT ( tbl[ID] ), VAR _base = FILTER ( tbl, tbl[TransactionDate] <> BLANK () && tbl[ConvertedDate] <> BLANK () && tbl[Date_of_Last_Status_Change__c] <> BLANK () ) VAR _left = FILTER ( tbl, tbl[ID] <> BLANK () ) VAR _join = EXCEPT ( _left, FILTER ( _base, MONTH ( COALESCE ( tbl[ConvertedDate], tbl[Date_of_Last_Status_Change__c] ) ) <> MONTH ( tbl[TransactionDate] ) && YEAR ( COALESCE ( tbl[ConvertedDate], tbl[Date_of_Last_Status_Change__c] ) ) <> YEAR ( tbl[TransactionDate] ) ) ) RETURN _join )
8 Replies
- smpa01Community Champion
sun-sboyanapall I don't think the above SQL query returns 2 for the data provided, it returns 4, cause the following part returns 0
COUNT( DISTINCT (case when MONTH(ISNULL(ConvertedDate,Date_of_Last_Status_Change_c)) <> MONTH(TransactionDate) and Year(ISNULL(ConvertedDate,Date_of_Last_Status_Change_c)) <> YEAR(TransactionDate) then DocumentNumber end))breakdown
MONTH(ISNULL(ConvertedDate,Date_of_Last_Status_Change_c)) <> MONTH(TransactionDate) has two matches row 2 and row 3 as {12,11}, {9,10} Year(ISNULL(ConvertedDate,Date_of_Last_Status_Change_c)) <> YEAR(TransactionDate) has no match as all of them are {2021,2021}- sun-sboyanapallAdvocate I
That's correct the correct answer would be 4, I thought I have changed year.
Looking forward to how you would do this with dax.- smpa01Community Champion
sun-sboyanapall can you try this measure
Measure = CALCULATE ( DISTINCTCOUNT ( tbl[ID] ), VAR _base = FILTER ( tbl, tbl[TransactionDate] <> BLANK () && tbl[ConvertedDate] <> BLANK () && tbl[Date_of_Last_Status_Change__c] <> BLANK () ) VAR _left = FILTER ( tbl, tbl[ID] <> BLANK () ) VAR _join = EXCEPT ( _left, FILTER ( _base, MONTH ( COALESCE ( tbl[ConvertedDate], tbl[Date_of_Last_Status_Change__c] ) ) <> MONTH ( tbl[TransactionDate] ) && YEAR ( COALESCE ( tbl[ConvertedDate], tbl[Date_of_Last_Status_Change__c] ) ) <> YEAR ( tbl[TransactionDate] ) ) ) RETURN _join )
- sun-sboyanapallAdvocate I
Thank you, another way I did was created a calculated column, is consumed that is taking care of my case statement.
IsConsumed = var yearTranDate = YEAR(Tbl[TransactionDate]) var monthTranDate = MONTH(Tbl[TransactionDate]) var yearConvDate = YEAR(Tbl[ConvertedDate])) var monthConvDate = MONTH(Tbl[ConvertedDate])) var yeardateLastSC = YEAR(Tbl[Date_of_Last_Status_Change__c])) var monthdateLastSC = MONTH(Tbl[Date_of_Last_Status_Change__c])) RETURN IF(ISBLANK(vLeadConsumedWithSales[ID]), FALSE(), IF(ISBLANK(yearConvDate), yearDateLastSC <> yearTranDate && monthDateLastSC <> monthTranDate, yearConvDate <> yearTranDate && monthConvDate <> monthTranDate))- smpa01Community Champion
sun-sboyanapall well done.
One rule of thumb to always remember with DAX; if you can create a measure, don't create a column.
- sun-sboyanapallAdvocate I
can you explain your measure too please, in its current form it is hard to understand. smpa01