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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
DaGemsta
Helper I
Helper I

Break out an email matrix into single row with multiple columns

Hi guys

 

This is a tough one to explain!

 

I'm migrating data between two systems and using M query to help me!

 

The first table is the output from the old system and the second table is the input format for the new system.

 

I need to turn this

 

IdContact nameCompanyEmailQuotesOrder ConfirmationInvoice EmailOrder DispatchedstatementsCredit Notes
2050User1

ACME

user1@domain.comTrueFalseFalseTrueFalseFalse
2050User2ACMEuser2@domain.comTrueFalseTrueTrueTrueTrue
2050User3ACMEuser3@domain.comFalseFalseFalseTrueFalseTrue
2050User4ACMEuser4@domain.comTrueTrueTrueTrueTrueFalse
2050User5ACMEuser5@domain.comFalseTrueTrueTrueFalse

True

 

Into this

IdQuotes Email 1Quotes Email 2Quotes Email 3Quotes Email 4Quotes Email 5Order Conf Email 1Order Conf Email 2Order Conf Email 3Order Conf Email 4Order Conf Email 5Invoice Email 1Invoice Email 2Invoice Email 3Invoice Email 4Invoice Email 5Order Disp Email 1Order Disp Email 2Order Disp Email 3Order Disp Email 4Order Disp Email 5Statements Email 1Statements Email 2Statements Email 3Statements Email 4Statements Email 5Credit Note Email 1Credit Note Email 2Credit Note Email 3Credit Note Email 4Credit Note Email 5
2050user1@domain.comuser2@domain.comuser4@domain.com  user4@domain.comuser5@domain.com   user2@domain.comuser4@domain.comuser5@domain.com  user1@domain.comuser2@domain.comuser3@domain.comuser4@domain.comuser5@domain.comuser2@domain.comuser4@domain.com   user2@domain.comuser3@domain.comuser5@domain.com  

 

I've tried using conditional columns but can't get it to work out where to put the email addresses when there's more than one true in the column and keep it to one row. 

 

Then repeat this for around 20,000 customers!

 

Anyone got any ideas?

 

Many thanks in advance!

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @DaGemsta ,

Here are the steps you can follow:

1. Go to Power Query Check the yellow label Unpivot Columns.

vyangliumsft_0-1671156810127.png

Result:

vyangliumsft_1-1671156810129.png

2. Transform – Group  -- [id]– Operation:All Rows.

vyangliumsft_2-1671156810131.png

3. Add Column – Custom Column – Enter the following code.

Table.AddIndexColumn([Count],"Rank",1,1)

vyangliumsft_3-1671156810134.png

4. Selected [Count] – Remove.

vyangliumsft_4-1671156810135.png

 

vyangliumsft_5-1671156810135.png

5. Expand [Custom] - Do not select column [id] - OK.

vyangliumsft_6-1671156810136.png

6. Select the four columns -- right mouse button in the Merge Column interface to set the information at the yellow label.

vyangliumsft_7-1671156810139.png

7. Select [Merged] and [Custom.Value] -- Transform Pivot Column.

Value Column  --  [Custom.Email].

Advanced options Dont Aggregate.

vyangliumsft_8-1671156810142.png

Result:

vyangliumsft_9-1671156810143.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

2 REPLIES 2
DaGemsta
Helper I
Helper I

Thank you SO much @Anonymous 

 

Your solution got me most of the way there so I could tweak it and get the result I wanted with the following M query

 

