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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
PBI12345
Frequent Visitor

DAX Help: How to map intentional many-to-many relationship & double count some values on purpose?

Hi,

 

I am working with data and am struggling to return the output I intend, due to the presence of many-to-many relationships between my data.

 

Here is an example of a mapping table I have, noting that there are multiple values (India and Singapore) against an OD Country value of AustraliaSingapore:

RouteOrigin CountryDestination CountryOD Country
JapanAustraliaJapanAustraliaJapan
SingaporeAustraliaSingaporeAustraliaSingapore
IndiaSingaporeIndiaSingaporeIndia
IndiaAustraliaSingaporeAustraliaSingapore


Here is an example of my data, with "Route" hypothetically matched in on OD Country::

Record #Origin CountryDestination CountryOD CountrySeatsRoute
1AustraliaJapanAustraliaJapan50Japan
2IndiaSingaporeIndiaSingapore70India
3AustraliaSingaporeAustraliaSingapore30Singapore;India


However,  "Route" cannot be mapped in, because it maps to both Singapore and India. Annoyingly, I do infact want it to map against both of these conditions. Here is the output I want:

RouteSeats
Japan50
India100
Singapore30


I am aware that this double counts Record # 3 as both "India" and "Singapore", but this is actually the result I am after.
Is there a DAX calculation I can make to force this to work this way?


Bonus question: What I really want to achieve with this data is to take the minimum value between two defined city pairs. So in this example, I want the minimum value between either AustraliaSingapore or IndiaSingapore; this is becuase the data deals with connecting flights, where the restraining factor is the flight with the minimum number of seats. I would then need to build this logic out across a large whitelist of city pairs, with anything not on the list just equalling its stated value.

In this case, the expected output would be:

RouteSeats
Japan50
India30
Singapore30


I understand this is a huge longshot but any help would be TREMENDOUSLY appreciated -- this logic has been driving me absolutely crazy!

 

Thank you in advance

3 REPLIES 3
Kedar_Pande
Resident Rockstar
Resident Rockstar

@PBI12345 

Split the "Route" values in the "OD Country" column into individual country values.

Measure:

Route Seats = 
SUMX(
FILTER(
'YourDataTable',
CONTAINSSTRING('YourDataTable'[OD Country], 'YourMappingTable'[OD Country])
),
'YourDataTable'[Seats]
)

First, create a mapping table for the city pairs:

Origin Country     Destination Country        Minimum Seats
Australia               Singapore                         (min seats)
India                     Singapore                         (min seats)

 

Then, use a DAX formula to find the minimum value:

Min Seats = 
MINX(
FILTER(
'YourDataTable',
'YourDataTable'[Route] IN VALUES('YourMappingTable'[OD Country])
),
'YourDataTable'[Seats]
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Poojara_D12
Solution Sage
Solution Sage

Hi @PBI12345 

Can you please try the below steps:

Step 1: Double Counting Records

To achieve the result where a record is counted for both mapped "Route" values, you can use a calculated table to "flatten" the mappings in your data. This ensures each OD Country splits into multiple rows for each valid route.

 

Solution: Create a Calculated Table for Mapping

In Power BI, create a calculated table using DAX to expand the Route mappings for your OD Country.

 

ExpandedRoutes = 
ADDCOLUMNS(
    GENERATE(
        'MappingTable',
        VAR RoutesList = SUBSTITUTE('MappingTable'[Route], ";", ",")
        RETURN SELECTCOLUMNS(
            SPLIT(RoutesList, ","),
            "Route", TRIM([Value])
        )
    ),
    "OD Country", 'MappingTable'[OD Country]
)

 

This table will:

Split Route on ; into separate rows.

Add a new row for each route in the original mapping.

Example output for ExpandedRoutes:

Route OD Country

JapanAustraliaJapan
SingaporeAustraliaSingapore
IndiaAustraliaSingapore

 

Step 2: Map Records to Routes

Once the mappings are expanded, we can create a new calculated table or measure to aggregate the Seats by Route.

 

Measure for Aggregation

Create a measure to calculate the total seats by each route:

 

Total Seats = 
CALCULATE(
    SUM('Data'[Seats]),
    TREATAS('ExpandedRoutes'[OD Country], 'Data'[OD Country]),
    TREATAS('ExpandedRoutes'[Route], 'MappingTable'[Route])
)

 

The TREATAS function ensures that OD Country and Route relationships are used for context filtering. The measure will give you the double-counted seat values:

Route Seats

Japan50
India100
Singapore30

 

Step 3: Minimum Seats Between City Pairs

For your bonus requirement, you want to calculate the minimum value between city pairs for connecting flights. This requires additional logic to evaluate and filter data.

Whitelist of City Pairs
Create a table for the whitelist of city pairs in Power BI:

City Pair

AustraliaSingapore
IndiaSingapore

 

Measure for Minimum Seats
Use the following measure to calculate the minimum value between city pairs:

 

Min Seats = 
VAR WhitelistPairs = VALUES('Whitelist'[City Pair])
RETURN
SUMX(
    DISTINCT('ExpandedRoutes'[Route]),
    MINX(
        FILTER(
            'Data',
            'Data'[OD Country] IN WhitelistPairs
                && 'Data'[Route] = 'ExpandedRoutes'[Route]
        ),
        'Data'[Seats]
    )
)

 

This measure:

Iterates over each route.

Filters the data for city pairs in the whitelist.

Takes the minimum Seats value for each pair.

 

Final Output

Route Seats

Japan50
India30
Singapore30

 

I think this should handle both your requirements effectively. Let me know if you need clarification or further adjustments!

 

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS 

Hi Poojara,

 

Thank you for the detailed this response. I have read the steps and this sounds like it will work.

 

However, I cannot get past your step 1 -- specifically, 'SPLIT' is not showing up as a valid DAX function and the whole piece of code errors. Am I doing something wrong? 

 

PBI12345_0-1734705288948.png

PBI12345_1-1734705410438.png

 

Thanks

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.