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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
frknklcsln
Helper II
Helper II

how to get total distinct count in Power BI

I have 2 tables.

Sector and ID columns are coming from 10-CSV file. Reason and Included columns are coming from 07-CSV file.

 

Ekwc5.png

Filter with Included column as "Included". I do this filter by applying the filter to the table with the filters tab, not with the DAX.

 

Here is what I did so far:

Measure = 

var reason_all = CALCULATE(DISTINCTCOUNT('10-CSV'[ID]))
var reason_blank = CALCULATE(DISTINCTCOUNT('10-CSV'[ID]),'07-CSV'[Reason] = BLANK())

var reason_A = CALCULATE(DISTINCTCOUNT('10-CSV'[ID]), '07-CSV'[Reason] = "A")

var reason_B = CALCULATE(DISTINCTCOUNT('10-CSV'[ID]), '07-CSV'[Reason] = "B")

var reason_C = CALCULATE(DISTINCTCOUNT('10-CSV'[ID]), '07-CSV'[Reason] = "C")

RETURN

VALUE(
IF(
    ISFILTERED('07-CSV'[Reason]),

        IF(
            VALUES(
            '07-CSV'[Reason]) = BLANK(),
            reason_blank/reason_all ,
            IF(
                VALUES(
                    '07-CSV'[Reason]) = "A",
                    reason_A/reason_all,
                IF(
                    VALUES(
                        '07-CSV'[Reason]) = "B",
                        reason_B/reason_all,
                        IF(
                            VALUES(
                                '07-CSV'[Reason]) = "C",
                                reason_C/reason_all
            ))))))

With this, I am always getting is 1. The problem is reason_all is not getting out correctly. It comes same as the other reasons. I think when I breakdown the table with the Reason column, it breakdown also.

 

 
 

H7Mk3.png

 

But it should be like this in order to get the total distinct count of a sector:

 

BqIDz.png

With this, every reason will have its percentage of distribution in the same sector. For example, A reason in Retail should show the 8/412 = 0,019. Blank reason in Retail should show the 396/412 = 0,96.

 

ID column is in text type including values like "100021", "100023".

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

Hi  @frknklcsln ,

I created some data:

07-CSV file:

vyangliumsft_0-1657592923683.png

10-CSV file:

vyangliumsft_1-1657592923684.png

Here are the steps you can follow:
1. Create calculated table.

Table1 =
var _table1=
SUMMARIZE('10-CSV file',
'10-CSV file'[ID],
'10-CSV file'[Sector],"Reason",
CALCULATE(MAX('07-CSV file'[Reason]),FILTER(ALL('07-CSV file'),
'07-CSV file'[ID]='10-CSV file'[ID])))
var _table2=
ADDCOLUMNS(_table1,
"Count",
COUNTX(
    FILTER(_table1,
    [Sector]=EARLIER('10-CSV file'[Sector])&&[Reason]=EARLIER([Reason])),[ID]))
var _table3=
SUMMARIZE(
    _table2,
    [Sector],[Reason],[Count])
return
ADDCOLUMNS(
    _table3,
    "Sum",
    SumX(
    FILTER(_table3,
    [Sector]=EARLIER('10-CSV file'[Sector])),[Count]))

vyangliumsft_2-1657592923687.png

2. Create measure.

COUNTid =
CALCULATE(
    MAX('Table1'[Count]),
    FILTER(ALL('Table1'),
    'Table1'[Sector]=MAX('10-CSV file'[Sector])&&'Table1'[Reason]=MAX('07-CSV file'[Reason])))
SUMid =
CALCULATE(
    SUM('Table1'[Sum]),
    FILTER(ALL('Table1'),
    'Table1'[Sector]=MAX('10-CSV file'[Sector])&&'Table1'[Reason]=MAX('07-CSV file'[Reason])))
Desired Output =
DIVIDE(
    [COUNTid],
    [SUMid])

3. Result:

vyangliumsft_3-1657592923688.png

If you need pbix, please click here.

Best Regards,

Liu Yang

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

5 REPLIES 5
v-yangliu-msft
Community Support
Community Support

Hi  @frknklcsln ,

I created some data:

07-CSV file:

vyangliumsft_0-1657592923683.png

10-CSV file:

vyangliumsft_1-1657592923684.png

Here are the steps you can follow:
1. Create calculated table.

Table1 =
var _table1=
SUMMARIZE('10-CSV file',
'10-CSV file'[ID],
'10-CSV file'[Sector],"Reason",
CALCULATE(MAX('07-CSV file'[Reason]),FILTER(ALL('07-CSV file'),
'07-CSV file'[ID]='10-CSV file'[ID])))
var _table2=
ADDCOLUMNS(_table1,
"Count",
COUNTX(
    FILTER(_table1,
    [Sector]=EARLIER('10-CSV file'[Sector])&&[Reason]=EARLIER([Reason])),[ID]))
var _table3=
SUMMARIZE(
    _table2,
    [Sector],[Reason],[Count])
return
ADDCOLUMNS(
    _table3,
    "Sum",
    SumX(
    FILTER(_table3,
    [Sector]=EARLIER('10-CSV file'[Sector])),[Count]))

vyangliumsft_2-1657592923687.png

2. Create measure.

COUNTid =
CALCULATE(
    MAX('Table1'[Count]),
    FILTER(ALL('Table1'),
    'Table1'[Sector]=MAX('10-CSV file'[Sector])&&'Table1'[Reason]=MAX('07-CSV file'[Reason])))
SUMid =
CALCULATE(
    SUM('Table1'[Sum]),
    FILTER(ALL('Table1'),
    'Table1'[Sector]=MAX('10-CSV file'[Sector])&&'Table1'[Reason]=MAX('07-CSV file'[Reason])))
Desired Output =
DIVIDE(
    [COUNTid],
    [SUMid])

3. Result:

vyangliumsft_3-1657592923688.png

If you need pbix, please click here.

Best Regards,

Liu Yang

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

hi @v-yangliu-msft again, I want to add a new filter to the table1. How can I apply this?

 

There is  a transport mode column in the 07-csv file. And there is another column called Region in the 04-csv file. So the filter is going to be like this:

 

If the '07-CSV'[transport mode] = "Air",
Then '04-CSV'[Region] <> "Middle East"  &  '04-CSV'[Region] <> "Europe"

 

I tried to add this to the Table1 table that you created but I couldnt.

 

07-csv file has the ID column which I use as the primary key for lookup. 10-CSV has ID and Sec_ID column. Same ID number may contain different Sec_ID numbers. You can think about invoices. Same invoice number may contain different delivery numbers etc.

04-csv has the Sec_ID column. So, I cant create a relationship directly between the 04-csv file and the 07-csv file.

 

Thank you very much again.

Hi @v-yangliu-msft , you are an angel. It worked, thank you.

amitchandak
Super User
Super User

@frknklcsln , try a measure like

 

Divide(
CALCULATE(DISTINCTCOUNT('10-CSV'[ID]), filter(Allselected('07-CSV') , '07-CSV'[Reason] = max('07-CSV'[Reason]))
, Filter(allselected('10-CSV') , [ID] = max('10-CSV'[ID]) ))
, CALCULATE(DISTINCTCOUNT('10-CSV'[ID])
, Filter(allselected('10-CSV') , [ID] = max('10-CSV'[ID]) ))
)

 

 

Seem like there are two tables

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thank you for your time and effort.

Unfortunately, it still shows as 1. The numerator part of the divide function returns blank and the denominator part of the function returns as like mine(2. pic). It gave me an idea tho. Thank you again.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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