Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
Hi Everyone,
Looking for some help of which im sure will be a simple solution that i just can quite grasp, i have a table example below but essentially i want to add a Cumulative Count or Running Count column based on the group of 3 Columns, SuborderID, ActivityCategoryID and Postcode ID and the count from the date ascending
As you can see the first 3 rows highlighted all have the same SuborderID, ActivityCategoryID and PostcodeID and the RunningCount column is ascending from 1 to 3 based on the date, the same for next 2 rows
Could someone provide some insight on how to do this in Power Query? for context my table has ~8 Million Rows
Thank you in advanced
SuborderID | ActivityCategoryID | PostcodeID | DateID | RunningCount |
253657 | 132435 | 36748 | 20200101 | 1 |
253657 | 132435 | 36748 | 20200201 | 2 |
253657 | 132435 | 36748 | 20200301 | 3 |
321987 | 789321 | 75412 | 20230918 | 1 |
321987 | 789321 | 75412 | 20230921 | 2 |
463783 | 823712 | 67543 | 20240221 | 1 |
253657 | 789321 | 67543 | 20200101 | 1 |
Solved! Go to Solution.
You should be able to adapt the code I provided to preserve them.
One method
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc9BDoAwCATAv/TcA+zSQt9i/P83RGuM8WJPbGBCYNsKGnvzUosSxpaB3S2yQiCiomWvvwxrjDcjdMTJPEbmMzRTTEYZGksMc5t1ejC7Afo17uk4mQnwue3Z9mL3p/sB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SuborderID = _t, ActivityCategoryID = _t, PostcodeID = _t, DateID = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"SuborderID", Int64.Type}, {"ActivityCategoryID", Int64.Type}, {"PostcodeID", Int64.Type}, {"DateID", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"SuborderID", "ActivityCategoryID", "PostcodeID"}, {
{"Running Count", each List.Numbers(1, Table.RowCount(_)), type {Int64.Type}}}),
#"Expanded Running Count" = Table.ExpandListColumn(#"Grouped Rows", "Running Count")
in
#"Expanded Running Count"
Using your data above, excluding the last column:
Hey ronrsnfld
Thanks for the response, i just added that code to my advanced editor and im getting a 'Token Identifier exepcted' error which i cant work out what is missing, could you help please? my data is from an SQL Server so im just removed the server name from the code, the issue is on line 5 and the second let is highlighted just before the '_t'
let
Source = Sql.Databases("ServerName"),
DatabaseName = Source{[Name="DatabaseName"]}[Data],
Fact_Activity = DatabaseName{[Schema="Fact",Item="Activity"]}[Data],
let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SuborderID = _t, ActivityCategoryID = _t, PostcodeID = _t, DateID = _t]),
#"Sorted Rows" = Table.Sort(Fact_Activity,{{"StartDateID", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"ActivityCategoryID", Int64.Type}, {"SuborderID", Int64.Type}, {"PostcodeID", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"SuborderID", "ActivityCategoryID", "PostcodeID"}, {
{"Running Count", each List.Numbers(1, Table.RowCount(_)), type {Int64.Type}}}),
#"Expanded Running Count" = Table.ExpandListColumn(#"Grouped Rows", "Running Count")
in
#"Expanded Running Count"
Did your code work before adding the extra code? It does look odd, but I don't have an SQL database to check it against. Maybe I can set something up later today. Perhaps if you show your working code before you added mine (with confidential info obfuscated), I might be better able to ascertain the problem.
Thank you,
this is the code, very simple, just getting the data from the database and sorting the rows mainly, i could also remove the changed type i only added that in as it was in your code to made it easier for me to copy and paste in
let
Source = Sql.Databases("ServerName"),
DatabaseName = Source{[Name="DatabaseName"]}[Data],
Fact_Activity = DatabaseName{[Schema="Fact",Item="Activity"]}[Data],
#"Sorted Rows" = Table.Sort(Fact_Activity,{{"StartDateID", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"ActivityCategoryID", Int64.Type}, {"SuborderID", Int64.Type}, {"PostcodeID", Int64.Type}})
in
#"Changed Type"
Looks like you pasted my code incorrectly and copied part of the line that really belonged to my "Source=" line.
Try:
let
Source = Sql.Databases("ServerName"),
DatabaseName = Source{[Name="DatabaseName"]}[Data],
Fact_Activity = DatabaseName{[Schema="Fact",Item="Activity"]}[Data],
#"Sorted Rows" = Table.Sort(Fact_Activity,{{"StartDateID", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"ActivityCategoryID", Int64.Type}, {"SuborderID", Int64.Type}, {"PostcodeID", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"SuborderID", "ActivityCategoryID", "PostcodeID"}, {
{"Running Count", each List.Numbers(1, Table.RowCount(_)), type {Int64.Type}}}),
#"Expanded Running Count" = Table.ExpandListColumn(#"Grouped Rows", "Running Count")
in
#"Expanded Running Count"
Thank you, at first look that seems to be doing the trick, however i think the issue is i havent mentioned i have more than just these 3 coloumns in the data, so now ive lost all the other columns
You should be able to adapt the code I provided to preserve them.
One method
Do you need this in DAX or Power Query?
In DAX, try the ROWNUMBER function: ROWNUMBER function (DAX) - DAX | Microsoft Learn
Pay attention to the PARTITION BY parameter as that will determine when the numbers 'start over'.
Or check this post:
Solved: How to add Row_number over partition by Customer -... - Microsoft Fabric Community
Proud to be a Super User! | |
Hey ToddChitt,
Thanks for reply, i do need this in Power Query, ill take a look at the post you shared and let you know how it goes
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
33 | |
16 | |
12 | |
11 | |
9 |
User | Count |
---|---|
44 | |
24 | |
16 | |
14 | |
14 |