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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
ngocrin1
Frequent Visitor

Create From date & End Date colums base on Date columns event

Hi everyone,

 

I need transform by Power Query with this orignal data (Tbl 1) to new table (tbl 2) and use it to lookup Code column base on Order Date (tbl 3) 

                    1                                                                    2                                                                                  3pq date question.png

 Has everyone help me to solve this problem?

Thank you.

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @ngocrin1 ,

About Step1(Tbl 1 > Tbl 2):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMFTSUTLUN9Q3MjAyUYrVgYuZ6hthiJmhqTPCotcIRW8sAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}, {"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Items"}, {{"Count", each Table.AddIndexColumn(_, "Code", 1, 1, Int64.Type)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "Code"}, {"Date", "Code"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Count",{"Items", "Code", "Date"}),
    #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Custom", each let
        currentItems = [Items],
        currentCodePlusOne = [Code] + 1,
        relatedRow = Table.SelectRows(#"Reordered Columns", each [Items] = currentItems and [Code] = currentCodePlusOne)
    in
        if Table.IsEmpty(relatedRow) 
        then null 
        else Date.AddDays(relatedRow{0}[Date], -1)),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Date", "From"}, {"Custom", "To"}})
in
    #"Renamed Columns"

vjunyantmsft_0-1719986248091.png


Then about Step2(Tbl 2 > Tbl 3):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMFTSUTLSN9Q3MjAyUYrVgYuZ6ptjiBka6ZuiCBoBBY31TTDEzOCaYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t, OrderDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}, {"OrderDate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let
    OrderDate = [OrderDate],
    _items=[Items],
    MatchRow = Table.SelectRows(Table, each [Items] = _items and [From] <= OrderDate and [To] >= OrderDate),
    MaxCode=List.Max(Table.SelectRows(Table,each [Items]=_items)[Code]),
    Result = try MatchRow{0}[Code] otherwise null
in
    if Result<>null then Result else MaxCode)
in
    #"Added Custom"

vjunyantmsft_1-1719987186355.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
ngocrin1
Frequent Visitor

Tks @aduguid but I need PQ than by Dax.
@Anonymous  it work but with the fact table (Order) with 100k rows it refresh quite long.

aduguid
Super User
Super User

First create a ranking for the "code"

 

Code = 
RANKX(
    FILTER(
        'ItemsTable',
        'ItemsTable'[Items] = EARLIER('ItemsTable'[Items])
    ),
    'ItemsTable'[Date],
    ,
    ASC,
    DENSE
)

 

Then create a calculated column for the next date in the series

 

To Date = 
VAR CurrentItem = 'ItemsTable'[Items]
VAR CurrentDate = 'ItemsTable'[Date]
RETURN
    CALCULATE(
        MIN('ItemsTable'[Date]) - 1,
        FILTER(
            'ItemsTable',
            'ItemsTable'[Items] = CurrentItem &&
            'ItemsTable'[Date] > CurrentDate
        )
    )

 

 

Anonymous
Not applicable

Hi @ngocrin1 ,

About Step1(Tbl 1 > Tbl 2):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMFTSUTLUN9Q3MjAyUYrVgYuZ6hthiJmhqTPCotcIRW8sAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}, {"Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Items"}, {{"Count", each Table.AddIndexColumn(_, "Code", 1, 1, Int64.Type)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "Code"}, {"Date", "Code"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Count",{"Items", "Code", "Date"}),
    #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Custom", each let
        currentItems = [Items],
        currentCodePlusOne = [Code] + 1,
        relatedRow = Table.SelectRows(#"Reordered Columns", each [Items] = currentItems and [Code] = currentCodePlusOne)
    in
        if Table.IsEmpty(relatedRow) 
        then null 
        else Date.AddDays(relatedRow{0}[Date], -1)),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Date", "From"}, {"Custom", "To"}})
in
    #"Renamed Columns"

vjunyantmsft_0-1719986248091.png


Then about Step2(Tbl 2 > Tbl 3):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMFTSUTLSN9Q3MjAyUYrVgYuZ6ptjiBka6ZuiCBoBBY31TTDEzOCaYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t, OrderDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}, {"OrderDate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let
    OrderDate = [OrderDate],
    _items=[Items],
    MatchRow = Table.SelectRows(Table, each [Items] = _items and [From] <= OrderDate and [To] >= OrderDate),
    MaxCode=List.Max(Table.SelectRows(Table,each [Items]=_items)[Code]),
    Result = try MatchRow{0}[Code] otherwise null
in
    if Result<>null then Result else MaxCode)
in
    #"Added Custom"

vjunyantmsft_1-1719987186355.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors