cancel
Showing results for
Did you mean:
Regular Visitor

## 28 days rolling count of repeats in power bi Calculated column

&nbsp;
I am trying to figure out how to calculate a rolling count in power bi calculated column.
&nbsp;
For example count the number of each "Name" occurence in the last 28 days.
&nbsp;
Also, another calculated column that count the number of occurrence of same "Name" and "Number" over the last 30 days.
1 ACCEPTED SOLUTION
Anonymous
Not applicable

OK, I've done it.

Here's the M script:

```let

Source = <A reference to your table here>,

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Number", Int64.Type}, {"Name", type text}}),
#"Buffered Table" = Table.Buffer(#"Changed Type"),
#"Add Day 28 Days Back" = Table.AddColumn(#"Changed Type", "Date28DaysBack", each [Date] - #duration(27, 0, 0, 0)),
#"Add Day 28 Days Back", "Count Of Name (28 Days Back)",
(r) =>
let
A = Table.SelectRows(#"Buffered Table", each [Name] = r[Name] and r[Date28DaysBack] <= [Date] and [Date] <= r[Date]),
CountOfRows = Table.RowCount(A)
in
CountOfRows
),
#"Removed Auxiliary Column (28)" = Table.RemoveColumns(#"Add Count of Name (28 Days Back)",{"Date28DaysBack"}),
#"Add Day 30 Days Back" = Table.AddColumn(#"Removed Auxiliary Column (28)", "Date30DaysBack", each [Date] - #duration(29, 0, 0, 0)),
#"Add Day 30 Days Back", "Count Of Combination (30 Days Back)",
(r) =>
let
A = Table.SelectRows(#"Buffered Table", each [Number] = r[Number] and [Name] = r[Name] and r[Date30DaysBack] <= [Date] and [Date] <= r[Date]),
CountOfRows = Table.RowCount(A)
in
CountOfRows
),
#"Removed Auxiliary Column (30)" = Table.RemoveColumns(#"Add Count of Combination (30 Days Back)",{"Date30DaysBack"})
in
#"Removed Auxiliary Column (30)"```

And here's what my Source table looks like:

Best

Darek

5 REPLIES 5
Anonymous
Not applicable
This is not a job for DAX. It's a job for Power Query. DAX should only be used for calculated columns if there is NO OTHER OPTION.

Can you please use Power Query to do the above? And if not, why not?

Best
Darek
Regular Visitor
Thanks,
Any suggestion on how to do this in Power Query?
Anonymous
Not applicable

OK, I've done it.

Here's the M script:

```let

Source = <A reference to your table here>,

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Number", Int64.Type}, {"Name", type text}}),
#"Buffered Table" = Table.Buffer(#"Changed Type"),
#"Add Day 28 Days Back" = Table.AddColumn(#"Changed Type", "Date28DaysBack", each [Date] - #duration(27, 0, 0, 0)),
#"Add Day 28 Days Back", "Count Of Name (28 Days Back)",
(r) =>
let
A = Table.SelectRows(#"Buffered Table", each [Name] = r[Name] and r[Date28DaysBack] <= [Date] and [Date] <= r[Date]),
CountOfRows = Table.RowCount(A)
in
CountOfRows
),
#"Removed Auxiliary Column (28)" = Table.RemoveColumns(#"Add Count of Name (28 Days Back)",{"Date28DaysBack"}),
#"Add Day 30 Days Back" = Table.AddColumn(#"Removed Auxiliary Column (28)", "Date30DaysBack", each [Date] - #duration(29, 0, 0, 0)),
#"Add Day 30 Days Back", "Count Of Combination (30 Days Back)",
(r) =>
let
A = Table.SelectRows(#"Buffered Table", each [Number] = r[Number] and [Name] = r[Name] and r[Date30DaysBack] <= [Date] and [Date] <= r[Date]),
CountOfRows = Table.RowCount(A)
in
CountOfRows
),
#"Removed Auxiliary Column (30)" = Table.RemoveColumns(#"Add Count of Combination (30 Days Back)",{"Date30DaysBack"})
in
#"Removed Auxiliary Column (30)"```

And here's what my Source table looks like:

Best

Darek

Regular Visitor
Many thanks for your help Derek.
It worked on small data set but when I used the M code on a larger data set it kept crashing powerbi after taking forever to load the data. What do you advise?
I also found out that the code created a table on each row. Is it possible to create just the columns.
Thanks
Anonymous
Not applicable
Of course. But you have to give me some sample data. Then I'll give you the M code to do what you want.

Best
Darek

Announcements