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
chela199
New Member

Add column with distinct count and conditions

Hello 

I'm facing some issues finding way to solve how to create a column that does the following:

A. Count distinct values according to a category, for example, count of different ids for pants, sweaters and t-shirts. 

B. Then take every distinct value per category and multiply by a fixed value like pants = 5, sweaters= 2, and t shirts=1 

C. Count distinct values of all categories.

D. Take point C and multiply by a fixed value according to a category like in B. 

E. Take the product of B/D and multiply by 100. 

Is this possible? I have accomplished to do so in several measures but I can not figure out how to create a column with the  

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @chela199 ,

Please try this:

Column = 
VAR _category = 'Table'[Category]
VAR _A =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[ID] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Category] = _category )
    )
VAR _B =
    SUMX (
        VALUES ( 'Table'[Category] ),
        SWITCH ( _category, "pants", _A * 5, "sweaters", _A * 2, "t-shirts", _A )
    )
VAR _C =
    DISTINCTCOUNT ( 'Table'[ID] )
VAR _D =
    SUMX (
        VALUES ( 'Table'[Category] ),
        SWITCH ( _category, "pants", _C * 5, "sweaters", _C * 2, "t-shirts", _C )
    )
VAR _E =
    DIVIDE ( _B, _D ) * 100
RETURN
    _E

vcgaomsft_0-1667972504421.png

You can change the variable name after RETURN to check if the calculation at each step meets your needs. If this does not work, consider providing sample data and expected output.

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @chela199 ,

Please try this:

Column = 
VAR _category = 'Table'[Category]
VAR _A =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[ID] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Category] = _category )
    )
VAR _B =
    SUMX (
        VALUES ( 'Table'[Category] ),
        SWITCH ( _category, "pants", _A * 5, "sweaters", _A * 2, "t-shirts", _A )
    )
VAR _C =
    DISTINCTCOUNT ( 'Table'[ID] )
VAR _D =
    SUMX (
        VALUES ( 'Table'[Category] ),
        SWITCH ( _category, "pants", _C * 5, "sweaters", _C * 2, "t-shirts", _C )
    )
VAR _E =
    DIVIDE ( _B, _D ) * 100
RETURN
    _E

vcgaomsft_0-1667972504421.png

You can change the variable name after RETURN to check if the calculation at each step meets your needs. If this does not work, consider providing sample data and expected output.

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

Shaurya
Memorable Member
Memorable Member

Hi @chela199,

 

You can use EARLIER function to get the count based on different category. With that you would get a column that shows count of all IDs in pants category for all rows with pants and so on for all category. You can then multiply these values with your fixed value using IF conditions. Once you have B and D, division should be easy.

 

Mark this post as a solution if that works for you!
Check out this blog of mine: How to Export Telemetry Data from Azure IoT Central into Power BI

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.

Top Solution Authors
Top Kudoed Authors