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
Vienna83
Frequent Visitor

Normalize related pairs of Origins and Destinations

Hi to all,

 

I'm struggling with a - to my mind - simple task, which was in a way solved in this Community Message: http://community.powerbi.com/t5/Desktop/Transform-Data-with-Power-Query/td-p/62740 

 

I have a table with following columns:

Origin | Destination | Passengers

Vienna | Paris | 1200

Vienna | Berlin | 900

Vienna | Moscow | 400

Vienna | London | 4500

Berlin | Vienna | 1100

Paris | Vienna | 1800

Moscow | Vienna | 1600

 

I'm trying to create a table in PowerBI which can be used for a FlowMap, so I need to kind of normalize the pairs and sum the Passenger values:

 

Origin | Destination | Passengers

Vienna | Paris | 3000

Vienna | Berlin | 2000

Vienna | Moscow | 2000

Vienna | London | 4500

 

How can I realize that with a PowerQuery?

 

Cheers and thank you!

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

My suggestion would be to add a column with Origin and Destination sorted alphabetically in nested records, remove the original columns, expand the record column, group by the 2 new columns and sum the Passengers.

 

let
    Source = OriginsAndDestinations,
    #"Added Custom" = Table.AddColumn(Source, "Sorted", each if [Origin] > [Destination] then [Origin = [Origin], Destination = [Destination]] else [Origin = [Destination], Destination = [Origin]], type [Origin = text, Destination = text]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Origin", "Destination"}),
    #"Expanded Sorted" = Table.ExpandRecordColumn(#"Removed Columns", "Sorted", {"Origin", "Destination"}, {"Origin", "Destination"}),
    #"Grouped Rows" = Table.Group(#"Expanded Sorted", {"Origin", "Destination"}, {{"Passengers", each List.Sum([Passengers]), type number}})
in
    #"Grouped Rows"

 

Specializing in Power Query Formula Language (M)

View solution in original post

2 REPLIES 2
MarcelBeug
Community Champion
Community Champion

My suggestion would be to add a column with Origin and Destination sorted alphabetically in nested records, remove the original columns, expand the record column, group by the 2 new columns and sum the Passengers.

 

let
    Source = OriginsAndDestinations,
    #"Added Custom" = Table.AddColumn(Source, "Sorted", each if [Origin] > [Destination] then [Origin = [Origin], Destination = [Destination]] else [Origin = [Destination], Destination = [Origin]], type [Origin = text, Destination = text]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Origin", "Destination"}),
    #"Expanded Sorted" = Table.ExpandRecordColumn(#"Removed Columns", "Sorted", {"Origin", "Destination"}, {"Origin", "Destination"}),
    #"Grouped Rows" = Table.Group(#"Expanded Sorted", {"Origin", "Destination"}, {{"Passengers", each List.Sum([Passengers]), type number}})
in
    #"Grouped Rows"

 

Specializing in Power Query Formula Language (M)

@MarcelBeug That worked out perfectly! I tried it (now at home) with a piece of example data:

 

raw_data.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

My sample was neutralized to that table:

 

neutralized_data.PNG

 

 

 

 

 

 

 

 

 

 

 

Thank you!

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.