Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi,
I download bank statements in CSV formats, and consolidate them in PQ. These are my steps
1. Load folder from SharePoints (using SharePoint.Contents)
2. Make all my transformations then load to Excel as table
3. Add a few columns in Excel, among them Category and Subcategory
4. I create a mapping table in Excel (attached) then load it PQ
5. I write some M code to reconcile some transactions automatically with Category and Subcategory.
My issue is, that I cannot map all transactions automatically, and I need some manual input and need to reconcile Category and Subcategory manually for the ones not able to do it automatically
What I need
I give sample transactions
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rVxNjxy5Df0rDZ93tCKpz6MNb4IsECCIg1ycPXTs9u4g42ljZtaJ8+tDzbRUKqsmFlm+jV1dbH08Pj6SUr99+wLcjzb8iBbdix9e/PF8/vXmxH/85fjl4+n24XB3ene6/nx6f/hwd/54aI8xeTSUXvzyw9sXGDsDPx9vT4c3H68ffuN/vDneHO++HD5dbD2cD6vHV+BtNPbJSD+Klx+P/z3fPjuK9pgI0OT8aAByb+DTp/8zi/qUQsgmwOPrtn/9r+d/nu4eDq/uzv++3ZzGVx+4ggz2MhGbOkN/O93fHJ8dR30KwG/D0zQwdK//9PH65svh9fHz9f3mMNbPryhbqKPo9+TlzfW70+Hn82+39+ft+Xz9iSsIsU5IhQ8CH4x/mpLFHh/8HYfX59M2OpaHPAJIdQT93vz9/P744XxbPvOn28/nMu7OQvf0CmJwxrunZUWVCQSbjMOnaYR9S4oRXQW7V3tMtHbLiGI8RITVlNWDDm3OFXRet08hWONGFhBsExX3g3Gnp9GGzoYttL28vnt1++p5JqmP0YM3aYOJZh3GxUKFNK7Cm+Ovz6zA5Qm7fTB0YTGNr0LIZBC/Bw0maBBPeyBFbissiDyFI1Tlj9Wkvr2egSeR49PXkyIqJceh8el9AA13UvQNzU4BRgIgk54MQNSA0Scy7gKpfgleHe/e3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8QsG8sp111IYuwAXO1AVN0mdq1JyuaeGbtQa2sIZw25BSbVrXaecNyudLSHmazYcsZpRUn72hrNJMoziUNCuPrgssalroKkXZJV2UmadTLiUV2Gp1BdliYRe3I8wLx0G6AWfU6PLeYMy6VL5VkuzNIQIHExkQ0xXHb9mUVvHXHTJlt/IaxackfOPGhDcqczNsT5+24HIjR8b2jaPJyPpHkhfpy3XKpepHQU3z0tWhSyqDi73fRJ0O5aWQSMDePHHCc+ux1Ag54NdxCP3GFDAqxv65LosDVSn/Yj0ICggTVVaH/dnmqkJZ7O74zpLsq4qAWg0sc0I8Kgm/ZcG9I0kJuit/tW59c1W4ROaRiZFqms9ot1SIThppMrJxnUr1g8FW9QFSCDxzGLseV8od1tWq6QslkT74eUELl4AMnY7RhQ1W0ibHrjzVTksMbLrVTcL3/Td/eywyHpY68azbR4RajTfwAQWm7Lr+dImXlci/UjXQobyp05a8+rEmbCtT0k93FrUjtxiuuwqz8SgVBaxtmscMhMkzRjwsjPO8AbTawazZYTjtXvEQR0pBCaj+R43eFmBjsVoSYblx6i/VyN3g56EPEdshx10QI42YEl0Uo2wopqx2ZTQAtq5HQUg7h6+AptDZq3OG4sPwGFKromF+AejZp5WjStDwsd91lAAfWCvWe/8rNdCdOOae9nGxeg3wyJWaJG90YXGYidewv/qi2IrRe88qE5nYFM1fJun75Hw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Counter Party" = _t, Reference = _t, #"Amount (GBP)" = _t])
in
Source
the result should be something like this
where I have a single Category and Subcategory column, and for example the highlighted row need manual input, I want to be able to add manually the Category and Subcategory and still be able to refresh queries without losing all the auto ones. Please a code that optimizes the performance
Thanks for your time and expertise.
@ronrsnfld @lbendlin @MarkLaf @SundarRaj @Nasif_Azam @speedramps
Hey @Einomi ,
I obviously try my code here I attached the screenshot, the excel file, and the pbix file for your easier understandings. It is a kindly request not to judge people, I am here to help you just and spend much time on this.🙂
Before you conclude that I need to be clear about your requirements. You're building a Power Query solution that merges both automatic and manual categorizations of financial transactions for streamlined reporting. You download bank statements in CSV format, transform them in Power Query, and add extra columns like Category and Subcategory manually in Excel.
If your requirements are those, follow the below steps:
M Code (Power Query)
let
// 1. Load the transaction table from SharePoint or file
Source = Excel.Workbook(File.Contents("C:\Users\NasifAzam\Desktop\New folder (2)\All Tables.xlsx"), null, true),
Transactions_Table = Source{[Item="Transactions",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Transactions_Table,{{"Date", type text}, {"Counter Party", type text}, {"Reference", type text}, {"Amount", type number}}),
// 2. Load the mapping table
MappingSource = Excel.Workbook(File.Contents("C:\Users\NasifAzam\Desktop\New folder (2)\All Tables.xlsx"), null, true),
MappingTable = Source{[Item="Mapping",Kind="Table"]}[Data],
// 3. Auto Map Category/Subcategory using Mapping Table
AutoMerged = Table.NestedJoin(
Transactions, "Counter Party",
MappingTable, "Description",
"Mapping", JoinKind.LeftOuter
),
ExpandedAuto = Table.ExpandTableColumn(
AutoMerged, "Mapping", {"Category", "Subcategory"},
{"AutoCategory", "AutoSubcategory"}
),
// 4. Load the manually edited table (same structure as output table with manual overrides)
ManualSource = Excel.Workbook(File.Contents("C:\Users\NasifAzam\Desktop\New folder (2)\All Tables.xlsx"), null, true),
ManualEdits = Source{[Item="ManualEdits",Kind="Table"]}[Data],
// 5. Merge auto-mapped + manual edits (manual takes priority)
FinalMerged = Table.NestedJoin(
ExpandedAuto, {"Reference"},
ManualEdits, {"Reference"},
"Manual", JoinKind.LeftOuter
),
ExpandedFinal = Table.ExpandTableColumn(
FinalMerged, "Manual", {"Category", "Subcategory"},
{"ManualCategory", "ManualSubcategory"}
),
// 6. Determine Final Category and Subcategory (manual wins)
AddedFinalColumns = Table.AddColumn(ExpandedFinal, "Final Category", each
if [ManualCategory] <> null then [ManualCategory] else [AutoCategory]),
FinalWithSub = Table.AddColumn(AddedFinalColumns, "Final Subcategory", each
if [ManualSubcategory] <> null then [ManualSubcategory] else [AutoSubcategory]),
// 7. Optional: Remove temporary columns
Cleaned = Table.RemoveColumns(FinalWithSub, {"AutoCategory", "AutoSubcategory", "ManualCategory", "ManualSubcategory"})
in
Cleaned
Step 1: Prepare Excel Tables
You’ll need 3 named tables in Excel:
Transactions contains Date, Counter Party, and Reference Amount column
Mapping contains Description, Category, and Subcategory column
ManualEdits contains Reference, Category, and Subcategory column
Step 2: Open Power Query Editor in Power BI
Select the 3 tables
Create a new Blank Query and paste the M Code above (Change the file sources as per your needs)
Click Done
The Final Result:
Step 3: Refresh & Maintain Edits
When you refresh, Power Query will:
Auto-map what it can using the Mapping table
Respect any manual changes from the ManualEdits table
This means manual changes will persist even after refreshes.
Step 4: Output for Reporting
Load this final query as a table into Excel
Use this table for PivotTables, charts, or Power BI
Best Regards,
Nasif Azam
Hi,
I am sorry if you got offended I did not say you used AI (which could be fine in some cases) I said it it very similar because the style of the posts and the style of the code along with Table.AddColumns were a bit confusing.
I am not sure why you have a third table called ManualEdits. I only work with two tables, the one I download from SharePoint (in PQ, I connect to SP, then combine all files) then load to Excel where I can add two columns and manually edit
Plus, another mapping table where it fetches all auto categories
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!