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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register 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

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
March Fabric Community Update

Fabric Community Update - March 2024

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

Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.