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
When importing a bank statement into PQ, all is well except for the fact that the Description column on some entries is spread across two or more (variable number) rows. I would like to combine them into one row.
I have a pattern to work with, however, in that the Date column only displays the date in the first row of each statement entry. The rest are null. For example:
Date
2024-12-02
null
2024-12-02
null
null
2024-12-02
null
null
null
etc...
So I can see what needs to be done, just no idea on the implementation. Can this be done exclusively with the user interface, or is some M code manipulation required?
Thanks.
Solved! Go to Solution.
Probably most efficient with M-Code.
You can Group by the dates, then concatenate the Description column.
Given your date column, you can use the fourth and fifth arguments of the GroupBy function:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1NNI1MFLSUUpMUorViVbKK83JAfKSU8A8FBWpacgq0jOQeZlZmOqzc5BV5OYh8/ILkHmFRUqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Description = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Description", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Date"}, {
{"Description", each Text.Combine([Description]," "), type text}
}, GroupKind.Local,(x,y)=>Number.From(x[Date] <> null and y[Date]<>null))
in
#"Grouped Rows"
Source Data example
Results:
You will need to consider how to treat the other columns, depending on how they are laid out.
Probably most efficient with M-Code.
You can Group by the dates, then concatenate the Description column.
Given your date column, you can use the fourth and fifth arguments of the GroupBy function:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1NNI1MFLSUUpMUorViVbKK83JAfKSU8A8FBWpacgq0jOQeZlZmOqzc5BV5OYh8/ILkHmFRUqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Description = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Description", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Date"}, {
{"Description", each Text.Combine([Description]," "), type text}
}, GroupKind.Local,(x,y)=>Number.From(x[Date] <> null and y[Date]<>null))
in
#"Grouped Rows"
Source Data example
Results:
You will need to consider how to treat the other columns, depending on how they are laid out.
That is interesting. I actually tried that first but had the grouping column within a List "..{"Date"}...", where it doesn't work (requires y[Date]).
Doesn't seem to be much of a performance difference though.
Some times there is (with the y only version being slower) and sometimes not.
Hi @ronrsnfld , that's interesting, but I think the speed is the same. It's not that easy to measure the exact query calculation duration because you have to maximize CPU usage and turn off all other unnecessary processes
Update:
I've copied your M code and placed it in the appropriate spot and it is working perfectly to solve the problem. I don't understand WHY it's working yet, but I'll work on that. 🙂
Thank you for your help in providing the solution.
Glad to help. Take a look here for an explanation of the fifth argument of the GroupBy function.
This solution is working great, but I always like to understand why it's working.
I've been reviewing the 5th argument in the Table.Group function. It appears you have created basically a Lambda function as I would see in Excel. I believe x refers to the current row, and y refers to the following row.
How is the logic working there? The way I've been reading it, it should NOT work! I am clearly missing comething. Can you assist my understanding?
Thank you.
It is mentioned in the article I linked above. It is counterintuitive (because 0 = false), but if fifth argument returns a zero (0) then the rows are in the same group.
Thanks for the response. I am not at all proficient with M coding as yet to make any sense of this. I will need to look it over carefully and see how I can incorporate it into my own problem.
...some M code hand writing would be easiest in your case. Show a sample of your data - it's not clear what to do with any other columns (other than Description)
Step 1: Fill Down the Date Column
Since the Date column contains the correct date in the first row of each statement entry and null values in subsequent rows for the same entry, you can fill down the date to align the entries:
Now, your table will look like this:
|
Date |
Description |
|
2024-12-02 |
Transaction 1 |
|
2024-12-02 |
Description part 2 |
|
2024-12-02 |
Description part 3 |
|
2024-12-02 |
... |
|
2024-12-02 |
... |
Step 2: Group the Rows Based on the Date
After filling down the Date column, you can group the rows by the Date column and concatenate the descriptions for each group.
This step will group all the rows by the Date column and create a nested table for each Date group.
Step 3: Combine the Descriptions
Now, you'll combine the descriptions from each group (for each Date) into one cell:
Now, you have a new column with the descriptions for each Date grouped together.
Text.Combine([Combined Description][Description], " ")
This will combine all descriptions within each group into a single string, separated by a space (you can choose a different separator if desired).
Step 4: Final Cleanup
Thanks for the response. I think I was not specific enough in my first post, however.
Your solution would work IF date were a unique identifier. In other words, if there were only one statement entry for each date. But there are many entires for each date (the description for each entry spans a variable number of rows). If I fill down and then group by date, ALL the description fields matching that date will simply be mashed together, no?
I was thinking that the solution might have something to with creating an Index column, transforming that index column somehow to reflect the correct number of rows per entry, and then using your method to group by that index column and concatenate the description. But I don't know how to implement it.
Thanks again.
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 19 | |
| 9 | |
| 8 | |
| 7 | |
| 6 |