Forum Discussion
sun-sboyanapall
4 years agoAdvocate I
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(Co...
- 4 years ago
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-sboyanapall
4 years agoAdvocate 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))smpa01
4 years agoCommunity 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-sboyanapall4 years agoAdvocate I
can you explain your measure too please, in its current form it is hard to understand. smpa01
- smpa014 years agoCommunity Champion
Measure = VAR _base = FILTER ( //#1- returns a table that has no tbl[ID]or tbl[ConvertedDate]or tbl[Date_of_Last_Status_Change__c] as BLANK tbl, tbl[TransactionDate] <> BLANK () && tbl[ConvertedDate] <> BLANK () && tbl[Date_of_Last_Status_Change__c] <> BLANK () ) VAR _left = FILTER ( tbl, tbl[ID] <> BLANK () ) //#2- returns a table that has no tbl[ID] as BLANK VAR _join = EXCEPT ( // #4 - this returns a left anti joined tables between 2 and 3, i.e. return everything from 2 that are not in 3 _left, FILTER ( // #3- previously created base table is further filteres with SQL IS NULL conditions _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 CALCULATE ( //5. DISTINCTCOUNT is performed on the above derived table DISTINCTCOUNT ( tbl[ID] ),_join)- sun-sboyanapall4 years agoAdvocate I
That's clear, Thank you Sir