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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
chuyiyi
New Member

date period

Dear all, good day 

 

Two tables:

Table 1 has these fields , Start Date 、End Date 、Location.

Table 2 has Date Filed , how to estimate the date in which period and get the Location Value.

 

Please see screenshot below.

 

chuyiyi_0-1693230000018.png

 

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

Hello, @chuyiyi 

let
    Table1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Table2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    combo = Table.RenameColumns(Table1, {"Start Date", "Order Date"}) & Table.AddColumn(Table2, "mark", each 1),
    sort = Table.Sort(combo,{{"Order Date", Order.Ascending}, {"mark", Order.Descending}}),
    fdown = Table.FillDown(sort,{"Location"}),
    filter = Table.SelectRows(fdown, each ([mark] = 1)),
    select = Table.SelectColumns(filter,{"Location", "Order Date"})
in
    select

View solution in original post

6 REPLIES 6
AlienSx
Super User
Super User

Hello, @chuyiyi 

let
    Table1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Table2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    combo = Table.RenameColumns(Table1, {"Start Date", "Order Date"}) & Table.AddColumn(Table2, "mark", each 1),
    sort = Table.Sort(combo,{{"Order Date", Order.Ascending}, {"mark", Order.Descending}}),
    fdown = Table.FillDown(sort,{"Location"}),
    filter = Table.SelectRows(fdown, each ([mark] = 1)),
    select = Table.SelectColumns(filter,{"Location", "Order Date"})
in
    select
foodd
Community Champion
Community Champion

The question and visually efficient pattern caught my attention. 

While adding this pattern to a project I had open, I noticed

that if you add new values to Table 2 that are not in a range of

Table 1, there is no logic to handle this condition and say

return a blank or null.   Would you have a moment to add 

handling for an out-of-range condition?

 

Table 1 lookup ranges include:

foodd_2-1693234537758.png

 

Update Table 2 entries to include 2018-12-31 and 2023-01-01 

that are not included in the Table 1 ranges.

 

I would have expected 2018-12-31 to return blank, however

would not have expected 2023-01-01 to return a match of 

Location C.

 

Query Results:

foodd_3-1693235455059.png

 

 

Hi, @foodd how about this? 

let
    Table1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Table2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    combo = Table.AddColumn(Table1, "Order Date", each [Start Date]) & Table.AddColumn(Table2, "mark", each 1),
    sort = Table.Sort(combo,{{"Order Date", Order.Ascending}, {"mark", Order.Descending}}),
    fdown = Table.FillDown(sort,{"Location", "End Date"}),
    filter = Table.SelectRows(fdown, each ([mark] = 1)),
    later = Table.ReplaceValue(filter, each [Order Date] > [End Date], null, (v, o, n) => if o then n else v, {"Location"}),
    select = Table.SelectColumns(later,{"Location", "Order Date"})
in
    select
foodd
Community Champion
Community Champion

@AlienSx, thank you.   This works well. 

 

foodd_0-1693243085395.png

 

@AlienSx  

It's such an ingenious code.

Thank you very much.

chuyiyi
New Member

In power query.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.