Forum Discussion
Updating table rows based on earlier row
- Anonymous2 years ago
Hi PaulBoden ,
You can create a calculated column as below to get it, please find the details in the attachment.
Output = VAR _id = 'Table'[ID] VAR _date = 'Table'[Date] VAR _case = 'Table'[Case] VAR _level = 'Table'[Level] VAR _ladjdate = CALCULATE ( MIN ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[ID] = _id && 'Table'[Flag] = "Adj" && 'Table'[Case] = "L" ) ) VAR _madjdate = CALCULATE ( MIN ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[ID] = _id && 'Table'[Flag] = "Adj" && 'Table'[Case] = "M" ) ) RETURN IF ( ( 'Table'[ID] = _id && NOT ( ISBLANK ( _ladjdate ) ) && 'Table'[Date] >= _ladjdate && 'Table'[Case] <> "L" ) || ( 'Table'[ID] = _id && NOT ( ISBLANK ( _madjdate ) ) && 'Table'[Date] >= _madjdate && 'Table'[Case] <> "M" && 'Table'[Level] = _level ), "H" )Best Regards
Apologies for being unclear - The table should be formatted as below.
| ID | Case | Flag | Level | Date | Output |
| 1 | HC | Source | 1 | 03/07/2024 | S |
| 1 | HC | Source | 1 | 04/07/2024 | S |
Hi PaulBoden ,
Could you please explain the judgement logic of getting the field [Output] base on the shared sample data and below formula? For example: if xx="Adj" && xxx = xxx then xx
IF Flag = “Adj” (
IF Case = “L” (
For any Date >= Than X.Date WHERE ID = X.ID And Case != X.Case
Output = “H”
)
IF Case = “M” (
For any Date >= Than X.Date WHERE ID = X.ID And Case != X.Case
Level = X.Level
)
)
Best Regards
- PaulBoden2 years ago
Helper I
I'm sorry, I'm not sure I understand the question.
If a record has "[Flag] = "Adj" and [Case] = "L", I want to set [Output] ="H" for all other records with the same [ID] where [Date] > than the date value contained in this record and where [Case] != "L"".
The second part of the logic ([Case] != "L") is because eventually the golden source will update and a record will be ingested containing x.[ID], x.[Case] and [Flag] = Source.
Hopefully this helps clarify?
Someone else suggested the below to me:
let Source = Table.Sort(your_table, {"ID", "Date"}), rec = (x) => [L = {"Output", each "H"}, M = {"Level", each x{0}[Level]}], fx = (tbl) => Table.TransformColumns(tbl, Record.FieldOrDefault(rec(tbl), tbl{0}[Case], {})), group = Table.Group( Source, {"ID", "Case"}, {"x", fx}, GroupKind.Local, (s, c) => Number.From(s[ID] <> c[ID] or List.Contains({"L", "M"}, c[Case])) ), z = Table.Combine(group[x]) in zThis seems to achive close to the outcome I'm looking for, although I suspect for performance optimisation, it would be better to execute this as a step within the existing query, rather than a standalone. Additionally, the outputs seem slightly different from what I'm expecting. I wonder if it's because the flag logic isn't considered and the rules are applied to all records regardless.
If you have any thoughts on how to update to achieve the correct output, I'd be greatly appreciative.
Thanks
- Anonymous2 years agoNot applicable
Hi PaulBoden ,
You can create a calculated column as below to get it, please find the details in the attachment.
Output = VAR _id = 'Table'[ID] VAR _date = 'Table'[Date] VAR _case = 'Table'[Case] VAR _level = 'Table'[Level] VAR _ladjdate = CALCULATE ( MIN ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[ID] = _id && 'Table'[Flag] = "Adj" && 'Table'[Case] = "L" ) ) VAR _madjdate = CALCULATE ( MIN ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[ID] = _id && 'Table'[Flag] = "Adj" && 'Table'[Case] = "M" ) ) RETURN IF ( ( 'Table'[ID] = _id && NOT ( ISBLANK ( _ladjdate ) ) && 'Table'[Date] >= _ladjdate && 'Table'[Case] <> "L" ) || ( 'Table'[ID] = _id && NOT ( ISBLANK ( _madjdate ) ) && 'Table'[Date] >= _madjdate && 'Table'[Case] <> "M" && 'Table'[Level] = _level ), "H" )Best Regards