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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
v-cgao-msft
Community Support
Community Support

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
v-cgao-msft
Community Support
Community Support

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.