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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
sboinala
Regular Visitor

Count values

I have two tables

Customers
a
b
c
d
e
f

 

Account Type:

CustomerFuel
aGas
aElec
bGas
cGas
cElec
dGas
dElec
eElec
eElec
fGas
fGas

 

Required Output

CustomerGas & Elec CountsGas Only CountElec Only Count
a1  
b01 
c1  
d1  
e0 1
f 1 

 

The Visulation matrix would be :

Gas & Elec Counts3
Gas  Counts2
Elec Counts1
1 ACCEPTED SOLUTION
v-gizhi-msft
Community Support
Community Support

Hi,

 

Please try these three measures first:

Gas & Elec Counts = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) > 1,
            1,
            0
        )
    )
)
Gas Only Count = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) = 1
                && CALCULATE (
                    MAX ( 'Account Type'[Fuel] ),
                    FILTER (
                        ALLSELECTED ( 'Account Type' ),
                        'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                    )
                ) = "Gas",
            1,
            0
        )
    )
)
Elec Only Count = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) = 1
                && CALCULATE (
                    MAX ( 'Account Type'[Fuel] ),
                    FILTER (
                        ALLSELECTED ( 'Account Type' ),
                        'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                    )
                ) = "Elec",
            1,
            0
        )
    )
)

Then create a slicer table by Enter Data:

85.PNG

Then try this count measure:

Count =
SUMX (
    DISTINCT ( 'Slicer Table'[Category] ),
    CALCULATE (
        SWITCH (
            MAX ( 'Slicer Table'[Category] ),
            "Elec Counts", [Elec Only Count],
            "Gas  Counts", [Gas Only Count],
            "Gas & Elec Counts", [Gas & Elec Counts]
        )
    )
)

The result shows:

90.PNG

91.PNG

Here is my test pbix file:

pbix 

Hope this helps.

 

Best Regards,

Giotto Zhi

View solution in original post

4 REPLIES 4
v-gizhi-msft
Community Support
Community Support

Hi,

 

Please try these three measures first:

Gas & Elec Counts = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) > 1,
            1,
            0
        )
    )
)
Gas Only Count = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) = 1
                && CALCULATE (
                    MAX ( 'Account Type'[Fuel] ),
                    FILTER (
                        ALLSELECTED ( 'Account Type' ),
                        'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                    )
                ) = "Gas",
            1,
            0
        )
    )
)
Elec Only Count = 
SUMX (
    DISTINCT ( Customer[Customers] ),
    CALCULATE (
        IF (
            CALCULATE (
                DISTINCTCOUNT ( 'Account Type'[Customer&Fuel] ),
                FILTER (
                    ALLSELECTED ( 'Account Type' ),
                    'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                )
            ) = 1
                && CALCULATE (
                    MAX ( 'Account Type'[Fuel] ),
                    FILTER (
                        ALLSELECTED ( 'Account Type' ),
                        'Account Type'[Customer] IN FILTERS ( Customer[Customers] )
                    )
                ) = "Elec",
            1,
            0
        )
    )
)

Then create a slicer table by Enter Data:

85.PNG

Then try this count measure:

Count =
SUMX (
    DISTINCT ( 'Slicer Table'[Category] ),
    CALCULATE (
        SWITCH (
            MAX ( 'Slicer Table'[Category] ),
            "Elec Counts", [Elec Only Count],
            "Gas  Counts", [Gas Only Count],
            "Gas & Elec Counts", [Gas & Elec Counts]
        )
    )
)

The result shows:

90.PNG

91.PNG

Here is my test pbix file:

pbix 

Hope this helps.

 

Best Regards,

Giotto Zhi

az38
Community Champion
Community Champion

Hi @sboinala 

I see 2 required outputs in your post? what do you need exactly?

for first matrix just creatte a visual matrix, drop Customer to rows field, Fuel into Columns, Customer to values and set Values aggregation parameter as Count (Distinct)

 

for second, fuel - as rows, customer as Values and the same set Values aggregation parameter as Count (Distinct)


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

The output i am looking for is :

Gas & Elec3
Gas2
Elec1

 

I have tried the below ,however i couldnt get the first value  Gas & Elec

az38
Community Champion
Community Champion

@sboinala 

create a column

FuelType = 
var _isGas = CALCULATE(COUNTROWS('Table'),ALLEXCEPT('Table','Table'[Customer]),'Table'[Fuel]="Gas")
var _isElec = CALCULATE(COUNTROWS('Table'),ALLEXCEPT('Table','Table'[Customer]),'Table'[Fuel]="Elec")
RETURN
SWITCH(TRUE(),
_isElec > 0 && _isGas > 0, "Gas & Elec",
_isElec > 0, "Elec Only",
_isGas > 0, "Gas Only"
)

then add new column FuelType as rows and Count(Distinct) of Customers as Value


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.