Forum Discussion
Delate rows based on Column Value
- 1 year ago
Matt_JEM Try with:
let
// Step 1: Connect to SQL Database and Load Table
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo", Item="QotMaster"]}[Data],// Step 2: Sort data by Quote (ascending) and QuoteVersion (descending) so the highest version appears first
SortedData = Table.Sort(dbo_QotMaster, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),// Step 3: Group by Quote, keeping only the first row (which now has the highest QuoteVersion)
GroupedData = Table.Group(SortedData, {"Quote"}, {{"AllData", each Table.FirstN(_, 1), type table [Quote=nullable text, QuoteVersion=nullable number, QuoteStatus=nullable text]}}),// Step 4: Expand the grouped table, but exclude the duplicated "Quote" column
ExpandedData = Table.ExpandTableColumn(GroupedData, "AllData", {"QuoteVersion", "QuoteStatus"})in
ExpandedDatacan you paste a sample data on which work in parallel?
BBF
Hi Matt_JEM, check this:
Before
After
Replace word GroupKind.Local with GroupKind.Global in code if your [Quote] column is not sorted in the table, but keep it if it is. Do not sort it as a separate step - it is not necessary.
let
Source = Table.FromColumns({{"0001", "0001", "0002", "0003", "0004", "0004"}, {0,1,0,0,1,0}}, {"Quote", "QuoteVersion"}),
FilteredMaxQuoteVersion = Table.Combine(Table.Group(Source, {"Quote"}, {{"T", each Table.MaxN(_, {"QuoteVersion"}, 1), type table, GroupKind.Local}})[T])
in
FilteredMaxQuoteVersion