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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
ResearchKnell
Frequent Visitor

Direct Query - Two counts in the same visual with different filters

I'm am doing research to breakdown racial comparisons of victims to suspects/arrestees.  This is a direct connection to a SQL database.  For example, I can't figure out how to get a count of victim's that were white with a white suspect/arrestee.  Then, I would need a count of how many victims that were white had a suspect/arrestee who were black, asian, hispanic, etc.  Then do the same thing for the different victims that were black, asian, hispanic, etc.  The rows will have the same "INC_ID_KEY".  However, the way the columns are set up, if I filter for "Name_Type" of victim, I lose all of my "Subject_Types" in the count and vice versa.   See attached of how it is showing from the direct query of the SQL database.  For example (this is one incident below), I want to count how many asian victims had a white suspect, how do I do that with this below data?  

 

ResearchKnell_0-1624545672994.png

 

 

 

2 REPLIES 2
v-kelly-msft
Community Support
Community Support

Hi  @ResearchKnell ,

First fill in the blanks of "Subject_Type" use below M codes:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjMzMTMwUNJRCi5NykpNLgGywnXDPTxDXEFiocEBrs4hQJZSrA6S2rDM5JLMXCDDUdcx2NPRTz/A0dnTzdNZwd/Z1dEPpFxHydPPxTPM0yXU0QdVr3N+bkFOYmZeYl4JfgOUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [INC_ID_Key = _t, Name_Type = _t, Race = _t, Subject_Type = _t, Victim_Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"INC_ID_Key", Int64.Type}, {"Name_Type", type text}, {"Race", type text}, {"Subject_Type", type text}, {"Victim_Type", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Max(Table.SelectRows(Source,each [INC_ID_Key]=[INC_ID_Key]),"Subject_Type")),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Subject_Type"}, {"Custom.Subject_Type"})
in
    #"Expanded Custom"

And you will see:

vkellymsft_0-1624848047764.png

Then you could filter the rows and get the counts.

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

What I'm trying to do is count how many Inc_ID_Keys (that are the same number) have a white suspect and an asian female (for example from the data listed above as if there were mulitple records like the one shown above but each unique record is connected by that Inc_ID_Key).

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors