Forum Discussion
LiamFallows
2 years agoNew Member
Find Products Running in Consecutive Weeks
I want to write a query on a work order report to return all work orders for products that run in consecutive weeks. A small sample of the table is below and the desired result of my query would ret...
- 2 years ago
Hi LiamFallows, try this.
I'm not sure about last two rows with OK (bacause the order number is the same). My logic is: If orders are in consecutive weeks (not in the same week but in consecutive) then result is Product Running in Consecutive Weeks else OK.
Result:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jcw7CoAwEADRq8jWgewnMbu3sA+ptBdE768iQogodlM8Jmcg8yiekQM4EEShXo+KaqpyxLDM0zauHUFxGTg9tL1rio025VMbBqo1X29qtX1oCq1OdOtYabne+luXHQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t, #"Order Number" = _t, Product = _t, #"Product Description" = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"Start Date", type date}}, "sk-SK"), AD_YearWeek = Table.AddColumn(ChangedType, "Week of Year", each Date.Year([Start Date]) * 100 + Date.WeekOfYear([Start Date]), Int64.Type), GroupedRows = Table.Group(AD_YearWeek, {"Product"}, { { "All", each Table.AddColumn( Table.AddIndexColumn(Table.Sort(_, {{"Start Date", Order.Ascending}}), "Index", 0, 1, Int16.Type), "Check", (x)=> if (try x[Week of Year] +1 = [Week of Year]{x[Index]+1} otherwise false) or (try x[Week of Year] -1 = [Week of Year]{x[Index]-1} otherwise false) then "Product Running in Consecutive Weeks" else "OK", type text), type table } }), CombiedAll = Table.Combine(GroupedRows[All]), RemovedColumns = Table.RemoveColumns(CombiedAll,{"Week of Year", "Index"}) in RemovedColumns
dufoq3
2 years agoCommunity Champion
Hi LiamFallows, try this.
I'm not sure about last two rows with OK (bacause the order number is the same). My logic is: If orders are in consecutive weeks (not in the same week but in consecutive) then result is Product Running in Consecutive Weeks else OK.
Result:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jcw7CoAwEADRq8jWgewnMbu3sA+ptBdE768iQogodlM8Jmcg8yiekQM4EEShXo+KaqpyxLDM0zauHUFxGTg9tL1rio025VMbBqo1X29qtX1oCq1OdOtYabne+luXHQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t, #"Order Number" = _t, Product = _t, #"Product Description" = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Start Date", type date}}, "sk-SK"),
AD_YearWeek = Table.AddColumn(ChangedType, "Week of Year", each Date.Year([Start Date]) * 100 + Date.WeekOfYear([Start Date]), Int64.Type),
GroupedRows = Table.Group(AD_YearWeek, {"Product"}, {
{ "All", each Table.AddColumn(
Table.AddIndexColumn(Table.Sort(_, {{"Start Date", Order.Ascending}}), "Index", 0, 1, Int16.Type),
"Check", (x)=> if (try x[Week of Year] +1 = [Week of Year]{x[Index]+1} otherwise false) or (try x[Week of Year] -1 = [Week of Year]{x[Index]-1} otherwise false) then "Product Running in Consecutive Weeks" else "OK", type text), type table }
}),
CombiedAll = Table.Combine(GroupedRows[All]),
RemovedColumns = Table.RemoveColumns(CombiedAll,{"Week of Year", "Index"})
in
RemovedColumns