Forum Discussion
Custom Column that Checks for Matches in Another Row
- 2 years ago
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"
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"