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
G_Whit-UK
Helper II
Helper II

Multiple Condition If Statements

Hi,

 

I'm attempting to write a DAX IF statement with multiple conditions using data from two tables.

 

My primary data set has a list of transaction IDs together with a month end date.  I then have two other related data tables from vendors where they list a subset of the transaction IDs, the same month end date reference and a column denoting the vendor's ID.  I want to create two kinds of measures:

  1. A measure which identifies those transaction which match between the primary data set and that of a vendor file (this will need two measures - one per vendor);
  2. A measure which identifies any transactions which are not listed on either of the vendor files.

My source data can be represented as follows:

Primary Data:

Transaction IDME Date
131-Aug-21
231-Aug-21
331-Aug-21
431-Aug-21
531-Aug-21
631-Aug-21
731-Aug-21
831-Aug-21
931-Aug-21
1031-Aug-21

 

Vendor1 Data & Vendor2 Data (seperate tables)

Vendor 1   Vendor 2  
Transaction IDME DateVendor ID Transaction IDME DateVendor ID
131-Aug-21Vendor 1 231-Aug-21Vendor 2
331-Aug-21Vendor 1 431-Aug-21Vendor 2
531-Aug-21Vendor 1 631-Aug-21Vendor 2
731-Aug-21Vendor 1 831-Aug-21Vendor 2
931-Aug-21Vendor 1    

 

In the "Relationships" view, I have linked the IDs from the Primary table to the two vendor tables (many to many, both directions) as the active link, then the ME Data fields (also many to many, both directions).

 

My attemp at the DAX formula to pull out the matching transactions:

Vendor1 Rec =
IF ( and(
     'Primary Data'[Transaction ID] = 'Vendor1'[Transaction ID],
      RELATED ( 'primary Data'[ME Date] ) = 'Vendor1'[ME Date]),
'Vendor1'[Vendord ID],
BLANK ()
)
 
What is the correct syntax to use for the above, and what would it look like to achieve my 2nd reqirement where I need to find the transactions which do not appear in either of the vendor's files (i.e. transaction ID 10 isn't in either file)?

Thanks.
1 ACCEPTED SOLUTION
G_Whit-UK
Helper II
Helper II

Here is the solution to the above problem statement.  This is a multi step solution:

Step 1:  Power Query
Edit the query underpinning the Primary Data table by creating a new column and merging the "ME Date" and "Transaction ID" fields. 

 

=Text.Combine({Text.From([Date], "en-GB"), "-", Text.From([Transaction ID], "en-GB")})

 


As this should be a unique value, this can be further refined by removing any erroneous duplicates.

Repeat the above process for the "Vendor1" and "Vendor2" tables.

Step 2: Desktop
Now that we have unique references in all source tables, we can create a one to one link between the Primary Data table and the two vendor data tables.

With new tables relationships established, we can now easily add new columns to the Primary Data table using DAX (one column per vendor data table) using the formula: 

 

Vendor1 Tag = RELATED('Vendor1 Data'[Vendor ID])

 


We can use similar logic to create a third column to identify any unmatched items using the following DAX formula:

 

Not Found =
IF (
'Primary Data'[Vendor1 Tag] = BLANK ()
&& 'Primary Data'[Vendor2 Tag] = BLANK (),
"Not found",
BLANK ()
)

 

 

View solution in original post

4 REPLIES 4
G_Whit-UK
Helper II
Helper II

Here is the solution to the above problem statement.  This is a multi step solution:

Step 1:  Power Query
Edit the query underpinning the Primary Data table by creating a new column and merging the "ME Date" and "Transaction ID" fields. 

 

=Text.Combine({Text.From([Date], "en-GB"), "-", Text.From([Transaction ID], "en-GB")})

 


As this should be a unique value, this can be further refined by removing any erroneous duplicates.

Repeat the above process for the "Vendor1" and "Vendor2" tables.

Step 2: Desktop
Now that we have unique references in all source tables, we can create a one to one link between the Primary Data table and the two vendor data tables.

With new tables relationships established, we can now easily add new columns to the Primary Data table using DAX (one column per vendor data table) using the formula: 

 

Vendor1 Tag = RELATED('Vendor1 Data'[Vendor ID])

 


We can use similar logic to create a third column to identify any unmatched items using the following DAX formula:

 

Not Found =
IF (
'Primary Data'[Vendor1 Tag] = BLANK ()
&& 'Primary Data'[Vendor2 Tag] = BLANK (),
"Not found",
BLANK ()
)

 

 

Greg_Deckler
Super User
Super User

@G_Whit-UK Well, if it is a measure, you need to use aggregations around column references. 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler , then perhaps a measure is not the correct solution.  Perhaps I need to add 2 columns to the Primary Data table pulling in the Vendor ID into the respective columns, then a 3rd column to denote any transactions which are blank.

 

What would be the correct DAX code to acheive the above?

 

Thanks?

@G_Whit-UK Well, your other problem is that RELATED won't work across a many-to-many relationship. There is a lot of unknowns here. I recommend that you provide more sample data or a sample PBIX that can fully replicate your situation. That's the only way to test and really be sure of a solution.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.