Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Measure that returns DISTINCT COUNT by Country and Brand in Power BI

I have a table like below:

Country, Advertiser, Brand, Spend
C1,      A1,         B1,    10
C1,      A1,         B2,    5
C1,      A2,         B3,    0
C1,      A2,         B4,    20
C2,      A1,         B1,    8
C2,      A1,         B5,    7
C2,      A2,         B6,    4
C2,      A2,         B3,    3
C2,      A2,         B7,    2
C2,      A3,         B8,    9

I'd like to get the DISTINCT COUNT of Brands by Country, which is simply:

C1 => 4
C2 => 6

How do I create a measure in Power BI that I can embed in a table or a tree-map? I have already tried this:

Measure = COUNTX(DISTINCT(sourceTable),sourceTable[Brand])

which returns total Brand count (not DISTINCT COUNT) per Country...

I have also tried below and it didn't work:

DistinctBrandCount =
    CALCULATE(DISTINCTCOUNT(SampleDataForDashboard[Brand]),
        GROUPBY(SampleDataForDashboard, SampleDataForDashboard[Country]))
like image 688
user1330974 Avatar asked May 18 '18 15:05

user1330974


People also ask

How do I get a group by count in Power BI?

Power BI Group By count In the Power Query editor, select the Product column -> Home tab and select the Group by icon from the ribbon. Now Group BY window will open, select the Basic option -> provide the new Column name -> select the operation as Count rows. Click on OK.

How does DAX calculate distinct count?

You can use columns containing any type of data. When the function finds no rows to count, it returns a BLANK, otherwise it returns the count of distinct values. DISTINCTCOUNT function counts the BLANK value. To skip the BLANK value, use the DISTINCTCOUNTNOBLANK function.

What is the difference between count and count distinct in Power BI?

Counts the number of values in that field that aren't blank. Count (Distinct). Counts the number of different values in that field.


1 Answers

StelioK is correct, you can just use a measure like this:

DistinctBrands = DISTINCTCOUNT(sourceTable[Brand])

When you put the measure into a table with Country on the rows, the filter context passed to the measure only includes the country for that row.

Table

If you want to keep all of your other columns as well, you can write you measure like this:

DistinctBrands = CALCULATE(DISTINCTCOUNT(sourceTable[Brand]),
                     ALLEXCEPT(sourceTable, sourceTable[Country]))

This removes all filter context except for the Country and then calculates the distinct count.

enter image description here

like image 87
Alexis Olson Avatar answered Sep 30 '22 11:09

Alexis Olson