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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Drea
New Member

Custom Column that Checks for Matches in Another Row

I'm trying to add a check for reliability between ratings given by coordinators and supervisors, when all the ratings are in the same column, and another column lists the rater's role, and a series of other columns specify that they are of the same person in the same time and context.

The easiest way to do this would be to pivot the ratings row into columns by role and then create a simple conditional column, but I want the reliability test integrated into a matrix visualization that does a lot else and depends on the ratings staying in one column.

 

I'm hoping to achieve this in Power Query, but maybe it needs a Measure in DAX.

 

The logic is roughly this:

if rater_role = "Supervisor"

  then if (row exists where ID=ID and FY=FY and Department=Department and Month=Month and Level=Level and Rating_Criterion=Rating_Criterion and Rater_Role = "Coordinator"

  then if (Rating=Rating 1 else 0))

  else [blank]

 

So adding values only to Supervisor rows, if a row exists where all values are identical except that the rater is Coordinator, test if the Rating matches.  If it does, 1, if it doesn't, 0.  For all non-supervisor rows, or supervisor rows where no row with a matching Coordinator rating, blank.

 

I've been searching the forums and haven't found a case that addresses quite what I'm trying to do here.  Thank you all for any guidance you can provide!

 

IDFYDepartmentMonthLevelRATER_ROLERATING_CRITERIONRATINGDESIRED_ROW
12122ECJanuary2Coordinator13 
12122ECJanuary2Supervisor131
12122ECJanuary2Coordinator23 
12122ECJanuary2Supervisor220
22122ECJanuary1Coordinator3  
22122ECJanuary1Supervisor33 
22122ECJanuary1Coordinator43 
22122ECJanuary1Supervisor431
32122SAFebruary2Coordinator53 
32122SAFebruary2Supervisor510
32122SAFebruary2Supervisor63 
1 ACCEPTED SOLUTION
Drea
New Member

Thank you, Yolo!  Although this is producing an odd result!

 

My source is actually an Oracle database through an SQL query.  Adding the code for the #"Added Custom" does indeed produce preview data where I'm seeing 1 or 0 beside Supervisor rows and blanks next to Coordinator.  But when I apply and close, the refresh on the query begins pulling many, many more rows than actually exist in the data.  So something about this #"Added Custom" is producing many extra rows rather than only adding a column with 1,0, or null next to existing data.  It's so many rows that the query inevitably crashes before completing--which is generally *not* an issue with our database, even when handling extremely large queries.  But there shouldn't be any additional rows created at all, so whatever you might say could be an issue with our database, we have an issue with the query.

 

Here's what I'm adding.  The query already has commands for changing types and correcting errors, so what appears above differs slightly from what you posted, but it achieves the same goal and retrieves the correct number of rows before the following is added.

 

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=null )) else null,
b=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] )) else null,
c=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=[RATING] )) else null
in if [RATER_ROLE]<>"Supervisor" or ([RATER_ROLE]="Supervisor" and (b=1 or a>0)) then null else if [RATER_ROLE]="Supervisor" and c>0 then 1 else 0)
in
    #"Added Custom"

View solution in original post

2 REPLIES 2
Drea
New Member

Thank you, Yolo!  Although this is producing an odd result!

 

My source is actually an Oracle database through an SQL query.  Adding the code for the #"Added Custom" does indeed produce preview data where I'm seeing 1 or 0 beside Supervisor rows and blanks next to Coordinator.  But when I apply and close, the refresh on the query begins pulling many, many more rows than actually exist in the data.  So something about this #"Added Custom" is producing many extra rows rather than only adding a column with 1,0, or null next to existing data.  It's so many rows that the query inevitably crashes before completing--which is generally *not* an issue with our database, even when handling extremely large queries.  But there shouldn't be any additional rows created at all, so whatever you might say could be an issue with our database, we have an issue with the query.

 

Here's what I'm adding.  The query already has commands for changing types and correcting errors, so what appears above differs slightly from what you posted, but it achieves the same goal and retrieves the correct number of rows before the following is added.

 

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=null )) else null,
b=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] )) else null,
c=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=[RATING] )) else null
in if [RATER_ROLE]<>"Supervisor" or ([RATER_ROLE]="Supervisor" and (b=1 or a>0)) then null else if [RATER_ROLE]="Supervisor" and c>0 then 1 else 0)
in
    #"Added Custom"
v-xinruzhu-msft
Community Support
Community Support

Hi @Drea 

You can put the follwing code to advanced editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyNDICUq7OQMIrMa80sagSJArEzvn5RSmZeYkl+UVAHkitsVKsDiFdwaUFqUVlmcUkaUK1yogcq4zAGKTJCKcmQwyrjIFYgQhdKFYZw91HilUmROpCsQqhyRihKdgRSLilJhXhDEFTYrWhWGYKdgGpuswgdsUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, FY = _t, Department = _t, Month = _t, Level = _t, RATER_ROLE = _t, RATING_CRITERION = _t, RATING = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"FY", Int64.Type}, {"Department", type text}, {"Month", type text}, {"Level", Int64.Type}, {"RATER_ROLE", type text}, {"RATING_CRITERION", Int64.Type}, {"RATING", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let a=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=null )) else null,
b=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] )) else null,
c=if [RATER_ROLE]="Supervisor" then Table.RowCount(Table.SelectRows(#"Changed Type",(x)=>x[ID]=[ID] and x[FY]=[FY] and x[Department]=[Department] and x[Month]=[Month] and x[Level]=[Level] and x[RATING_CRITERION]=[RATING_CRITERION] and x[RATER_ROLE]="Coordinator" and  x[RATING]=[RATING] )) else null
in if [RATER_ROLE]<>"Supervisor" or ([RATER_ROLE]="Supervisor" and (b=1 or a>0)) then null else if [RATER_ROLE]="Supervisor" and c>0 then 1 else 0)
in
    #"Added Custom"

Output

vxinruzhumsft_0-1707878001408.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.