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
AutoKris
Frequent Visitor

Measure counting unique Report Key based on values in another column

Hi,

I tried to solve it with DAX Calculate, with FIlters and Counting Rows, however cannot get my head around it, even ChatGPT 3.5 nor Bing Chat were able so simply solve this. I think this is easy but cannot get a straight and easy solution.

I have this example table.

 

Report KeyIndexComment
A1 
A2 
B3 
B4R1
B5R1
C6 
C7Single
C8Multiple
C9 
C10 
D11R1
D12 
D13Single
D14 
D15 
E16R1
E17 
E18Multiple
E19 

 

I want to count the number of unique Report Key that have only Comments that are blank or blank and with value R1, meaning the result here should be 2, as only A and B have this combination of lines that have Blank or R1 values in column Comment.

C has Blanks but also Single and Multiple values in Comment column so it should not be counted. D has Blank and R1 however also Single, so it should not be counted. D has Blank but also Single, so not counted, nor E as it has Blank, R1 but also Multiple, so not counted.

 

What should be the Measure formula to obtain this result?

Please help! 🙂 Thank you

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@AutoKris Try this. PBIX is attached below signature. I guess my job is safe from AI for now...

Measure = 
    VAR __Good = { "R1" }
    VAR __Table = FILTER( 'Table', [Comment] <> BLANK() )
    VAR __Bad = DISTINCT(SELECTCOLUMNS(FILTER( __Table, NOT( [Comment] IN __Good ) ), "__Comment", [Comment] ) )
    VAR __BadKeys = SELECTCOLUMNS( FILTER( 'Table', [Comment] IN __Bad), "__ReportKey", [Report Key] )
    VAR __GoodKeys = DISTINCT( SELECTCOLUMNS( FILTER( 'Table', NOT( [Report Key] IN __BadKeys ) ), "__Key", [Report Key] ) )
    VAR __Result = COUNTROWS( __GoodKeys )
RETURN
    __Result

@ 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...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@AutoKris Try this. PBIX is attached below signature. I guess my job is safe from AI for now...

Measure = 
    VAR __Good = { "R1" }
    VAR __Table = FILTER( 'Table', [Comment] <> BLANK() )
    VAR __Bad = DISTINCT(SELECTCOLUMNS(FILTER( __Table, NOT( [Comment] IN __Good ) ), "__Comment", [Comment] ) )
    VAR __BadKeys = SELECTCOLUMNS( FILTER( 'Table', [Comment] IN __Bad), "__ReportKey", [Report Key] )
    VAR __GoodKeys = DISTINCT( SELECTCOLUMNS( FILTER( 'Table', NOT( [Report Key] IN __BadKeys ) ), "__Key", [Report Key] ) )
    VAR __Result = COUNTROWS( __GoodKeys )
RETURN
    __Result

@ 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...

Hi @Greg_Deckler , thanks for the quick reply! Your measure actually worked, by the way, mine below also worked without complex variables 😉 

 

The calculation logic in the measure is as follows:

  • First, calculate the distinct count of Report Keys in the Dump table.
  • Filter the Dump table to only include rows where the following conditions are satisfied:
    • The Report Key matches the current row's Report Key (using EARLIER function). This is done by using two nested FILTER functions.
    • The Comments are either blank or "Reason 1".
    • There are no other non-blank comments for that Report Key. This is done by comparing the count of all comments with the count of blank and "Reason 1" comments for that Report Key. If they are equal, then there are no other non-blank comments for that Report Key.

If all of these conditions are met, the row is included in the filtered table and is counted towards the final distinct count of Report Keys.

 

 

ReportKeyOnlyEmptyCommentOrReason1 = 
CALCULATE(
DISTINCTCOUNT(Table[Report Key]),
FILTER(
Table,
COUNTROWS(
FILTER(
Table,
Table[Report Key] = EARLIER(Table[Report Key])
&& (
Table[Comment] = BLANK()
|| Table[Comment] = "R1"
)
)
) = COUNTROWS(FILTER(Table, Table[Report Key] = EARLIER(Table[Report Key])))
)
)

Please let me know if you think this is also correct?

Thanks 

@AutoKris We'll have to agree to disagree that variables make DAX complex. I find that they make things easier by allowing a top-down coding style and the ability to easily debug things (especially using TOCSV). Purists would say to use a variable over EARLIER but I'm not one of those. Interesting approach and probably the best you can do in order to cram a CALCULTE in there. Wonder which approach is faster at scale...


@ 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...

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.