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
o59393
Post Prodigy
Post Prodigy

How to distinct count based on 2 different columns

Hi all

 

How can I do a distinct count for the column "month 445" where it depends on the column "bottler"

distinct count.JPG

 

In the image above, the result desired for bottler "AB" should be 7, since for that "AB" bottler there are 7 months of value.

 

For "FCR" the result desired is 4.

 

How can I get right my calculated column called "distinct count"

 

PBI: https://1drv.ms/u/s!ApgeWwGTKtFdhydAQ7n3Th2KpUHl?e=GK3MCU

 

Thanks.

3 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

This should probably be done as a measure, but here is a column expression you can use.

 

NewColumn = CALCULATE(DISTINCTCOUNT(Sheet1[Month 445]), ALLEXCEPT(Sheet1, Sheet1[L1.2 - Bottler]))

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

Greg_Deckler
Super User
Super User

@o59393 

Column =
  VAR __Table = SELECTCOLUMNS(FILTER('Table',[L1.2 - Bottler]=EARLIER([L1.2 - Bottler])),"Month",[Month 445])
RETURN
  COUNTROWS(DISTINCT(__Table))

@ 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

I see the issue.  Columns do not recalculate or respond to filters (either in data view or slicers in visual).  If you want it to consider the code too, you can adapt the code to this to get a result of 7.  Otherwise you need to make a measure instead and use it in your visuals.

 

NewColumn =
CALCULATE (
DISTINCTCOUNT ( 'Import de capacity'[[Month 445]]] ),
ALLEXCEPT (
'Import de capacity',
'Import de capacity'[[L1.2 - Bottler]]],
'Import de capacity'[[ BPP Code ]]]
)
)
 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

7 REPLIES 7
Greg_Deckler
Super User
Super User

@o59393 

Column =
  VAR __Table = SELECTCOLUMNS(FILTER('Table',[L1.2 - Bottler]=EARLIER([L1.2 - Bottler])),"Month",[Month 445])
RETURN
  COUNTROWS(DISTINCT(__Table))

@ 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  and @mahoneypat 

 

Both worked on the test pbix.

 

When I go to my real pbi and use both formulas I get an "8" again.

 

32432424g.JPG

 

You may filter by code 232031405 and bottler "abasa" and "femsa cr" to see the example of the test pbix and image above.

 

Any idea what could be wrong?

 

Thanks!

I see the issue.  Columns do not recalculate or respond to filters (either in data view or slicers in visual).  If you want it to consider the code too, you can adapt the code to this to get a result of 7.  Otherwise you need to make a measure instead and use it in your visuals.

 

NewColumn =
CALCULATE (
DISTINCTCOUNT ( 'Import de capacity'[[Month 445]]] ),
ALLEXCEPT (
'Import de capacity',
'Import de capacity'[[L1.2 - Bottler]]],
'Import de capacity'[[ BPP Code ]]]
)
)
 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Correct @mahoneypat !

 

I added that and worked.

 

Thank you both @Greg_Deckler and @mahoneypat 

 

 

@mahoneypat - Nice one, I totally missed that he was wanting this to be dynamic or have an additional filter on it.


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

@o59393 - It's because there really are 8 distinct values of Month 445. Filter L1.2 - Bottler to Coca-Cola Femsa CO, both the columns display 8

Greg_Deckler_0-1598578018731.png

That's because there are 8 distinct values, count them:

Greg_Deckler_1-1598578097909.png

 

8


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

This should probably be done as a measure, but here is a column expression you can use.

 

NewColumn = CALCULATE(DISTINCTCOUNT(Sheet1[Month 445]), ALLEXCEPT(Sheet1, Sheet1[L1.2 - Bottler]))

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.