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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
12Bowers12
Helper V
Helper V

translate SQL code for unique count into Power BI Code unique code

Good morning, everyone,

I have a Power BI data called PolicyData which is imported from SQL dataset PoicyData. IT department gave me the SQL code to get unique count as shown below.
I tried days to write a Measure with Calculated Column and earlier DAX to try to get the count but failed.

I appreciate your help to “translate” this SQL code into a Power BI measure.

Dennis

 

 

select count(distinct Claimant)

from PolicyData f1

where

Account_Date >= '2019-01-01' and

Claimant not in

(select Claimant from PolicyData where

Account_Date >= '2019-01-01' and Record_Type = 'P') and

Claimant in

(select Claimant

from PolicyData

where Account_Date < '2019-05-01'

group by Claimant having sum(Amount) = 0

) ;

 

1 ACCEPTED SOLUTION

Hi, Pattem,

I added one more criteria [TRASN_TYPE] = “INDEM” and tested the all the situations but changing NOT in differetn filtering colujmns but still got the number  which is higher than the SQL result (the unique count is 923). The lowest number based on your code is 1,619.

Had a chance, could you take a look?

Appreciate your help.

Dennis

 

 

L16 CWOP DistinctCount =

VAR Exclusion =

   SELECTCOLUMNS (

       FILTER (

           LossFile,

           LossFile[RECORD_TYPE] = "P"

               && LossFile[TRANS_TYPE] = "INDEM"

               && LossFile[ACCOUNT_DATE] >= DATE ( 2019, 1, 1 )

       ),

       "CLAIMANT", LossFile[CLAIMANT]

   )

VAR Inclusion =

   SELECTCOLUMNS (

       FILTER (

           SUMMARIZE (

                FILTER (

                   LossFile,

                   LossFile[TRANS_TYPE] = "INDEM"

                       && LossFile[ACCOUNT_DATE] < DATE ( 2019, 5, 1 )

               ),

               LossFile[CLAIMANT],

               "Total", SUM ( LossFile[AMOUNT] )

           ),

           [Total] = 0

       ),

       "CLAIMANT", LossFile[CLAIMANT]

   )

RETURN

   CALCULATE (

       DISTINCTCOUNT ( LossFile[CLAIMANT] ),

       FILTER (

           LossFile,

             (LossFile[CLAIMANT]) IN Exclusion

               && NOT(LossFile[CLAIMANT]) IN Inclusion

       )

   )

 

View solution in original post

13 REPLIES 13

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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