let
Source = Excel.Workbook(File.Contents("D:\Documents\Go green customers (2).xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Go green", "Column12"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Delivery notes", "Order Dispatched"}, {"Orders", "Order Confirmation"}, {"Invoices", "Invoice Email"}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Quotes", type logical}, {"Order Confirmation", type logical}, {"Invoice Email", type logical}, {"Order Dispatched", type logical}, {"statements", type logical}, {"Credit Notes", type logical}, {"Email", type text}, {"Contact name", type text}, {"Company", type text}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Id", "Contact name", "Company", "Email"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Id"}, {{"Count", each _, type table [Id=text, Contact name=text, Company=text, Email=text, Attribute=text, Value=logical]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Rank",1,1)),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Count"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"Contact name", "Company", "Email", "Attribute", "Value", "Rank"}, {"Custom.Contact name", "Custom.Company", "Custom.Email", "Custom.Attribute", "Custom.Value", "Custom.Rank"}),
#"Removed Columns2" = Table.RemoveColumns(#"Expanded Custom",{"Custom.Contact name", "Custom.Company"}),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Removed Columns2", {{"Custom.Rank", type text}}, "en-GB"),{"Custom.Attribute", "Custom.Rank"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),
#"Filtered Rows" = Table.SelectRows(#"Merged Columns", each ([Custom.Value] = true)),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Merged]), "Merged", "Custom.Email"),
#"Removed Columns3" = Table.RemoveColumns(#"Pivoted Column",{"Custom.Value"}),
#"Renamed Columns1" = Table.RenameColumns(#"Removed Columns3",{{"Invoice Email-3", "Invoice Email 1"}, {"Invoice Email-9", "Invoice Email 2"}, {"Invoice Email-15", "Invoice Email 3"}, {"Invoice Email-21", "Invoice Email 4"}, {"Invoice Email-27", "Invoice Email 5"}, {"Invoice Email-39", "Invoice Email 6"}, {"Invoice Email-33", "Invoice Email 7"}, {"Invoice Email-45", "Invoice Email 8"}, {"Invoice Email-51", "Invoice Email 9"}, {"statements-5", "Statements Email 1"}, {"statements-11", "Statements Email 2"}, {"statements-17", "Statements Email 3"}, {"statements-23", "Statements Email 4"}, {"statements-29", "Statements Email 5"}, {"statements-41", "Statements Email 6"}, {"statements-35", "Statements Email 7"}, {"statements-53", "Statements Email 8"}, {"Credit Notes-6", "Credit Notes Email 1"}, {"Credit Notes-12", "Credit Notes Email 2"}, {"Credit Notes-18", "Credit Notes Email 3"}, {"Credit Notes-24", "Credit Notes Email 4"}, {"Credit Notes-30", "Credit Notes Email 5"}, {"Credit Notes-42", "Credit Notes Email 6"}, {"Credit Notes-48", "Credit Notes Email 7"}, {"Credit Notes-54", "Credit Notes Email 8"}, {"Credit Notes-36", "Credit Notes Email 9"}, {"Quotes-1", "Quotes Emails 1"}, {"Quotes-7", "Quotes Emails 2"}, {"Quotes-19", "Quotes Emails 3"}, {"Quotes-13", "Quotes Emails 4"}, {"Quotes-25", "Quotes Emails 5"}, {"Quotes-37", "Quotes Emails 6"}, {"Quotes-55", "Quotes Emails 7"}, {"Quotes-31", "Quotes Emails 8"}, {"Quotes-43", "Quotes Emails 9"}, {"Order Confirmation-2", "Order Confirmation Email 1"}, {"Order Confirmation-8", "Order Confirmation Email 2"}, {"Order Confirmation-20", "Order Confirmation Email 3"}, {"Order Confirmation-14", "Order Confirmation Email 4"}, {"Order Confirmation-26", "Order Confirmation Email 5"}, {"Order Confirmation-32", "Order Confirmation Email 6"}, {"Order Confirmation-38", "Order Confirmation Email 7"}, {"Order Confirmation-56", "Order Confirmation Email 8"}, {"Order Confirmation-44", "Order Confirmation Email 9"}, {"Order Dispatched-4", "Order Dispatched Email 1"}, {"Order Dispatched-10", "Order Dispatched Email 2"}, {"Order Dispatched-28", "Order Dispatched Email 3"}, {"Order Dispatched-16", "Order Dispatched Email 4"}, {"Order Dispatched-22", "Order Dispatched Email 5"}, {"Order Dispatched-40", "Order Dispatched Email 6"}, {"Order Dispatched-34", "Order Dispatched Email 7"}})
in
#"Renamed Columns1"

 

 

LEGEND!!! 😁

Anonymous
Not applicable

Hi  @DaGemsta ,

Here are the steps you can follow:

1. Go to Power Query Check the yellow label Unpivot Columns.

vyangliumsft_0-1671156810127.png

Result:

vyangliumsft_1-1671156810129.png

2. Transform – Group  -- [id]– Operation:All Rows.

vyangliumsft_2-1671156810131.png

3. Add Column – Custom Column – Enter the following code.

Table.AddIndexColumn([Count],"Rank",1,1)

vyangliumsft_3-1671156810134.png

4. Selected [Count] – Remove.

vyangliumsft_4-1671156810135.png

 

vyangliumsft_5-1671156810135.png

5. Expand [Custom] - Do not select column [id] - OK.

vyangliumsft_6-1671156810136.png

6. Select the four columns -- right mouse button in the Merge Column interface to set the information at the yellow label.

vyangliumsft_7-1671156810139.png

7. Select [Merged] and [Custom.Value] -- Transform Pivot Column.

Value Column  --  [Custom.Email].

Advanced options Dont Aggregate.

vyangliumsft_8-1671156810142.png

Result:

vyangliumsft_9-1671156810143.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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