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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
cmaloyb
Helper II
Helper II

Count Instance of ID Occurences According to Date

I am trying to add a column in Power Query that will give the count of the amount of time an ID occurs and that number (count) based on date in chronological order. Here is an example of what I am looking for:

 

ID#DateCount
00011/1/20191
00012/6/20202

002

3/24/20181
0026/7/20203
0024/2/20192

 

The "Count" column is what I am looking to add. I would like to do this in Power Query if possible. 

 

3 ACCEPTED SOLUTIONS
Mariusz
Community Champion
Community Champion

Hi @cmaloyb 

 

You can paste this code into Blank Query, the step you want to look at is Grouped Rows

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VcrJDcAgEAPAXvxGstdBHLWg7b+NIJFH+I5mLUgKFFgxGQxk+ZlFs33m0wYful62W2O/22SlkfkC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID#"}, {{"Count", each Table.AddIndexColumn( Table.Sort( _, { "Date" } ), "Count", 1, 1 ) , type table [#"ID#"=nullable text, Date=nullable date, Count=nullable number]}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "Count"}, {"Date", "Count"})
in
    #"Expanded Count"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

View solution in original post

Hi @cmaloyb - see if this will help. It will return this ID Count column

edhans_0-1597763290557.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUTLUN9Q3MjC0VIrVgYsZ6ZsBxYwMoGJGQCFjfSMTkDoLJDEzfXN0ZUA1UNNiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"ID Count" = 
        Table.AddColumn(
            #"Changed Type",
            "ID Count",
            each
                let
                    varCurrentID = [#"ID#"],
                    varCurrentDate = [Date]
                in
            Table.RowCount(
                Table.SelectRows(#"Changed Type", each [#"ID#"] = varCurrentID and [Date] <= varCurrentDate)
            ),
            Int64.Type
        )
in
    #"ID Count"

 How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

Anonymous
Not applicable

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYzBCQAhDAR7yVvIZhXvrEXsvw1X0TvIIzsM07uFJUO4joimETbSwXRUYUKDG1NfdpYlv5+8qEw8V84/Lq7MSasxJg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t, Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID#", Int64.Type}, {"Date", type date}, {"Count", Int64.Type}},"en-US"),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID#"}, {{"idx", (t)=>  Table.AddColumn(t,"rank",each  List.PositionOf(List.Sort(t[Date]),[Date])+1)}}),
    #"Expanded idx" = Table.ExpandTableColumn(#"Grouped Rows", "idx", {"Date", "Count", "rank"}, {"Date", "Count", "rank"})
in
    #"Expanded idx"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYzBCQAhDAR7yVvIZhXvrEXsvw1X0TvIIzsM07uFJUO4joimETbSwXRUYUKDG1NfdpYlv5+8qEw8V84/Lq7MSasxJg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t, Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID#", Int64.Type}, {"Date", type date}, {"Count", Int64.Type}},"en-US"),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"ID#"}, {{"idx", each Table.AddIndexColumn(Table.Sort(_,{"Date"}),"cc",1,1)}}),
    #"Expanded idx" = Table.ExpandTableColumn(#"Grouped Rows", "idx", {"Date", "Count", "Index", "cc"}, {"Date", "Count", "Index", "cc"}),
    #"Sorted Rows" = Table.Sort(#"Expanded idx",{{"Index", Order.Ascending}})
in
    #"Sorted Rows"

 

 

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYzBCQAhDAR7yVvIZhXvrEXsvw1X0TvIIzsM07uFJUO4joimETbSwXRUYUKDG1NfdpYlv5+8qEw8V84/Lq7MSasxJg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t, Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID#", Int64.Type}, {"Date", type date}, {"Count", Int64.Type}},"en-US"),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID#"}, {{"idx", (t)=>  Table.AddColumn(t,"rank",each  List.PositionOf(List.Sort(t[Date]),[Date])+1)}}),
    #"Expanded idx" = Table.ExpandTableColumn(#"Grouped Rows", "idx", {"Date", "Count", "rank"}, {"Date", "Count", "rank"})
in
    #"Expanded idx"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RYzBCQAhDAR7yVvIZhXvrEXsvw1X0TvIIzsM07uFJUO4joimETbSwXRUYUKDG1NfdpYlv5+8qEw8V84/Lq7MSasxJg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t, Count = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID#", Int64.Type}, {"Date", type date}, {"Count", Int64.Type}},"en-US"),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"ID#"}, {{"idx", each Table.AddIndexColumn(Table.Sort(_,{"Date"}),"cc",1,1)}}),
    #"Expanded idx" = Table.ExpandTableColumn(#"Grouped Rows", "idx", {"Date", "Count", "Index", "cc"}, {"Date", "Count", "Index", "cc"}),
    #"Sorted Rows" = Table.Sort(#"Expanded idx",{{"Index", Order.Ascending}})
in
    #"Sorted Rows"

 

 

 

Mariusz
Community Champion
Community Champion

Hi @cmaloyb 

 

You can paste this code into Blank Query, the step you want to look at is Grouped Rows

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VcrJDcAgEAPAXvxGstdBHLWg7b+NIJFH+I5mLUgKFFgxGQxk+ZlFs33m0wYful62W2O/22SlkfkC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID#"}, {{"Count", each Table.AddIndexColumn( Table.Sort( _, { "Date" } ), "Count", 1, 1 ) , type table [#"ID#"=nullable text, Date=nullable date, Count=nullable number]}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "Count"}, {"Date", "Count"})
in
    #"Expanded Count"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi @cmaloyb - see if this will help. It will return this ID Count column

edhans_0-1597763290557.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUTLUN9Q3MjC0VIrVgYsZ6ZsBxYwMoGJGQCFjfSMTkDoLJDEzfXN0ZUA1UNNiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID#" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"ID Count" = 
        Table.AddColumn(
            #"Changed Type",
            "ID Count",
            each
                let
                    varCurrentID = [#"ID#"],
                    varCurrentDate = [Date]
                in
            Table.RowCount(
                Table.SelectRows(#"Changed Type", each [#"ID#"] = varCurrentID and [Date] <= varCurrentDate)
            ),
            Int64.Type
        )
in
    #"ID Count"

 How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Greg_Deckler
Super User
Super User

@ImkeF , @edhans 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors