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
Covington
Helper I
Helper I

How to get Product Counts per 7 Days from this kind of table

DateProductID
12/17/2020        5
12/16/20209
12/15/20203
12/14/20205
12/13/20209
12/12/20201
12/11/20204
12/10/20201
12/9/20203
12/8/20202
12/7/20201
12/6/20203
12/5/20205
12/4/20209

 

Week123456789
1101120002
2212010001

 

Essentially I want to count the number of times a product ran based upon a rolling 7-day span.  I've been playing around and can't seem to figure out where to go with this.  I'm sure it isn't difficult. 

In Excel I might do a CountIf, but then I would have to explicity call out the rows to aggregate.

 

Thanks in advance!

1 ACCEPTED SOLUTION

I ended up creating another column in the Excel table that would read Week1, Week2, Week3, etc..   Then I pivoted off of that and it worked great.  Appreciate all of the help.

View solution in original post

9 REPLIES 9
Fowmy
Super User
Super User

@Covington 

You can add a column to your table get the 7 days Weekly bracket. I assume the dates are in sequence and the granularity of your table is as shown in the sample. I took Thursday to Friday as the week. You can change it as you need though. 

Fowmy_0-1608366105228.png

Then, add a matrix as below. 

Fowmy_1-1608366198047.png

If you want to see zero for missing values, you will have to have a dimension table for Product ID or create a new table to populate zero using generate or crossjoin. It is possible to do it in PQ as well.

________________________

If my answer was helpful, please click Accept it as the solution to help other members find it useful

Click on the Thumbs-Up icon if you like this reply 🙂


Website YouTube  LinkedIn

 

 




 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Jimmy801
Community Champion
Community Champion

Hello @Covington 

 

you can transform the date to week, add a index column and then pivoting your product ID

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RDcAgCATQVRq+TQRErbMY91/Dpu3RJvD54MLNSaJZelZWPt6hRJVWelbtXl00nCqoOBnoC5YYVJA4CcicOFyN8PCEKKSHVAupGlrar+Ta", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ProductID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ProductID", Int64.Type}}, "en-US"),
    #"Calculated Week of Year" = Table.TransformColumns(#"Changed Type",{{"Date", Date.WeekOfYear, Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Calculated Week of Year", "Index", 0, 1),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Index", {{"ProductID", type text}}, "de-DE"), List.Distinct(Table.TransformColumnTypes(#"Added Index", {{"ProductID", type text}}, "de-DE")[ProductID]), "ProductID", "Index", List.Count)
in
    #"Pivoted Column"

Jimmy801_0-1608360283659.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Very close, I'm playing around with it. This gives me weekly stuff based upon the calendar, but I'm looking for a "Rolling 7 days", so trying to figure out how to tweak this 🙂  Nice code!

Hello @Covington 

 

what you mean by rolling 7 days? from now on - 7 days - 14 days etc.?

Then try this code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RDcAgCATQVRq+TQRErbMY91/Dpu3RJvD54MLNSaJZelZWPt6hRJVWelbtXl00nCqoOBnoC5YYVJA4CcicOFyN8PCEKKSHVAupGlrar+Ta", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, ProductID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ProductID", Int64.Type}}, "en-US"),
    #"Calculated Week of Year" = Table.TransformColumns(#"Changed Type",{{"Date", each Number.RoundUp(Duration.TotalDays(Date.From(DateTime.FixedLocalNow())- _)/7), Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Calculated Week of Year", "Index", 0, 1),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Index", {{"ProductID", type text}}, "de-DE"), List.Distinct(Table.TransformColumnTypes(#"Added Index", {{"ProductID", type text}}, "de-DE")[ProductID]), "ProductID", "Index", List.Count)
in
    #"Pivoted Column"

Jimmy801_0-1608405392941.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

I ended up creating another column in the Excel table that would read Week1, Week2, Week3, etc..   Then I pivoted off of that and it worked great.  Appreciate all of the help.

Hi @Covington ,

Glad to hear the issue is solved. You can accept the suitable reply as solution, that way, other community members could easily find the answer when they get same issues.


Best Regards,
Community Support Team _ Yingjie Li

Covington
Helper I
Helper I

Is there a way to:

  • Add a Column
  • Iterate each 7 and set the value to Week{x}
  • Pivot the table?

 

That would likely solve the problem if possible (I think)

If you don't have a calendar table, you should get one of those first. https://exceleratorbi.com.au/power-pivot-calendar-tables/

then you need to work out what you need. A rolling 7 day number is different to weekly numbers (which is what your example seems to be).  Assuming you want the totals by week like in your example, you simply need a week number in your calendar table. You can add that in power query. Select the date column, go to add column, date, day of week. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

@MattAllington I'm really fine converting the dates to a simple number to be honest.  I could number those 1 - 14 (based upon Date Desc) and it would be fine to me.  I'm looking at it similar to a Rolling Average, but this would be more of a Rolling Count, I guess.

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