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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
don_writer
Helper II
Helper II

Compare different values in different rows

Hi,

I am trying to compare values from one row to values in other rows to determine if all the criteria is present. Here is a sample of my table:

 

RelOTObjectIDRORelatedID
003S024O125
008S024P024
003S111O087
008S111P017
003S138O081
008S138P138
003S149O502
008S149P458
003S154O049
008S154P337
012S154O049
003S164O249
008S164P164
003S176O086
008S176P700
003S178O128
008S178P178
003S191O727
008S191P191
012S191O727
003S231O060
008S231P231
003S237O541

 

I'm looking to make certain there is always and ObjectID with and RO of "O" and one with "P". And it's generally okay if there are multiple "O"s but should only be one "P" per ObjectID.

 

How might I go about that in DAX and/or Power Query?

 

Thanks,

~Don

2 ACCEPTED SOLUTIONS
HotChilli
Super User
Super User

If the sample provided is Table1, you could create a 2nd table

Table2 = SELECTCOLUMNS(FILTER(Table1, Table1[RO] = "P"), "ObjectID", Table1[ObjectID], "RO", Table1[RO])

You could do a DISTINCTCOUNT on ObjectID in Table2 to look for ObjectID with more than 1 P

 

Then add a calculated column to Table1

Is Matching P = 
VAR _PtoFind =
    LOOKUPVALUE (
        'Table2'[ObjectID], 
        'Table2'[ObjectID], Table1[ObjectID]
    )
RETURN
 IF (Table1[RO] = "P", "Skip",
 IF (_PtoFind <> BLANK (), "Y", "N" ) )

This should return N if there is an O record without a matching P.

Note: The calculated column will error if there are more than 1 P records in Table2 for a specific ObjectID

View solution in original post

I'll be damned, I thought the fourth parameter was a alternate result if the output was blank. Thanks for this.

 

Here is the final code for anyone looking for this kind of thing.

Matching P = 
    VAR varPtoFind = 
        LOOKUPVALUE( srcJRChief[ObjectID], srcJRChief[ObjectID], srcJRChief[ObjectID], srcJRChief[RO],"P")
    RETURN
        IF(srcJRChief[RO]="P","Skip",
        IF(varPtoFind<>BLANK(),"Y","N"))

View solution in original post

8 REPLIES 8
HotChilli
Super User
Super User

If the sample provided is Table1, you could create a 2nd table

Table2 = SELECTCOLUMNS(FILTER(Table1, Table1[RO] = "P"), "ObjectID", Table1[ObjectID], "RO", Table1[RO])

You could do a DISTINCTCOUNT on ObjectID in Table2 to look for ObjectID with more than 1 P

 

Then add a calculated column to Table1

Is Matching P = 
VAR _PtoFind =
    LOOKUPVALUE (
        'Table2'[ObjectID], 
        'Table2'[ObjectID], Table1[ObjectID]
    )
RETURN
 IF (Table1[RO] = "P", "Skip",
 IF (_PtoFind <> BLANK (), "Y", "N" ) )

This should return N if there is an O record without a matching P.

Note: The calculated column will error if there are more than 1 P records in Table2 for a specific ObjectID

This is awesome and the direction I was contemplating. Just curious if there is a way to accomplish this without an extra table?

There is.  I started with the separate table to make testing easier.

 

I won't do it for you but it's not too different from the code I provided.  Hint : you can use LOOKUPVALUE without a separate table

I appreciate the direction.

So I certainly get what the two table solution is doing. I know we can read off of virtual tables but I think that's the part I am missing, as in uncertain how I'd appraoch this. Is that what I should be trying to do here? Generate the table within the LOOKUPVALUE function?

You're probably overthinking this.

 

Let's look at this code

VAR _PtoFind =
    LOOKUPVALUE (
        'Table2'[ObjectID], 
        'Table2'[ObjectID], Table1[ObjectID]
    )

It says "Find me an ObjectID from Table2  (which is a table with only RO = 'P') and I'm going to pass the ObjectID of the current row " or, in other words, "Does a 'P' exist for the current row?"

 

You want to change this variable to check Table1 in a similar manner (because now  Table2 does not exist).  Therefore (next hint) you need to replace references to Table2 with references to Table1.  Also , you have to tell LOOKUPVALUE that Table1[RO] is P.

Matching P = 
    VAR varPtoFind = 
        LOOKUPVALUE(
            srcJRChief[ObjectID],
            srcJRChief[ObjectID],
            CALCULATE(VALUES(srcJRChief[ObjectID]),FILTER(srcJRChief,srcJRChief[RO]="P")))
    RETURN
        IF(srcJRChief[RO]="P","Skip",
        IF(varPtoFind<>BLANK(),"Y","N"))

I was thinking something like this but I get an error. I am assuming because the LOOKUPVALUE search_value parameter says it can't take an expression in the same table. Is there a way around this other than building another table?

 

Thanks again for your help.

Way too complicated.  LOOKUPVALUE https://dax.guide/lookupvalue/

has syntax of the form: 

      thing i want,

      search_column, search_column_value

      2nd_search_column, 2nd_search_column_value

LOOKUPVALUE(
            srcJRChief[ObjectID],
            srcJRChief[ObjectID], srcJRChief[ObjectID],
            srcJRChief[RO],"P")

The syntax does look weird but it means - get me an ObjectID for the current row's ObjectID and the RO is P.

 

This syntax will fail if there is more than one P row per ObjectID

I'll be damned, I thought the fourth parameter was a alternate result if the output was blank. Thanks for this.

 

Here is the final code for anyone looking for this kind of thing.

Matching P = 
    VAR varPtoFind = 
        LOOKUPVALUE( srcJRChief[ObjectID], srcJRChief[ObjectID], srcJRChief[ObjectID], srcJRChief[RO],"P")
    RETURN
        IF(srcJRChief[RO]="P","Skip",
        IF(varPtoFind<>BLANK(),"Y","N"))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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