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
win_anthony
Resolver III
Resolver III

Count in SQL to DAX

I am able to get a count in SQL. Trying to do the same with DAX measure is not resulting in the correct output. How would you translate the following: 

SELECT store_num, COUNT(emp_id)-1 AS alert_count
FROM store_loc
GROUP BY store_num
HAVING COUNT(emp_id) > 1;

Any advice on translating the above into a measure will be greatly appreciated. 

1 ACCEPTED SOLUTION

Hi @win_anthony ,

If you want to use measure and matrix, you can create this measure:

alert_count =
COUNT ( store_loc[emp_id] ) - 1

Put it in Matrix, and create another measure like this, set its value as 1 and put it in the visual filter:

visual control =
IF ( [alert_count] > 1, 1, 0 )

matrix.png

If you do not want to use this filter measure, you can filter it directly by [alert_count]:
filter.png

Attached a sample file in the below, hopes to help you.

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-yingjl
Community Support
Community Support

Hi @win_anthony ,

Based on your description, I have created this simple sample in sql server and run the sql statement to get the initial expected output:

sample tablesample tablesql resultsql result

To get the same result in power bi, you can create this calculated table by DAX:

Table =
FILTER (
    SUMMARIZE (
        'store_loc',
        store_loc[store_num],
        "alert_count", COUNT ( store_loc[emp_id] ) - 1
    ),
    [alert_count] > 1
)

re.png

Attached a sample file in the below, hopes to help you.

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-yingjl Thank you for your support. This is what I am looking for but is this possible to execute just as a measure and not as a calculated table? My ultimate goal is to create a matrix table that will be dynamic. Please let me know if I am to vague and I can try to elaborate with more detail. 

Hi @win_anthony ,

If you want to use measure and matrix, you can create this measure:

alert_count =
COUNT ( store_loc[emp_id] ) - 1

Put it in Matrix, and create another measure like this, set its value as 1 and put it in the visual filter:

visual control =
IF ( [alert_count] > 1, 1, 0 )

matrix.png

If you do not want to use this filter measure, you can filter it directly by [alert_count]:
filter.png

Attached a sample file in the below, hopes to help you.

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ashish_Mathur
Super User
Super User

Hi,

Share some data, describe the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
CNENFRNL
Community Champion
Community Champion

Hi, @win_anthony , you might want to try creating a calculated table this way,

 

Summary = 
SUMMARIZECOLUMNS (
    store_loc[store_num],
    FILTER (
        DISTINCT ( store_loc[store_num] ),
        CALCULATE ( COUNTROWS ( store_loc ) ) > 1
    ),
    "alert_count", COUNTROWS ( store_loc ) - 1
)

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

parry2k
Super User
Super User

@win_anthony add following measure and then use it in visual left filter

 

Count = COUNTROWS ( Table ) - 1

 

add a table visual, put store number and this new measure and in visual left filter choose where count > 1

 

Check my latest blog post Year-2020, Pandemic, Power BI and Beyond to get a summary of my favourite Power BI feature releases in 2020

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.