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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
love
Helper I
Helper I

How to count rows with additional logic (countrows/countx)

Hello.

 

I have a data set that looks like this:

 

 

Task	Config.	Time
Task 1	A	20
Task 1	A	60
Task 1	B	214
Task 2	A	8
Task 2	B	215
Task 3	A	67
Task 3	B	21
Task 3	B	57

 

If I create a measure like "= COUNTROWS(Tablename)" it will return 8, because there are 8 rows. What I would like is to only include unique Task + Config combinations in the count, meaning that "Task 1 - A" and "Task 3 - B" are only counted once, instead of twice (because they have 2 different time values, which I would like to not be included in the count) and that the returned value is 6, not 8.

 

I played around a little with COUNTX but was not able to achieve what I am trying.

 

Thank you for your help,

-L

1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey,

 

maybe this measure provides what you are looking for.

Countrows Summarize = 
COUNTROWS(
    SUMMARIZE('Table1'
        ,Table1[Task]
        ,Table1[Config.]
    )
) 

It results to 6.

 

Using SUMMARIZE allows to create a table using just the columns you want.

 

You can also use
ADDCOLUMNS(
    SUMMARIZE(...)

    ,"NewColumnName 1", Expression

    ,...
)

 

For more examples using SUMMARIZE and ADDCOLUMNS please have a look here:

https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns/

 

Hopefully this is what you are looking for

 

Regards

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

4 REPLIES 4
TomMartens
Super User
Super User

Hey,

 

maybe this measure provides what you are looking for.

Countrows Summarize = 
COUNTROWS(
    SUMMARIZE('Table1'
        ,Table1[Task]
        ,Table1[Config.]
    )
) 

It results to 6.

 

Using SUMMARIZE allows to create a table using just the columns you want.

 

You can also use
ADDCOLUMNS(
    SUMMARIZE(...)

    ,"NewColumnName 1", Expression

    ,...
)

 

For more examples using SUMMARIZE and ADDCOLUMNS please have a look here:

https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns/

 

Hopefully this is what you are looking for

 

Regards

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Greg_Deckler
Super User
Super User

You could add a calculated column that does a CONCATENATE of your two columns and then write a measure that does a DISTINCTCOUNT on that new column.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Measure =
COUNTROWS ( SUMMARIZECOLUMNS ( Table1[Task], Table1[Config.] ) )

this measure creates a table containg all distinct values of the task and config columns and then returns the number of rows in said table

 

edit: Tom Martens post does the same method just with summarize and add columns instead of summarizecolumns. They should produce the same result and perform the same, summarizecolumns just has simpler syntax.

 

https://www.sqlbi.com/articles/introducing-summarizecolumns/ to read more about this

Hi @Greg_Deckler@TomMartens & @Anonymous!

 

Thank you all for your super quick replies and the proposed solutions - much appreciated. I tried all of them and they work just fine and provide me with what I need. I ended up using TomMartens proposal, it seemed like the most simple solution for my actual data set.

 

Thanks again and have a nice day!

-L

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.