March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi
I have a table that has a list of categories alongwith account IDs, each account ID will have a varying number of categories, and we have a date column for an account active date.
Unfortunately there is no timestamp as to when a new category appears in the table, is there a way in Power Query that I can create a column that will auto assign a date when a new category row is added (ideally the date when it first appeared such as a refresh), and where there is no date for pre existing categories, it will default to the account active date?
Any help would be greatly appreciated.
Solved! Go to Solution.
To achieve this in Power Query, you can follow these steps:
Here's the step-by-step implementation in Power Query:
let
// Load your table
Source = ...,
// Sort by Account ID and Date
SortedTable = Table.Sort(Source,{{"Account ID", Order.Ascending},{"Date", Order.Ascending}}),
// Add an index column
AddIndex = Table.AddIndexColumn(SortedTable, "Index", 0, 1, Int64.Type),
// Add custom column to check for new categories
AddNewCategoryFlag = Table.AddColumn(AddIndex, "NewCategoryFlag", each if [Account ID] = AddIndex{[Index]-1}[Account ID] and [Category] <> AddIndex{[Index]-1}[Category] then 1 else 0),
// Use List.Accumulate to keep track of new category dates
AddNewCategoryDate = Table.AddColumn(AddNewCategoryFlag, "NewCategoryDate", each
let
CategoryDates = List.Accumulate(
{0..[Index]},
{},
(state, current) =>
let
Date = if AddNewCategoryFlag{current}[NewCategoryFlag] = 1 then AddNewCategoryFlag{current}[Date] else null
in
state & {Date}
)
in
List.Last(CategoryDates)),
// Fill down the new category date column
FillDownNewCategoryDate = Table.FillDown(AddNewCategoryDate,{"NewCategoryDate"}),
// Replace null values with Account Active Date
ReplaceNullWithActiveDate = Table.ReplaceValue(FillDownNewCategoryDate,null, each [Account Active Date], Replacer.ReplaceValue,{"NewCategoryDate"})
in
ReplaceNullWithActiveDate
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
To achieve this in Power Query, you can follow these steps:
Here's the step-by-step implementation in Power Query:
let
// Load your table
Source = ...,
// Sort by Account ID and Date
SortedTable = Table.Sort(Source,{{"Account ID", Order.Ascending},{"Date", Order.Ascending}}),
// Add an index column
AddIndex = Table.AddIndexColumn(SortedTable, "Index", 0, 1, Int64.Type),
// Add custom column to check for new categories
AddNewCategoryFlag = Table.AddColumn(AddIndex, "NewCategoryFlag", each if [Account ID] = AddIndex{[Index]-1}[Account ID] and [Category] <> AddIndex{[Index]-1}[Category] then 1 else 0),
// Use List.Accumulate to keep track of new category dates
AddNewCategoryDate = Table.AddColumn(AddNewCategoryFlag, "NewCategoryDate", each
let
CategoryDates = List.Accumulate(
{0..[Index]},
{},
(state, current) =>
let
Date = if AddNewCategoryFlag{current}[NewCategoryFlag] = 1 then AddNewCategoryFlag{current}[Date] else null
in
state & {Date}
)
in
List.Last(CategoryDates)),
// Fill down the new category date column
FillDownNewCategoryDate = Table.FillDown(AddNewCategoryDate,{"NewCategoryDate"}),
// Replace null values with Account Active Date
ReplaceNullWithActiveDate = Table.ReplaceValue(FillDownNewCategoryDate,null, each [Account Active Date], Replacer.ReplaceValue,{"NewCategoryDate"})
in
ReplaceNullWithActiveDate
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Thanks this is perfect and a great solution
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
21 | |
16 | |
13 | |
12 | |
9 |
User | Count |
---|---|
34 | |
31 | |
20 | |
19 | |
17 |