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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
QAA91
Regular Visitor

Distinct Count

Hi,

 

I have sample of data that looks like this:

 

Table A

StoreFruit 1Fruit 2Fruit 3Fruit 4Fruit 5
Aorangeorangeapple banana
Bappledragonfruitbananapeachplum
Cpearjackfruitpearpearjackfruit
Dwatermelonrapsberrygrape grape

 

And I would like to create a new table where the output looks like this:

Table B

StoreDistinct Fruit By RowsTotal Distinct Fruit For All The Store
A311
B5 
C2 
D3 

 

Many thanks for your help.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Picture1.png

 

New Table B = 
ADDCOLUMNS (
    VALUES ( 'Table A'[Store] ),
    "@DistinctFruitByRow",
        COUNTROWS (
            SUMMARIZE (
                FILTER (
                    UNION (
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 1] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 2] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 3] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store],"@Fruit", 'Table A'[Fruit 4] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 5] )
                    ),
                    [@Store] = EARLIER ( 'Table A'[Store] )
                        && [@Fruit] <> BLANK ()
                ),
                [@Fruit]
            )
        ),
    "@TotalDistinctFruit",
        IF (
            'Table A'[Store] = "A",
            COUNTROWS (
                SUMMARIZE (
                    FILTER (
                        UNION (
                            SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 1] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 2] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 3] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store],"@Fruit", 'Table A'[Fruit 4] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 5] )
                        ),
                        [@Fruit] <> BLANK ()
                    ),
                    [@Fruit]
                )
            )
        )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Picture1.png

 

New Table B = 
ADDCOLUMNS (
    VALUES ( 'Table A'[Store] ),
    "@DistinctFruitByRow",
        COUNTROWS (
            SUMMARIZE (
                FILTER (
                    UNION (
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 1] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 2] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 3] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store],"@Fruit", 'Table A'[Fruit 4] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 5] )
                    ),
                    [@Store] = EARLIER ( 'Table A'[Store] )
                        && [@Fruit] <> BLANK ()
                ),
                [@Fruit]
            )
        ),
    "@TotalDistinctFruit",
        IF (
            'Table A'[Store] = "A",
            COUNTROWS (
                SUMMARIZE (
                    FILTER (
                        UNION (
                            SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 1] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 2] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 3] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store],"@Fruit", 'Table A'[Fruit 4] ),
                        SELECTCOLUMNS ( 'Table A',"@Store", 'Table A'[Store], "@Fruit", 'Table A'[Fruit 5] )
                        ),
                        [@Fruit] <> BLANK ()
                    ),
                    [@Fruit]
                )
            )
        )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
amitchandak
Super User
Super User

@QAA91 ,I think it is better, you unpivot the fruits column and then these calculation would be easy

https://radacad.com/pivot-and-unpivot-with-power-bi

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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