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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
pkoevesdi
Regular Visitor

Multiply two records

Hey,

 

I have to records with the same fieldnames, but different values, for instance:

Record1:

fieldname1: 10

fieldname2: 20

fieldname3: 30

 

Record2:

fieldname1: 40

fieldname2: 50

fieldname3: 60

 

What I want is a third Record with the fields multiplied by each other,

so:

Record3:

fieldname1: Record1.fieldname1 * Record2.fieldname1 (=400)

fieldname2: Record1.fieldname2 * Record2.fieldname2 (=1000)

fieldname3: Record1.fieldname3 * Record2.fieldname3 (=1800)

 

The fieldnames are not known literally before, so they shouldn't appear in the code. The code should work with any number of fieldnames that are common in both records and generate the third record as described.

And the fields in the records are not necessarily in the same order, furthermore, there can be fields in Record 1 or 2, which are not present in the other. These field can be skipped, they don't need to appear in Record 3.

 

I'd like to solve it in Power Query, not in DAX.

 

How can I achieve that? Is a record the right dataype to do it, or ist a list or a table better? I consider a list a bad idea, since the fields can be in a different order.

 

Thank You very much for any hint.

1 ACCEPTED SOLUTION

Quick and dirty solution (there's probably a more elegant solution, but I don't have time for it now):

 

let
    #"1stRecord" = [A=1, B=2, C=3],
    #"2ndRecord" = [C=30, A=10],
    CommonFields = List.Intersect({Record.FieldNames(#"1stRecord"),Record.FieldNames(#"2ndRecord")}),
    #"1stFields" = Record.SelectFields(#"1stRecord", CommonFields),
    #"2ndFields" = Record.SelectFields(#"2ndRecord", CommonFields),
    ListOfRecordFields = List.Zip({Record.FieldValues(#"1stFields"), Record.FieldValues(#"2ndFields")}),
    ListMultiplication = List.Transform(ListOfRecordFields, each _{0} * _{1}),
    ToTable = Table.FromRows({ListMultiplication}, CommonFields),
    ToRecord = Table.ToRecords(ToTable){0}
in
    ToRecord

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

It's a good thing that you don't want DAX because you can't generate rows in an existing table using DAX. So, hopefully @ImkeF has a solution for you in M code. 



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Quick and dirty solution (there's probably a more elegant solution, but I don't have time for it now):

 

let
    #"1stRecord" = [A=1, B=2, C=3],
    #"2ndRecord" = [C=30, A=10],
    CommonFields = List.Intersect({Record.FieldNames(#"1stRecord"),Record.FieldNames(#"2ndRecord")}),
    #"1stFields" = Record.SelectFields(#"1stRecord", CommonFields),
    #"2ndFields" = Record.SelectFields(#"2ndRecord", CommonFields),
    ListOfRecordFields = List.Zip({Record.FieldValues(#"1stFields"), Record.FieldValues(#"2ndFields")}),
    ListMultiplication = List.Transform(ListOfRecordFields, each _{0} * _{1}),
    ToTable = Table.FromRows({ListMultiplication}, CommonFields),
    ToRecord = Table.ToRecords(ToTable){0}
in
    ToRecord

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thank You so much, it works fine.

Most parts of the algorithm are similar to what I tried, but the point I couldn't figure out was the syntax:

each _{0} * _{1}

in

List.Transform(ListOfRecordFields, each _{0} * _{1})

So, Thanks a lot!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.