Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Matt_JEM
Helper I
Helper I

Delate rows based on Column Value

Good day All.

 

I need help with the following please.

The data below is a direct import from our SQL server. I am want to create a master quote file with a unique quote number for each quote. Sometimes a quote get revised and the QuoteVersion number is increased. 

 

I want to keep the Quote data with the highest Quote version Number, all the other rows with the same quote number but with a lower quote version numbers must be deleted. How do I do this in Power Query Editor.

 

EG. When looking at the picture below,  Row 1 must be deleted but Row 2 must be kept and Row 7 must be deleted But Row 8 must be kept.

 

Matt_JEM_0-1740998095022.png

 

I thank you in advance for your assistance.

 

Regards

 

Matt

 

1 ACCEPTED SOLUTION

@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
ExpandedData

 

can you paste a sample data on which work in parallel?

 

BBF

View solution in original post

15 REPLIES 15
Matt_JEM
Helper I
Helper I

Please see the query below.

 

let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data]
in
dbo_QotMaster

Hello BeaBF

 

I have implement the following code. It is not deleting the Row where the Quote version is lower that the next line with a higher quote version.

 

I wwould appreciate your assistance.

 

= let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data],
SortedData = Table.Sort(Source, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),
KeepLatestVersion = Table.Distinct(SortedData, {"Quote"})
in
dbo_QotMaster

 

Kind Regards 

 

Matt

dufoq3
Super User
Super User

Hi @Matt_JEM, check this:

 

Before

dufoq3_0-1741034667120.png

 

After

dufoq3_1-1741034682878.png

 

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

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

BeaBF
Super User
Super User

@Matt_JEM Hi! can you provide your power query code or the data? Basically these are the steps that u have to do in your power query:

 

let
// Step 1: Load the data from the source (Replace with your actual data source)
Source = YourDataSource,

// Step 2: Sort the data by Quote (ascending) and QuoteVersion (descending)
SortedData = Table.Sort(Source, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),

// Step 3: Remove duplicates, keeping only the first (highest version per Quote)
KeepLatestVersion = Table.Distinct(SortedData, {"Quote"})

in
KeepLatestVersion

 

BBF

Good Day BeaBF.

 

I have changed the quote to the following, but then gets the following error.

let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data],
#"SortedData" = Table.Sort(Source, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),
#"KeepLatestVersion" = Table.Distinct(SortedData, {"Quote"})

in
KeepLatestVersion

 

ERROR Message:

 

Expression.Error: The specified sort criteria is invalid.
Details:
[List]

 

I am new to PowerBi and Power Query Editor. I would appreciate your help.

 

Thanks in advance 

 

Matt

@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)
SortedData = Table.Sort(dbo_QotMaster, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),

// Step 3: Remove duplicates, keeping only the latest version per Quote
KeepLatestVersion = Table.Distinct(SortedData, {"Quote"})

in
KeepLatestVersion

 

BBF

Good Day BeaBF

 

The following Code does sort the data correct.

Table.Sort(dbo_QotMaster, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}})

Matt_JEM_0-1741081333102.png

 

But the following code still delete the Row where the version is lowest and delte the row where the status is higher

Matt_JEM_1-1741081501779.png

 

Please help.

 

Regards Matt

 

 

 

Good day BeaBF.

 

I have tried that but get an error. Please see code and error warning below.

 

let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data],
#"SortedData" = Table.Sort(Source, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),
#"KeepLatestVersion" = Table.Distinct(SortedData, {"Quote"})

in
KeepLatestVersion

 

ERROR Message:

 

Expression.Error: The specified sort criteria is invalid.
Details:
[List]

 

I thank you in advance for your assistance.

 

Regards

 

Matt

@Matt_JEM in sorteddata step, you're still using Source step, instead of dbo_QotMaster step. Try with my code:

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: Ensure column names are correct
RenamedColumns = Table.RenameColumns(dbo_QotMaster, {{"Quote", "Quote"}, {"QuoteVersion", "QuoteVersion"}}),

// Step 3: Sort data by Quote (ascending) and QuoteVersion (descending)
SortedData = Table.Sort(RenamedColumns, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),

// Step 4: Remove duplicates, keeping only the latest version per Quote
KeepLatestVersion = Table.Distinct(SortedData, {"Quote"})

in
KeepLatestVersion

 

BBF

Hi BeaBF.

 

I have implemented the code as per your instrections. Please see below.

Data is sorted correctly but the Row with the latest version is still dleted and the row with the earliest version is kept. Version 1 delted and version 0 is kept.

 

let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data],
#"RenamedColumns" = Table.RenameColumns(dbo_QotMaster, {{"Quote", "Quote"}, {"QuoteVersion", "QuoteVersion"}}),
#"SortedData" = Table.Sort(RenamedColumns, {{"Quote", Order.Ascending}, {"QuoteVersion", Order.Descending}}),
#"KeepLatestVersion" = Table.Distinct(SortedData, {"Quote"})
in
KeepLatestVersion

@Matt_JEM Instead of using Table.Distinct(), we should group the data by "Quote" and then keep only the row with the maximum "QuoteVersion".

 

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: Ensure column names are correct (modify if needed)
RenamedColumns = Table.RenameColumns(dbo_QotMaster, {{"Quote", "Quote"}, {"QuoteVersion", "QuoteVersion"}}),

// Step 3: Group by Quote and keep the row with the MAX QuoteVersion
GroupedData = Table.Group(
RenamedColumns,
{"Quote"},
{{"AllData", each Table.Max(_, "QuoteVersion")}}
),

// Step 4: Expand the grouped data to get the latest version row for each Quote
ExpandedData = Table.ExpandRecordColumn(GroupedData, "AllData", Table.ColumnNames(RenamedColumns))

in
ExpandedData

 

BBF

Good day @BeaBF and @dufoq3 

 

I have implemented the code, please see below. And get the following error

Expression.Error: The field 'Quote' already exists in the record.
Details:
Name=Quote
Value=

 

let
Source = Sql.Databases("jt-sysprosqlsvr"),
SysproCompanyJEM = Source{[Name="SysproCompanyJEM"]}[Data],
dbo_QotMaster = SysproCompanyJEM{[Schema="dbo",Item="QotMaster"]}[Data],
#"RenamedColumns" = Table.RenameColumns(dbo_QotMaster, {{"Quote", "Quote"}, {"QuoteVersion", "QuoteVersion"}}),
#"GroupedData" = Table.Group(RenamedColumns,{"Quote"},{{"AllData", each Table.Max(_, "QuoteVersion")}}),
#"ExpandedData" = Table.ExpandRecordColumn(GroupedData, "AllData", Table.ColumnNames(RenamedColumns))
in
ExpandedData

@Matt_JEM Fixed code below:

 

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)
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)}}),

// Step 4: Expand the grouped data to get the latest version row for each Quote
ExpandedData = Table.ExpandTableColumn(GroupedData, "AllData", Table.ColumnNames(dbo_QotMaster))

in
ExpandedData

 

BBF

Hello @BeaBF & @dufoq3 

 

Sorry but the code as below is not working. Keep getting the following error.

Expression.Error: The field 'Quote' already exists in the record.
Details:
Name=Quote
Value=

 

 

@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
ExpandedData

 

can you paste a sample data on which work in parallel?

 

BBF

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors