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. How to write the M expression to compare current row with next row and assigning 0 if the difference is 1 day and assigning 1 if the difference is more than 1 day?
- Assistant
To compare the current row with the next row and assign values based on the difference in dates, you can use the following M expression in Power Query:
let // Load the table Source = // Your source table here (replace this comment) GroupAdded = Table.AddColumn(Source, "Group", each GetGroup([Customer], [Product], [Date]), Int64.Type), Grouped = Table.Group(GroupAdded, {"Customer", "Product", "Group"}, {{"Dates", each [Date], type date}}), Result = Table.ExpandTableColumn(Grouped, "Dates", {"Date"}, {"Date"}), IndexAdded = Table.AddIndexColumn(Result, "Index", 0, 1, Int64.Type) in IndexAdded
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.