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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.