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
Anonymous
Not applicable

Requirement of converting the SQL query to DAX

I need the following SQL script in DAX format.

 

Requirement: I have a table called Transactions which has 3 columns ID, [Billing Type] and [Year-Month] as mentioned below. I need to find the distinct count of ID by [Year-Month] where [Billing Type] = "Credit Card" and the distinct count of ID >= 3.

 

Transactions Table (sample data):

   ID       Billing Type  Year-Month

1Online2017-01
2Credit Card2017-01
2Credit Card2017-01
3Credit Card2017-01
4Credit Card2017-01
5Credit Card2017-01
6Credit Card2017-02
6Credit Card2017-02
7Credit Card2017-02
8Credit Card2017-02
9Credit Card2017-02
10Credit Card2017-02
1Credit Card2017-03
1Online2017-03
2Credit Card2017-03
2Credit Card2017-03
3Credit Card2017-03
4Credit Card2017-03
4Credit Card2017-04
4Credit Card2017-04
14Online2017-04
12Online2017-04
22Credit Card2017-05
34Credit Card2017-06
45Online2017-06

 

Select * From(Select distinct Count([ID]) as [RowCount] ,[Year-Month] from Transactions where [Billing Type] = 'Credit Card' GROUP BY [Year-Month] ) as Base Where Base.[RowCount] >= 3

When I run the above SQL code I get the following output.

Expected Output:

RowCount Year-Month

42017-01
52017-02
42017-03

 

Now I need to get the same result using DAX. Can someone please help me to get the same using DAX?

1 ACCEPTED SOLUTION

@Anonymous

 

I gave you a calculated table earlier.

 

Here is the MEASURE

 

Row_Count =
VAR mycount =
    CALCULATE (
        DISTINCTCOUNT ( Transactions[ID] ),
        Transactions[Billing Type] = "Credit Card"
    )
RETURN
    IF ( Mycount >= 3, mycount )

Regards
Zubair

Please try my custom visuals

View solution in original post

4 REPLIES 4
Zubair_Muhammad
Community Champion
Community Champion

HI @Anonymous

 

Try this

 

Go to Modelling Tab>> NEW TABLE

 

Table =
FILTER (
    SUMMARIZE (
        Transactions,
        Transactions[Year-Month],
        "Row Count", CALCULATE (
            DISTINCTCOUNT ( Transactions[ID] ),
            Transactions[Billing Type] = "Credit Card"
        )
    ),
    [Row Count] >= 3
)

Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Hi @Zubair_Muhammad,

 

Thanks for your help!

 

I need to use this as New Measure of Power BI.

When I use your script, I'm getting the following error.

 

The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.

@Anonymous

 

I gave you a calculated table earlier.

 

Here is the MEASURE

 

Row_Count =
VAR mycount =
    CALCULATE (
        DISTINCTCOUNT ( Transactions[ID] ),
        Transactions[Billing Type] = "Credit Card"
    )
RETURN
    IF ( Mycount >= 3, mycount )

Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Hi @Zubair_Muhammad

 

Can you also help me with some other requirement?

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.