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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Dynamic Calculated Columns - M Query (advanced)

Guys,

 

I have a reconciliation script that I often use; it's essentially a FullOuterJoin between two tables (orange and green below).  Once the data is loaded to Power BI, I then manually add several columns (in grey below) to see if the values match.

 

Example:

2018-06-27 08_21_16-Book1 - Excel.png

 

I would like to automate the creation of the columns highlighted in the red box.  Obviously the solution would need to be dynamic, i.e., should TableA or TableB change in column count, then so would the columns in the red box.

 

 

Thanks,

Simon

1 ACCEPTED SOLUTION

Oh, thanks Simon... I would have thought that you know me by now:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUXIsS80rTQUysvJRuG6lOTlKsTpAdYnZIH5wSVFqagmQAeWDUGwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [TableA.Name = _t, TableA.Adress = _t, TableB.Name = _t, TableB.Adress = _t, Recon.JoinType = _t]),
    FetchColNames = Table.ColumnNames(Source),
    ConvertToTable = Table.FromList(FetchColNames, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Duplicate = Table.DuplicateColumn(ConvertToTable, "Column1", "Column1 - Copy"),
    SplitCol = Table.SplitColumn(Duplicate, "Column1 - Copy", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Column1 - Copy.1", "Column1 - Copy.2"}),
    Group = Table.Group(SplitCol, {"Column1 - Copy.2"}, {{"Count", each Table.RowCount(_), type number}, {"All", each _[Column1], type table}}),
    Rename = Table.RenameColumns(Group,{{"Column1 - Copy.2", "PairName"}}),
    TableOfPairs = Table.Buffer(Table.SelectRows(Rename, each ([Count] = 2))),
    Compare = Table.AddColumn(Source, "Comparison", each List.Transform(TableOfPairs[All], (x) => Record.Field(_, x{0}) = Record.Field(_, x{1}))),
    CreateTable = Table.AddColumn(Compare, "Custom", each #table(TableOfPairs[PairName], {[Comparison]})),
    DynamicExpansion = Table.ExpandTableColumn(CreateTable, "Custom", TableOfPairs[PairName]),
    Cleanup = Table.RemoveColumns(DynamicExpansion,{"Comparison"})
in
    Cleanup

No pivoting, so it should be fast.

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

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

If it's advanced M code then your best bet is probably @ImkeF!



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...
Anonymous
Not applicable

Oooo, we can tag people? Time to bring in the pros! @mattmasson 

Yes we can, I call it "Invoking @ImkeF" 🙂



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...

Oh, thanks Simon... I would have thought that you know me by now:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUXIsS80rTQUysvJRuG6lOTlKsTpAdYnZIH5wSVFqagmQAeWDUGwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [TableA.Name = _t, TableA.Adress = _t, TableB.Name = _t, TableB.Adress = _t, Recon.JoinType = _t]),
    FetchColNames = Table.ColumnNames(Source),
    ConvertToTable = Table.FromList(FetchColNames, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Duplicate = Table.DuplicateColumn(ConvertToTable, "Column1", "Column1 - Copy"),
    SplitCol = Table.SplitColumn(Duplicate, "Column1 - Copy", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Column1 - Copy.1", "Column1 - Copy.2"}),
    Group = Table.Group(SplitCol, {"Column1 - Copy.2"}, {{"Count", each Table.RowCount(_), type number}, {"All", each _[Column1], type table}}),
    Rename = Table.RenameColumns(Group,{{"Column1 - Copy.2", "PairName"}}),
    TableOfPairs = Table.Buffer(Table.SelectRows(Rename, each ([Count] = 2))),
    Compare = Table.AddColumn(Source, "Comparison", each List.Transform(TableOfPairs[All], (x) => Record.Field(_, x{0}) = Record.Field(_, x{1}))),
    CreateTable = Table.AddColumn(Compare, "Custom", each #table(TableOfPairs[PairName], {[Comparison]})),
    DynamicExpansion = Table.ExpandTableColumn(CreateTable, "Custom", TableOfPairs[PairName]),
    Cleanup = Table.RemoveColumns(DynamicExpansion,{"Comparison"})
in
    Cleanup

No pivoting, so it should be fast.

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

Anonymous
Not applicable

Thanks Imke!

 

Super impressive - for each row, creating a list of a list of pairs that are used as variables against the main table.  I've never used Record.Field before so that was nice to see!

 

Simon

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.

Top Solution Authors
Top Kudoed Authors