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|
My code is
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"}), #"Expanded Custom1" = Table.ExpandTableColumn(#"Removed Columns2", "Custom", {"Client", "Product", "Date", "Index"}, {"Client", "Product", "Date", "Index"}) in #"Expanded Custom1"
- Assistant
To create the "Group" column based on consecutive dates for each unique combination of Customer and Product, you can use the following M code:
let Source = YourSourceTable, // replace with the actual source table name #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Customer", type text}, {"Product", type text}, {"Index", Int64.Type}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer", Order.Ascending}, {"Product", Order.Ascending}, {"Date", Order.Ascending}}), #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "GroupIndex", 0, 1), #"Added Custom" = Table.AddColumn(#"Added Index", "Group", each if [GroupIndex] = 0 then 1 else if #"Added Index"[Customer]{[GroupIndex]} <> #"Added Index"[Customer]{[GroupIndex]-1} then [GroupIndex]+1 else [GroupIndex]), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"GroupIndex"}) in #"Removed Columns"This code assumes that your source table is named "YourSourceTable". Replace it with your actual table name.