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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
bskiran1711
Regular Visitor

CSV file Sorting Issues

Hi,

 

I have  a CSV file from which

1. we create a new column "Sort" based on the Column "Loan Type". 

2. Data is sorted based on Column "TS_Number" and then on the Column "Sort". 

3. Duplicates are removed from Column "TS Number"

4. Data is selected for Current year.

 

I see the results dont match to the manual Excel Calculation.

 

let
Source = Csv.Document(File.Contents("C:\Users\Bangalore.Kiran\OneDrive - Archwell Holdings LLC\Nestor_Pipeline\current_holds.csv"),[Delimiter=",", Columns=5, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"MasterId", Int64.Type}, {"TSNumber", type text}, {"StateCode", type text}, {"HoldReason", type text}, {"StartDate", type datetime}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Sort", each if [HoldReason] = "Bankruptcy" then 1 else 2),
#"Sorted Rows" = Table.Sort(#"Added Conditional Column",{{"TSNumber", Order.Ascending}, {"Sort", Order.Ascending}}),
#"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"TSNumber"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Duplicates", each Date.IsInCurrentYear([StartDate]))
in
#"Filtered Rows"

6 REPLIES 6
Anonymous
Not applicable

Hi @bskiran1711 ,

 

Thanks for reaching out to our community.

Pls has your problem been solved? If so, accept the reply as a solution. This will make it easier for the future people to find the answer quickly.

If not, please provide a more detailed description.

 

Best Regards,

Stephen Tao

 

PwerQueryKees
Super User
Super User

Table.Distinct does not have a garantueed sort order as result. So do the distinct first and then the sort.

Omid_Motamedise
Super User
Super User

in the second step, ( Data is sorted based on Column "TS_Number" and then on the Column "Sort". ) when you sort the table, a functin is generated like 

List.Sort(.............) add Table.Buffer into it and rewrite it as Table.Buffer(List.Sort(.............)).

it make power query to sort the data first and then continue other steps, otherwise, power query will reorder the steps and apply the sort on the last result.

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
ZhangKun
Super User
Super User

add Table.Buffer function:

let
Source = Csv.Document(File.Contents("C:\Users\Bangalore.Kiran\OneDrive - Archwell Holdings LLC\Nestor_Pipeline\current_holds.csv"),[Delimiter=",", Columns=5, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"MasterId", Int64.Type}, {"TSNumber", type text}, {"StateCode", type text}, {"HoldReason", type text}, {"StartDate", type datetime}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Sort", each if [HoldReason] = "Bankruptcy" then 1 else 2),
#"Sorted Rows" = Table.Buffer(Table.Sort(#"Added Conditional Column",{{"TSNumber", Order.Ascending}, {"Sort", Order.Ascending}})),
#"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"TSNumber"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Duplicates", each Date.IsInCurrentYear([StartDate]))
in
#"Filtered Rows"

 

Hi

Can you please let me know how to add table.buffer. Thanks in Advance

 

Regards

Kiran

add it in #"Sorted Rows"(I have added it in the reply above).

 

Or you also add it in #"Removed Duplicates", see following code:

#"Removed Duplicates" = Table.Distinct(Table.Buffer(#"Sorted Rows"), {"TSNumber"}),

 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.