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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
owenv
Frequent Visitor

Value as percentage of another on multiple column lookup

I'm trying to calculate a percentage of vehicles involved in an incident based on the number of vehicles.

 

I have one table (Table A) with the values for total number of vehicles seen by type and month. My second table contains all the incidents with one row per incident (Table B). Table B also contains the type of vehicle involved and month of incident. Additionally, there is a type of incident column.

 

There are other columns in each table but I'm not interested in those for this purpose.

 

Here's an example:

Table A:

monthvehicle typenumber of vehicles
Aprilvehicle type 310
Aprilvehicle type 223
Augustvehicle type 348
Augustvehicle type 113
Decembervehicle type 133

 

Table B:

monthvehicle typeincident type 
Aprilvehicle type 1incident type 1
Aprilvehicle type 2incident type 1
Augustvehicle type 1incident type 2
Augustvehicle type 3 incident type 3
Decembervehicle type 1incident type 2
Decembervehicle type 1incident type 2

 

I'd like to make a third table based on a lookup of vehicly type and month, but also be able to carry across the incident type.

 

Desired result:

monthvehicle typeincident typenumber of vehiclesnumber of incidents
Aprilvehicle type 3null10null
Aprilvehicle type 2incident type 1231
Augustvehicle type 1incident type 2131
Augustvehicle type 3incident type 3481
Decembervehicle type 1incident type 2332

 

If I'm able to get the desired result table shown above, I can caluclate the required % values.

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@owenv  you can write three measures like this

 

_incidentType =
CALCULATE (
    MAX ( 'Table B'[incident type] ),
    TREATAS (
        SUMMARIZE ( 'Table A', 'Table A'[month], 'Table A'[vehicle type] ),
        'Table B'[month],
        'Table B'[vehicle type]
    )
)


_numberOfIncidents =
CALCULATE (
    COUNT ( 'Table B'[incident type] ),
    TREATAS (
        SUMMARIZE ( 'Table A', 'Table A'[month], 'Table A'[vehicle type] ),
        'Table B'[month],
        'Table B'[vehicle type]
    )
)

pct =
VAR _deno =
    CALCULATE (
        COUNT ( 'Table B'[incident type] ),
        TREATAS (
            SUMMARIZE ( ALL ( 'Table A' ), 'Table A'[month], 'Table A'[vehicle type] ),
            'Table B'[month],
            'Table B'[vehicle type]
        )
    )
RETURN
    DIVIDE ( [_numberOfIncidents], _deno )

 

 

 

smpa01_0-1639752413965.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

7 REPLIES 7
smpa01
Super User
Super User

@owenv  you can write three measures like this

 

_incidentType =
CALCULATE (
    MAX ( 'Table B'[incident type] ),
    TREATAS (
        SUMMARIZE ( 'Table A', 'Table A'[month], 'Table A'[vehicle type] ),
        'Table B'[month],
        'Table B'[vehicle type]
    )
)


_numberOfIncidents =
CALCULATE (
    COUNT ( 'Table B'[incident type] ),
    TREATAS (
        SUMMARIZE ( 'Table A', 'Table A'[month], 'Table A'[vehicle type] ),
        'Table B'[month],
        'Table B'[vehicle type]
    )
)

pct =
VAR _deno =
    CALCULATE (
        COUNT ( 'Table B'[incident type] ),
        TREATAS (
            SUMMARIZE ( ALL ( 'Table A' ), 'Table A'[month], 'Table A'[vehicle type] ),
            'Table B'[month],
            'Table B'[vehicle type]
        )
    )
RETURN
    DIVIDE ( [_numberOfIncidents], _deno )

 

 

 

smpa01_0-1639752413965.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
owenv
Frequent Visitor

Thank you for the reply. This doesn't seem to return any values for me (I've updated all the variables). Also, the % needs to be represented as the number of vehicles for the corresponding row rather than the grand total. Apologies I wasn't clear on that.

@owenv  what is the desired % based on the data that you provided?

 

you can simple change pct to this

 

smpa01_0-1639754506753.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
owenv
Frequent Visitor

number of incidents / number of vehicles*100

owenv_0-1639754573329.png

 

@owenv  try this

smpa01_0-1639754908138.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
owenv
Frequent Visitor

Sorted! Thanks so much.

The reason I wasn't seeing the values as I mentioned earlier is because I missed a step. I need to create the third table using the groupby function and select only the columns I was interested in. Once I added your measures to that third table things started to work. Thanks so much, this is a huge help!

@owenv  Glad to be of help. Can you please accept the answer !!!

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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.

Top Solution Authors