Power Query - Units per Month

therealjdj

New Member
Joined
Jul 8, 2013
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello teams,
So thanks to some of you, I have been using Power Query. It's a fantastic tool. I have used some of your help and by reading and watching videos have been able to do what I was set out to do. I have just one issue left and I can not figure this one out. Hoping you guys can come to the rescue once again.

Let's say I am selling cookies. And after pulling a few sheets together I have information on my customer, how large his order is, when he wants the cookies, and how many cookies I can make for him max that month.
So if you order 5 cookies starting in August, but I can only make 3 a month for you, I would sell you 3 in August and 2 in September. The amount of cookies I make for you does not impact the other customers. So I could make a max of 3 cookies for as many customers as I want.

I need this data to be transformed into single lines showing sales per month so I can Pivot Table it.

Hoping you guys can help...



Book1
ABCD
1Customer NameCookie QtyStart DateMax Cookies per Month
2David79/1/20233
3Michael18/1/20235
4Marie1412/1/20233
5
6
7Customer NameMonthYearCookies
8DavidSeptember20233
9DavidOctober20233
10DavidNovember20231
11MichaelAugust20231
12MarieDecember20233
13MarieJanuary20243
14MarieFebruary20243
15MarieMarch20243
16MarieApril20242
Sheet1
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.TransformColumnTypes(Source,{{"Start Date", type date}}),
    tbl1 = Table.AddColumn(tbl, "List", each 
        let 
            lst = List.Generate(()=> [Cookie Qty], (x)=> x > 0, (x)=> x - [Max Cookies per Month], (x)=> List.Min({[Max Cookies per Month], x})),
            lst1 = List.Accumulate({0..List.Count(lst)-1}, {}, (s,c)=> s & {Date.AddMonths([Start Date], c)}),
            lst2 = List.Transform(lst1, (x)=> {Date.MonthName(x), Date.Year(x)})
        in  
            List.Zip({lst2,lst})),
    tbl2 = Table.ExpandListColumn(tbl1, "List"),
    tbl3 = Table.SplitColumn(tbl2, "List", each _, {"DateList", "Cookies"}),
    tbl4 = Table.SplitColumn(tbl3, "DateList", each _, {"Month", "Year"}),
    Result = Table.RemoveColumns(tbl4,{"Cookie Qty", "Start Date", "Max Cookies per Month"})
in
    Result

Book1
ABCDEFGHIJ
1Customer NameCookie QtyStart DateMax Cookies per MonthCustomer NameMonthYearCookies
2David79/1/20233DavidSeptember20233
3Michael18/1/20235DavidOctober20233
4Marie1412/1/20233DavidNovember20231
5MichaelAugust20231
6MarieDecember20233
7MarieJanuary20243
8MarieFebruary20243
9MarieMarch20243
10MarieApril20242
11
Sheet2
 
Upvote 0
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.TransformColumnTypes(Source,{{"Start Date", type date}}),
    tbl1 = Table.AddColumn(tbl, "List", each
        let
            lst = List.Generate(()=> [Cookie Qty], (x)=> x > 0, (x)=> x - [Max Cookies per Month], (x)=> List.Min({[Max Cookies per Month], x})),
            lst1 = List.Accumulate({0..List.Count(lst)-1}, {}, (s,c)=> s & {Date.AddMonths([Start Date], c)}),
            lst2 = List.Transform(lst1, (x)=> {Date.MonthName(x), Date.Year(x)})
        in 
            List.Zip({lst2,lst})),
    tbl2 = Table.ExpandListColumn(tbl1, "List"),
    tbl3 = Table.SplitColumn(tbl2, "List", each _, {"DateList", "Cookies"}),
    tbl4 = Table.SplitColumn(tbl3, "DateList", each _, {"Month", "Year"}),
    Result = Table.RemoveColumns(tbl4,{"Cookie Qty", "Start Date", "Max Cookies per Month"})
in
    Result

Book1
ABCDEFGHIJ
1Customer NameCookie QtyStart DateMax Cookies per MonthCustomer NameMonthYearCookies
2David79/1/20233DavidSeptember20233
3Michael18/1/20235DavidOctober20233
4Marie1412/1/20233DavidNovember20231
5MichaelAugust20231
6MarieDecember20233
7MarieJanuary20243
8MarieFebruary20243
9MarieMarch20243
10MarieApril20242
11
Sheet2


You guys are amazing!!!
Transferred it to my actual data set, made some modifications and we are good to go. THANK YOU!!!
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top