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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
technicneo
Frequent Visitor

Reformat text to sort

Hi Community,

my problem is the following:
My row "KW" (data type text) has to be sorted in asc order. However when the year changes (2024->2025) the order screws up (orange marked).
I have found a YT-Video Power BI Week Number | Sort Week Number in the right Order | Convert Dates into Week Number in PBI (..., which explains the error and how to solve it in the table view, but in my case, it has to be do it in Power Query. I have tried to use the Number.FormText function, but i only get errors when using this function. Thanks for any help!

technicneo_0-1718014128955.png

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

    Table.Sort(
        your_table,
        (x, y) => Value.Compare(
            Text.BeforeDelimiter(x[KW], "/") & Text.PadStart(Text.AfterDelimiter(x[KW], "/"), 2, "0"),
            Text.BeforeDelimiter(y[KW], "/") & Text.PadStart(Text.AfterDelimiter(y[KW], "/"), 2, "0")
        )
    )

View solution in original post

2 REPLIES 2
ManuelBolz
Responsive Resident
Responsive Resident

Hello @technicneo 

 

In this solution I have integrated 2 possible approaches for you.
1) Sorting by date
2) Sorting by index

The Buffer step is important here. This ensures that the desired sorting is retained.

 

let
    //Sample Test Data
    StartDate = #date(2024, 1, 1),
    EndDate = #date(2025, 12, 31),
    DateList = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1, 0, 0, 0)),
    DateTable = Table.FromList(DateList, Splitter.SplitByNothing(), {"Date"}),
    Type = Table.TransformColumnTypes(DateTable,{{"Date", type date}}),
    ColumnWeek = Table.AddColumn(Type, "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),

    //The Logic
    Index = Table.AddIndexColumn(ColumnWeek, "Index", 1, 1, Int64.Type),
    ColumnYear = Table.AddColumn(Index, "Year", each Date.Year([Date]), Int64.Type),
    ColumnsYearWeek = Table.CombineColumns(Table.TransformColumnTypes(ColumnYear, {{"Year", type text}, {"Week of Year", type text}}, "de-DE"),{"Year", "Week of Year"},Combiner.CombineTextByDelimiter("/", QuoteStyle.None),"KW"),
    Duplicates = Table.Distinct(ColumnsYearWeek, {"KW"}),
    Sort = Table.Sort(Duplicates,{{"Date", Order.Ascending}}),
    Buffer = Table.Buffer(Sort)
in
    Buffer

 


Best regards from Germany
Manuel Bolz


If this post helped you, please consider Accept as Solution so other members can find it faster.

🤝Follow me on LinkedIn

AlienSx
Super User
Super User

    Table.Sort(
        your_table,
        (x, y) => Value.Compare(
            Text.BeforeDelimiter(x[KW], "/") & Text.PadStart(Text.AfterDelimiter(x[KW], "/"), 2, "0"),
            Text.BeforeDelimiter(y[KW], "/") & Text.PadStart(Text.AfterDelimiter(y[KW], "/"), 2, "0")
        )
    )

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.