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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
msuser48
Helper I
Helper I

Calculate COUNT of DISTINCT values with GROUP BY

I have two tables, where I am trying to make the right CalculatedColumn in table 2:

 

Table 1:

IdDifference
11
11
21
21
32
32

Table 2 (DESIRED OUTPUT TABLE)

NumbersCalculatedColumn
12
21
30

 

I need the right code for my "CalculatedColumn" in table 2.

 

The goal is to count the DISTINCT "Difference" value from table 1 for EACH Id, and add these together.

- So, you can see in table 1, both ID 1 & ID 2 have the distinct value of 1 EACH.

- 1+1 = 2, therefore the calculated column should put 2 as value where "Numbers" column = 1.

 

 

My attempt:

Counter =
SWITCH (
TRUE(),
Table2[Numbers] = 1, CALCULATE(SUM(Table1[Difference]),Table1[Difference] = 1),

Table2[Numbers] = 2, CALCULATE(SUM(Table1[Difference]),Table1[Difference] = 2
)

.. and so on.

1 ACCEPTED SOLUTION
v-jialluo-msft
Community Support
Community Support

Hi @msuser48 ,

 

Please follow these steps:

(1) Create a new column

Counter =
SWITCH (
TRUE(),
'Table 2'[Number] = 1, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 1),
'Table 2'[Number] = 2, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 2),
'Table 2'[Number] = 3, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 3)
)

 

(2)Final output

vjialluomsft_0-1669773363439.png

 

 

Best Regards,

Gallen Luo

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

1 REPLY 1
v-jialluo-msft
Community Support
Community Support

Hi @msuser48 ,

 

Please follow these steps:

(1) Create a new column

Counter =
SWITCH (
TRUE(),
'Table 2'[Number] = 1, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 1),
'Table 2'[Number] = 2, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 2),
'Table 2'[Number] = 3, CALCULATE(DISTINCTCOUNT('Table 1' [ID]),'Table 1' [Difference] = 3)
)

 

(2)Final output

vjialluomsft_0-1669773363439.png

 

 

Best Regards,

Gallen Luo

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.