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
OsmanEmi
Frequent Visitor

Creating counters in more than one instance

Hi,

 

I have the below table. I would like the counter coulmn to count based on dates at the same restaurant.

I cant's seem to wrap my head around how this would be done using a measure or a calculated column (preferably calculated column). Any help would be appreciated.

 

Restaurant ID        Delivery Date                    Counter
127/11/20201
227/11/20201
327/11/20201
126/11/20202
125/11/20203
225/11/20202
325/11/20202
124/11/20204
2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@OsmanEmi , Create a new column like

countx(filter(Table, [Restaurant ID] = earlier([Restaurant ID]) && [Delivery Date] >=earlier([Delivery Date]) ),[Restaurant ID])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

Anonymous
Not applicable

Hi @OsmanEmi 

 

C.Counter = RANKX(FILTER('Table','Table'[Restaurant ID]=EARLIER('Table'[Restaurant ID])),'Table'[Delivery Date],,DESC)

M query:

You need sort ID column by asc and sort Date column by Desc, group by ID column (Operation = ALLROWS)

Add a custom column , reomve other columns and expand custom column.

Custom = Table.AddIndexColumn([AllROWS],"Rank",1,1)

Mquery in Advanced Editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIy1zc01DcyMDJQitWJVjLCFDLGFAJrNMMUMsU0yxTTLFNMjSYIoVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Restaurant ID     " = _t, #"   Delivery Date                    " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Restaurant ID     ", Int64.Type}, {"   Delivery Date                    ", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"   Delivery Date                    ", "Delivery Date"}, {"Restaurant ID     ", "Restaurant ID"}}),
    #"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"Restaurant ID", Order.Ascending}, {"Delivery Date", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Restaurant ID"}, {{"AllROWS", each _, type table [Restaurant ID=nullable number, Delivery Date=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([AllROWS],"Rank",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Restaurant ID", "AllROWS"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Restaurant ID", "Delivery Date", "Rank"}, {"Custom.Restaurant ID", "Custom.Delivery Date", "Custom.Rank"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Expanded Custom",{{"Custom.Rank", "Counter"}}),
    #"Sorted Rows1" = Table.Sort(#"Renamed Columns1",{{"Custom.Delivery Date", Order.Descending}}),
    #"Renamed Columns2" = Table.RenameColumns(#"Sorted Rows1",{{"Counter", "MQ.Counter"}, {"Custom.Restaurant ID", "Restaurant ID"}, {"Custom.Delivery Date", "Delivery Date"}})
in
    #"Renamed Columns2"

Result is as below.

2.png

You can download the pbix file from this link: Creating counters in more than one instance

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @OsmanEmi 

 

C.Counter = RANKX(FILTER('Table','Table'[Restaurant ID]=EARLIER('Table'[Restaurant ID])),'Table'[Delivery Date],,DESC)

M query:

You need sort ID column by asc and sort Date column by Desc, group by ID column (Operation = ALLROWS)

Add a custom column , reomve other columns and expand custom column.

Custom = Table.AddIndexColumn([AllROWS],"Rank",1,1)

Mquery in Advanced Editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIy1zc01DcyMDJQitWJVjLCFDLGFAJrNMMUMsU0yxTTLFNMjSYIoVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Restaurant ID     " = _t, #"   Delivery Date                    " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Restaurant ID     ", Int64.Type}, {"   Delivery Date                    ", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"   Delivery Date                    ", "Delivery Date"}, {"Restaurant ID     ", "Restaurant ID"}}),
    #"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"Restaurant ID", Order.Ascending}, {"Delivery Date", Order.Descending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Restaurant ID"}, {{"AllROWS", each _, type table [Restaurant ID=nullable number, Delivery Date=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([AllROWS],"Rank",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Restaurant ID", "AllROWS"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Restaurant ID", "Delivery Date", "Rank"}, {"Custom.Restaurant ID", "Custom.Delivery Date", "Custom.Rank"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Expanded Custom",{{"Custom.Rank", "Counter"}}),
    #"Sorted Rows1" = Table.Sort(#"Renamed Columns1",{{"Custom.Delivery Date", Order.Descending}}),
    #"Renamed Columns2" = Table.RenameColumns(#"Sorted Rows1",{{"Counter", "MQ.Counter"}, {"Custom.Restaurant ID", "Restaurant ID"}, {"Custom.Delivery Date", "Delivery Date"}})
in
    #"Renamed Columns2"

Result is as below.

2.png

You can download the pbix file from this link: Creating counters in more than one instance

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

amitchandak
Super User
Super User

@OsmanEmi , Create a new column like

countx(filter(Table, [Restaurant ID] = earlier([Restaurant ID]) && [Delivery Date] >=earlier([Delivery Date]) ),[Restaurant ID])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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