Forum Discussion
mudassirmir
4 years agoFrequent Visitor
Summarize weekly production in monthly buckets
Hi There, I have production table in below format and i am trying to workout how to put production quantities in monthly buckets. In below example production falls in two months. I want output to...
ImkeF
Community Champion
4 years agoHi mudassirmir ,
you can use the function I've published here to create a list of days that allow you to allocate accordingly:
Date.DatesBetween to retrieve dates between 2 dates in Power BI and Power Query – The BIccountant
After that, the count of the items in your list can be used as a divisor to create the daily amount.
Then expand the days, apply the daily amount calculation and aggregate to month, to your liking:
(Please also check the file enclosed):
let
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText("i45WMjbUNzTQNzIwMlLSUTLTNzSEsQ0tjAwMlGJjAQ==", BinaryEncoding.Base64),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [#"Production Start" = _t, #"Production End" = _t, #"Production Qty" = _t]
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
{{"Production Start", type date}, {"Production End", type date}, {"Production Qty", Int64.Type}},
"en-GB"
),
#"Invoked Custom Function" = Table.AddColumn(
#"Changed Type",
"Dates",
each fnDatesBetween([Production Start], [Production End], "Day")
),
#"Added Custom" = Table.AddColumn(#"Invoked Custom Function", "Divisor", each List.Count([Dates])),
#"Expanded Dates" = Table.ExpandListColumn(#"Added Custom", "Dates"),
#"Inserted Division" = Table.AddColumn(
#"Expanded Dates",
"DailyAmount",
each [Production Qty] / [Divisor],
type number
),
// I would stop here and load the data as it is into the data model to stay flexible. But if you are sure that you only want on monthly level, continue with the grouping.
#"Calculated Start of Month" = Table.TransformColumns(
#"Inserted Division",
{{"Dates", Date.StartOfMonth, type date}}
),
#"Grouped Rows" = Table.Group(
#"Calculated Start of Month",
{"Dates"},
{{"MonthlyAmount", each List.Sum([DailyAmount]), type number}}
)
in
#"Grouped Rows"