I have a table with three columns Date, Customer, Product, Index in power query. Index…
- User
I have a table with three columns Date, Customer, Product, Index in power query. Index is index for unique combination of Customer and Product. Using M language provide code to get the column Group in which consecutive dates for unique combination of Customer and Product are placed to one group. For example, for Customer X and Product A1 dates 06.01.2023 to 06.02.2023 are placed to Group 1 and dates 06.05.2023 to 06.06.2023 are placed to Group 2. See the detailed example.
Input is
|Date|Customer|Product|Index| |06.01.2023|X|A1|0| |06.02.2023|X|A1|1| |06.05.2023|X|A1|2| |06.06.2023|X|A1|3| |06.01.2023|X|A2|0| |06.02.2023|X|A2|1| |06.03.2023|X|A2|2| |06.08.2023|X|A2|3| |06.01.2023|Y|A1|0| |06.05.2023|Y|A1|1| |06.09.2023|Y|A1|2|
The desired output is
|Date|Customer|Product|Index|Group| |06.01.2023|X|A1|0|1| |06.02.2023|X|A1|1|1| |06.05.2023|X|A1|2|2| |06.06.2023|X|A1|3|2| |06.01.2023|X|A2|0|1| |06.02.2023|X|A2|1|1| |06.03.2023|X|A2|2|1| |06.08.2023|X|A2|3|2| |06.01.2023|Y|A1|0|1| |06.05.2023|Y|A1|1|2| |06.09.2023|Y|A1|2|3|
Modify the last part of code. I misplaced some parts of code to change logic
let Source = Excel.Workbook(File.Contents(“C:\Users\snoale\Downloads\Пример\Данные.xlsx”), null, true), #“Исходные данные_Sheet” = Source{[Item=“Исходные данные”,Kind=“Sheet”]}[Data], #“Promoted Headers” = Table.PromoteHeaders(#“Исходные данные_Sheet”, [PromoteAllScalars=true]), #“Changed Type” = Table.TransformColumnTypes(#“Promoted Headers”,{{“Date”, type date}, {“Client”, type text}, {“Product”, type text}, {“Share”, type text}, {“Q”, Int64.Type}, {“P”, type number}, {“StartDate”, type date}, {“EndDate”, type date}}), #“Removed Columns” = Table.RemoveColumns(#“Changed Type”,{“Date”, “Share”, “Q”, “P”}), #“Added Custom” = Table.AddColumn(#“Removed Columns”, “Custom”, each {Number.From([StartDate])…Number.From([EndDate])}), #“Expanded Custom” = Table.ExpandListColumn(#“Added Custom”, “Custom”), #“Renamed Columns” = Table.RenameColumns(#“Expanded Custom”,{{“Custom”, “Date”}}), #“Removed Columns1” = Table.RemoveColumns(#“Renamed Columns”,{“StartDate”, “EndDate”}), #“Changed Type1” = Table.TransformColumnTypes(#“Removed Columns1”,{{“Date”, type date}}), #“Removed Duplicates” = Table.Distinct(#“Changed Type1”), #“Grouped Rows” = Table.Group(#“Removed Duplicates”, {“Client”, “Product”}, {{“AllData”, each _, type table [Client=nullable text, Product=nullable text, Date=nullable date]}}), #“Added Custom1” = Table.AddColumn(#“Grouped Rows”, “Custom”, each Table.AddIndexColumn([AllData],“Index”,0)), #“Removed Columns2” = Table.RemoveColumns(#“Added Custom1”,{“Client”, “Product”, “AllData”}), #“Added Custom3” = Table.AddColumn(#“Removed Columns2”, “Custom.1”, each Table.AddColumn(#“Removed Columns2”, “Group”, each List.Accumulate( [Date], {}, (state, current) => let previous = if List.Count(state) > 0 then List.Last(state) else null in if previous <> null and Date.AddDays(previous, 1) = current then state & {current} else state & {current} ) )),
- Assistant
let Source = Excel.Workbook(File.Contents("C:\Users\snoale\Downloads\Пример\Данные.xlsx"), null, true), #“Исходные данные_Sheet” = Source{[Item="Исходные данные",Kind="Sheet"]}[Data], #“Promoted Headers” = Table.PromoteHeaders(#“Исходные данные_Sheet”, [PromoteAllScalars=true]), #“Changed Type” = Table.TransformColumnTypes(#“Promoted Headers”,{{“Date”, type date}, {“Client”, type text}, {“Product”, type text}, {“Share”, type text}, {“Q”, Int64.Type}, {“P”, type number}, {“StartDate”, type date}, {“EndDate”, type date}}), #“Removed Columns” = Table.RemoveColumns(#“Changed Type”,{"Date", "Share", "Q", "P"}), #“Added Custom” = Table.AddColumn(#“Removed Columns”, "Custom", each {Number.From([StartDate])..Number.From([EndDate])}), #“Expanded Custom” = Table.ExpandListColumn(#“Added Custom”, "Custom"), #“Renamed Columns” = Table.RenameColumns(#“Expanded Custom”,{{“Custom”, “Date”}}), #“Removed Columns1” = Table.RemoveColumns(#“Renamed Columns”,{"StartDate", “EndDate”}), #“Changed Type1” = Table.TransformColumnTypes(#“Removed Columns1”,{{“Date”, type date}}), #“Removed Duplicates” = Table.Distinct(#“Changed Type1”), #“Grouped Rows” = Table.Group(#“Removed Duplicates”, {"Client", “Product”}, {{"AllData", each _, type table [Client=nullable text, Product=nullable text, Date=nullable date]}}), #“Added Custom1” = Table.AddColumn(#“Grouped Rows”, "Custom", each Table.AddIndexColumn([AllData],“Index”,0)), #“Removed Columns2” = Table.RemoveColumns(#“Added Custom1”,{"Client", “Product”, “AllData”}), #“Added Custom3” = Table.AddColumn(#“Removed Columns2”, “Custom.1”, each List.Accumulate( [Custom], {}, (state, current) => let previous = if List.Count(state) > 0 then List.Last(state) else null, group = if previous <> null and Date.AddDays(previous, 1) = current then List.Max(state) else List.Max(state) + 1 in state & {group} )), #“Expanded Custom.1” = Table.ExpandListColumn(#“Added Custom3”, “Custom.1”), #“Renamed Columns2” = Table.RenameColumns(#“Expanded Custom.1”,{{“Custom.1”, “Group”}}) in #“Renamed Columns2”
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.